一点小题目

1. 
class Base {}

class Agg extends Base{
public String getFields(){
String name =  "Agg";
return name;
}
}

public class Avf{

public static void main(String argv[]){
       Base a = new Agg();
//Here
}
}
What code placed after the comment //Here will result in calling the getFields method resulting in the output of the string "Agg"?

1) System.out.println(a.getFields());
2) System.out.println(a.name);
3) System.out.println((Base) a.getFields());
4) System.out.println( ((Agg) a).getFields());

a1的引用类型为base,而不是agg,所以其中不会有getfields方法,改成Agg a =new Agg()编译才会正确
answer 2:variable name not found..
answer 3:the reason  is the same as answer1
answer 4:correct
 

2thread has four states:
ready, running, non-runnable(sleeping, waiting,Blocked),dead
The relationship is shown as following
ready   <-> running
running -> dead, non-runnable
non-runnable -> ready

3.What happens when you try to compile and run the following program?

class Mystery {
 String s;
 public static void main(String[] args) {
   Mystery m = new Mystery();
   m.go();
 }

 void Mystery() {
   s = "constructor";
 }
 
 void go() {
   System.out.println(s);
 }
}

Select the one right answer.

this code runs and writes "null" in the standard output

 

构造函数没有返回值,即在构造函数前面不要写任何东西,void也不能写(除了可以写public).
如果写了void或者其他返回类型,比如此例中的
void Mystery();//这时,这个函数已经不是构造函数了,而且一个普通的函数,尽管与类和构造函数同名,但是他已经是一个普通的函数了,返回类型是void.

也就是说在此例中,其实是没有显示的写出构造函数。所以s当然为空。
4. 
class A {
  static int si=5;
  static { System.out.println(si);}
  A(){ si++;}
  public static void main(String args[]) {
         for (int i=0;i<3;i++)
              new A();
       }
  }
 
参考答案:print out "5" once
当类加载的时候首先是初始化static类变量和执行static类方法,只有这一次机会。此时也许还没有一个类实例。以后所有对象都共享static变量。对static类变量的修改会影响到
所有的类实例。
class Z {
       public static void main(String args[]) {
              System.out.println("AAA"+new Z());
       }
       public String toString() {
              System.out.println("###");
              return "Z";
       }
  }
 
参考答案:"###" followed by "AAAZ"
要打印"AAA"+new Z(),就要先把他翻译成字符串,要想翻译成字符串就要先执行z.toString(),所以先遇到了打印“###”,于是打印了出来,然后z.toString()执行完毕,然后开始执行打印"AAA"+"z",顺序是这样的。
6. 
1. public class Test {
2. private int i = j;
3. private int j = 10;
4.
5. public static void main(String args[]) {
6. System.out.println((new Test()).i);
7. }
8. }

[Select one correct answer]
a. Compiler error complaining about access restriction of private variables of Test.
b. Compiler error complaining about forward referencing.
c. No error - The output is 0;
d. No error - The output is 10;
答案是: b
JAVA 中类的编译顺序是只要建立了对象实例,即NEW,就会先初始化变量,在调用CONSTRUCTOR,而变量的初始化是必须按顺序的,所以,第一题选B
What is the output of the following program

1. public class Test {
2. private int i = giveMeJ();
3. private int j = 10;
4.
5. private int giveMeJ() {
6. return j;
7. }
8.
9. public static void main(String args[]) {
10. System.out.println((new Test()).i);
11. }
12. }

Select one correct answer
a. Compiler error complaining about access restriction of private variables of AQuestion.
b. Compiler error complaining about forward referencing.
c. No Compilation error - The output is 0;
d. No Compilation error - The output is 10;
答案是: c。生成test类,首先为i,j分配空间并初始化为0,然后执行i=giveMeJ(),这时j还是0,所以i的结果为0;然后执行j=10。所以最后输出为0。
float f=4.2F;
Float g=new Float(4.2F);
Double d=new Double(4.2);
Which are true?
A. f==g   
B. g==g   
C. d==f   
D. d.equals(f) 
E d.equals(g) 
F. g.equals(4.2);
 
Answer: B
== 两边类型不同不相等。所以 A C 不等。 equals 只能用于引用类型,不能用于基本类型,所以 D 不对,而且两边类型不兼容的话 , 即使对象的内容一样 , 也不相等,所以 E F 不对。9
9
 
1)public class Test{
2) public static void add3(Integer i){
3)        int val=i.intValue();
4)        val+=3;
5)        i=new Integer(val);
6) }
7) public static void main(String args[]){
8)       Integer i=new Integer(0);
9)       add3(i);
10)       System.out.println(i.intValue());
11) }
12)}
 what is the result?
 A. compile fail       
 B.print out "0"      
 C.print out "3"   
 D.compile succeded but exception at line 3
 
Answer: b
在第五行里,程序又操作了 New ,重新分配了内存空间。所以此 i 非彼 i 啦。
10
 
1)class EnclosingOne{
2)public class InsideOne{}
3) }
4)public class InnerTest{
5) public static void main(String args[]){
6) EnclosingOne eo=new EnclosingOne();
7) //insert code here
8) }
9)}
A.InsideOne ei=eo.new InsideOne();
B.eo.InsideOne ei=eo.new InsideOne();
C.InsideOne ei=EnclosingOne.new InsideOne();
D.InsideOne ei=eo.new InsideOne();
E.EnclosingOne.InsideOne ei=eo.new InsideOne();
 
Answer: e
这里边的一些形式是固定的。
1 )静态方法访问非静态内类:
                     方法为:
       Outer myouter=new Outer(); //这里的 myouter 是创建的外部类的对象。
       Outer.Inner myinner=myouter.new Inner(); // myinner 是内类的对象。
       然后再 myinner.showName(); // showName() 是外类中的非静态方法。
2 )非静态方法访问非静态内类
                     直接创建该内部类的对象: new Inner().showName();
3 )静态方法访问静态内类:
                     也是直接创建该内部类的对象,即 Inner myinner = new Inner() ,或者 Outer.Inner myinner = new Outer.Inner() 也行得通哦。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值