经典考试选择题(一)——每次20题

1 、给出下面的代码段 : public class Base{
int w, x, y ,z;
public Base(int a,int b) {
x=a;
y=b;
}
public Base(int a, int b, int c, int d) {
// assignmentx=a, y=b
w=d;
z=c;
}
} 在代码说明 // assignment x=a, y=b 处写入如下哪个代码是正确的? D

A 、 Base(a,b);
B 、 x=a, y=b;
C 、 this(a),this(b);
D 、 this(a,b);


2 、如下哪个不是 Java 中正确的整数表示 ? D
A 、 22
B 、 0x22 ——8进制
C 、 022 ——16进制
D 、 22H


3 、指出下列程序运行的结果(D) ---- 应该是B

public class Example{
String str=new String(〃good〃);
char〔〕ch={′a′,′b′,′c′};

public static void main(String args〔〕){
Example ex=new Example();
ex.change(ex.str,ex,ch);
System.out.print(ex.str+〃and〃);
System.out.print(ex.ch);
}
public void change(String str,char ch〔〕){
str=〃test ok〃;
ch〔0〕=′g′;
}

} A.good and abc
B.good and gbc
C.test ok and abc
D.test ok and gbc


4 、下面哪条语句定义了 5 个元素的数组 A )
A、int [] a={22,23,24,25,12};
B、int a []=new int(5); ——————定义数组 int a [] = new int【5】
C、int [5] array;
D、int [] arr;


5 、对于 catch 子句的排列,下列哪种是正确的( B ) ————考点是 :catch捕获异常后会直接finally,不会向下继续catch,所以子先父后
A.父类在先,子类在后
B.子类在先,父类在后
C.有继承关系的异常不能在同一个 try 程序段内
D.如何排列都可以


6 、已知如下代码:
1: class Example{
2: String str;
3: public Example(){
4: str= "example";
5: }
6: public Example(String s){
7: str=s;
8: }
9:}
10: class Demo extends Example{
11: }
12: public class Test{
13:public void f () { 14:Example ex = new Example("Good");
15:Demo d = new Demo("Good");
16:} }
哪句语句会导致错误? E —— 构造方法不继承
A 、 line 3
B 、 line 6
C 、 line 10
D 、 line 14
E 、 line 15


7 、给出一段程序,试判断哪个是正确的结果( B
public class rtExcept{
public static void throwit(){
System.out.print(“throwit”);
throw new RuntimeException();
}
public static void main(String [] aa){
try{
System.out.print(“hello “);
throwit();
}
catch(Exception re){
System.out.print(“caught ”);
}
finally{
System.out.print(“finally ”);
}
System.out.print(“after ”);
} }

A、hello throwit caught B、hello throwit caught finally after
C、hello throwit RuntimeException after D、hello throwit caught finally after RuntimeException )


8 、public class Foo{
public static void main(String sgf[]){
StringBuffer a = new StringBuffer("A");
StringBuffer b = new StringBuffer("B");
operate(a,b);
System.out.println(a+","+b);
}
static void operate(StringBuffer x,StringBuffer y){
x.append(y); y=x; }
} What is the result?
A.The code compiles and prints “A.B”.
B.The code compiles and prints “A.A”.
C.The code compiles and prints “B.B”.
D.The code compiles and prints “AB.B”.
E.The code compiles and prints “AB.AB”. 答案是 D

java 中都是按值传递的,所以 a,b 还是指向原来的地址空间,经过 operate 操作后,x 更改 了该地址空间的值,而 y 没有.

public class Foo{ public static void main(String sgf[]){ StringBuffer a = new StringBuffer("A"); StringBuffer b = new
StringBuffer("B"); operate(a,b); //方法调用完以后,a 对象的内容为:AB,b 对象的内容为:B System.out.println(a+","+b); }
static void operate(StringBuffer x,StringBuffer y){ //对象传递进来以后又分别复制了一个 x 和 y 对象 x'和 y', x'和 x 指
向同一个对象。y'和 y 指向同一个对象。 x.append(y); //所以执行此步操作以后,main 中的 x 对象的内容也变化了。 //因为本来
就是指向同一个对象吗! y=x; //执行此步操作以后,main 中的 y 对象的内容没变! //因为此 y 费彼 y 也! } }


9.class ValHold{ public int i = 10; }

public class ObParm{
public static void main(String argv[]){
ObParm o = new ObParm();
o.amethod();
}

public void amethod(){
int i = 99;
ValHold v = new ValHold();
v.i=30;
another(v,i);
System.out.println(v.i);
}//End of amethod

public void another(ValHold v, int i){
i=0;
v.i = 20;
ValHold vh = new ValHold();
v = vh;
System.out.println(v.i+ " "+i);
}//End of another
}
1) 10,0,30
2) 20,0,30
3) 20,99,30
4) 10,0,20 答案是 4)

