1. 如下代码
class A {
A() { }
}
class B extends A {
}
哪两个说明是正确的?
A. B类的构造器应该是 public.
B. B类的构造器应该是没有参数
C. B类的构造器应该调用this().
D. B类的构造器应该调用super().
答案:BD
解析:默认构造器的修饰符只跟当前类的修饰符有关。 比如B如果是public的,则默认构造方法是public的。
2. 如下代码
public class Test {
public int aMethod() {
static int i = 0;
i ;
return i;
}
public static void main (String args[]) {
Test test = new Test();
test.aMethod();
int j = test.aMethod();
System.out.println(j);
}
}
输出结果是什么?
A. 0
B. 1
C. 2
D. 编译失败
答案:D
解析:static在Java语言中的使用有四种:(成员变量、成员方法、代码块、内部类)。普通函数不能使用static变量。
3.
如下代码:
boolean bool = true;
if(bool = false) {
System.out.println("a");
} else if (bool) {
System.out.println("c");
} else if (!bool) {
System.out.println("c");
} else {
System.out.println("d");
}
输出结果是什么?
A. a
B. b
C. c
D. d
E. 编译失败
答案:C
4.如下代码:
public class SwitchTest{ public static void main(String[]args){ System.out.println("value="+switch(4)); //这里调用了 switch方法,传递了参数 4 } public static int switchIt(int x){ //被调用的switch方法,接收参数,现在 x=4 。 int j = 1;// 定义 j=1 switch (x) { //选择开关,判断 x 的值 case 1:j++; //因为 x=4, 所以 case 1不执行。。 只有case 4的时候才执行 case 2:j++;//因为 x=4, 所以 case 2不执行。。 只有case 4的时候才执行 case 3:j++;//因为 x=4, 所以 case 3不执行。。 只有case 4的时候才执行 case 4:j++;//因为 x=4,所以case 4 执行。 j++ 之后, j=2 case 5:j++; //继续执行, j++ 之后, j=3 default:j++; //继续执行, j++ 之后, j=4 } return j + x; // 返回 j+x 的结果,j=4 ,x=4, 所以返回结果等于 8 } }
输出结果是什么?
A. value = 3
B. value = 4
C. value = 5
D. value = 6
E. value = 7
F. value = 8
答案:F
注意case没有 break; 继续执行6. 以下数组的定义,哪三条是正确的?
A. public int a []
B. static int [] a
C. public [] int a
D. private int a [3]
E. private int [3] a []
F. public final int [] a
答案:A,B,F7. 如下代码:
class Super {
public Integer getLenght() { return new Integer(4); }
}
public class Sub extends Super {
public Long GetLenght() { return new Long(5); }
public static void main(String[] args) {
Super sooper = new Super();
Sub sub = new Sub();
System.out.println(sooper.getLenght().toString() ","
sub.getLenght().toString() );
}
}
输出是什么?
A. 4,4
B. 4,5
C. 5,4
D. 5,5
E. 编译失败.
答案:A8. 在接口中以下哪条定义是正确的? (两个答案)
A. void methoda();
B. public double methoda();
C. public final double methoda();
D. static void methoda(double d1);
E. protected void methoda(double d1);