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或者其他返回类型,比如此例中的
void Mystery();//这时,这个函数已经不是构造函数了,而且一个普通的函数,尽管与类和构造函数同名,但是他已经是一个普通的函数了,返回类型是void.
也就是说在此例中,其实是没有显示的写出构造函数。所以s当然为空。
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
所有的类实例。
public static void main(String args[]) {
System.out.println("AAA"+new Z());
}
public String toString() {
System.out.println("###");
return "Z";
}
}
参考答案:"###" followed by "AAAZ"
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;
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;