首先必须明白一点 参数传递到方法内的时候实际上是复制了一份 而且 java 并不直接处理对象,而是通过对象参考来处理的。

public static void main(String argv[]){
ObParm o = new ObParm();
o.amethod();
}
public void amethod(){
int i = 99;
ValHold v = new ValHold();
v.i=30;
another(v,i);
System.out.println(v.i);
}//End of amethod
public void another(ValHold v, int i){ //参数 v,i 传进来后进行复制,并且两个 v 都是指向同一个对象
i=0;
v.i = 20; //这里就导致所指向的对象的 i 值变化
ValHold vh = new ValHold();
v = vh; //这里改变了 v 的对象参考,指向新的一个 ValHold 对象
System.out.println(v.i+ " "+i);
}//End of another
}


10.Given the following code 4)

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());

answer 1:a's reference type is Base ,not Agg,so method getFields not found ... Base a=new Agg(); change>>>> Agg a =new Agg(); compile correct..
answer 2:variable name not found..
answer 3:the reason is the same as answer1
answer 4:correct


11.请看以下例子:
1).
public class Test{
public static void main(String argv[]){
Test t = new Test();
t.amothed();
}

void amothed(){
byte[] a;
System.out.println(a[0]);
}
} 编译时报错:变量 a 可能未被初始化。

2).
public class Test{
public static void main(String argv[]){
Test t = new Test();
t.amothed();
}
void amothed(){
byte[] a = new byte[5];
System.out.println(a[0]);
}
} 编译通过,输出 0。 数组是会自动初始化的……这肯定没错…… 但你必须先把它实例化……
int[] a;//定义一个整型数组,但尚未实例化……
a = new int[3];//实例化 a,同时 a 的所有元素被初始化为 0……
System.out.println(a[0]);
System.out.println(a[1]);
System.out.println(a[2]);


13.For the following code:
class Super
int index = 5;
public void printVal() { System.out.println( "Super" ); }

class Sub extends Super
int index = 2;
public void printVal() { System.out.println( "Sub" ); }

public class Runner
public static void main( String argv[] ) {
Super sup = new Sub();
System.out.print( sup.index + "," );
sup.printVal();
} What will be printed to standard output?
The code compiles and "5, Sub" is printed to standard output. .

成员变量是编译时绑定,
Super sup = new Sub(); //这里声明 sup 为 Super, 所以在编译时, 就确定了 sup.index=5
System.out.print( sup.index + "," );//打印出 5 成员函数则是动态绑定,
sup.printVal(); //尽管 sup 被声明为 super,但是实际上是 new Sub(),所以在运行时, sup实际上指向的一个 Sub 对象。由于是动

态绑定,所以这里执行的是 Sub 的方法。


14.class A { ——[b]这个是类的初始化,并不是实例的初始化,实例的初始化打印3次5[/b] 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 类变量的修改会影响 对 到 所有的类实例。


15.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",顺序是这样的。


16.What is the output of the following program
public class Test {
private int i = giveMeJ();
private int j = 10;
private int giveMeJ() { return j; }
public static void main(String args[]) {
System.out.println((new Test()).i);
}
}
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。
这道题与13题有着异曲同工的意义,在初始化时是类的初始化,这时i,j都有了,但是指向的值均为0


17.What will happen if you compile/run the folowing lines of code?
1: Vector a = new Vector();
2:
3: a.addElement(10);
4:
5: System.out.println(a.elementAt(0));
A) Prints 10
B) Prints 11
C) Compilation error at line 3
D) Prints some garbage.
答案是 C。Vetor 接受的是一个对象,不是一个 primitive type ,接受type时,用add
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值