1、/注:红色为自己所做答案1. Given:1. public class returnIt 2. returnType methodA(byte x, double y)3. return (short) x/y * 2 /强制转换只转换了 x y 未变;4. 5. What is the valid returnType for methodA in line 2?A. intB. byteC. longD. shortE. floatF. double/ byte 和 double 运算后得到的应该是double 所以答案为 F:double注释:short 类型的 x,除以 double
2、 类型的 y,再乘 int 的 2,所以结果是 double 类型的。注意第三行的强制转换,只是转换了 x。2. 1) class Super2) public float getNum()return 3.0f; 3) 4) 5) public class Sub extends Super 6) 7) which method, placed at line 6, will cause a compiler error? A. public float getNum()return 4.0f; B. public void getNum() C. public void getNum(do
3、uble d) D. public double getNum(float d)return 4.0d; 注意这道题主要考的是方法的 overload 和 override。对于 overload,只有参数列表不同,才做为标准,而返回值和访问控制关键字不能做为标准,所以 B 错在方法名相同,但只有返回值不同,这是错的。C 和 D 是正确的 overload。对于 override,则访问控制关键字只能更加公有化,异常只能是超类方法抛出的异常的子类,也可以不抛出。返回类型,参数列表必须精确匹配。所以 A 是正确的 override。3. 1)public class Foo 2) public
4、static void main(String args) 3) tryreturn; 4) finally System.out.println(“Finally“); 5) 6) what is the result? A. The program runs and prints nothing.B. The program runs and prints “Finally”./遇到 return 则 finally 先被执行再执行 returnC. The code compiles, but an exception is thrown at runtime.D. The code w
5、ill not compile because the catch block is missing.trycatchfinally 的问题。程序中如果遇到 return,则 finally 块先被执行,然后再执行 retrun,而 finally 块后面的语句将不被执行。如果遇到 System.exit(1),则 finally块及其后的语句都不执行,整个程序退出,还执行什么呀。4. 1) public class Test 2) public static String output=“; 3) public static void foo(int i) 4) try 5) if(i=1)
6、 6) throw new Exception(); 7) 8) output +=“1“; 9) 10) catch(Exception e) 11) output+=“2“; 12) return; /所以没输出 413) 14) finally 15) output+=“3“; 16) 17) output+=“4“; 18) 19) public static void main(String args) 20) foo(0); 134 2321) foo(1); 22) 23) 24) what is the value of output at line 22? /13423执行第
7、一个 foo(0)时,执行第 8 条语句,output=1 ,然后执行语句 15,output=13,然后是 17 条,output=134,因为是 static 类型的变量,所以任何对其值的修改都有效。执行第二条 foo(1),先执行语句 5,结果抛出异常,转到 catch 块,output=1342,finally 任何情况下都执行,所以 output=13423,然后 return 跳出方法体,所以 output=134235 1)public class IfElse 2)public static void main(String args) 3)if(odd(5) 4)System
8、.out.println(“odd“); 5)else 6)System.out.println(“even“); 7) 8)public static int odd(int x)return x%2; 9) what is output? 编译错误 因为 if()里面的返回值必须是 boolean 类型的 ,而本题中不是if 中的判断条件的结果必须是 boolean 类型的。注意这里说的是结果 .6 1)class ExceptionTest 2)public static void main(String args) 3)try 4)methodA(); 5)catch(IOExcept
9、ion e) 6)System.out.println(“caught IOException“); 7)catch(Exception e) 8)System.out.println(“caught Exception“); 9) 10) 11) If methodA() throws a IOException, what is the result? 如果 methodA()抛出 IOExecption,被语句 6 捕获,输出 caught IOException,然后呢?然后就结束了呗。71)int i=1,j=10; 2)do 3) if(i+-j) continue; 4)whil
10、e(ij)break tp; System.out.println(“i=“+i+“,j=“+j); what is the result? A.i=1,j=-1 B. i=0,j=-1 C.i=1,j=4 D.i=0,j=4 E.compile error at line 4 Answer: b break tp;退出了最外层的 for 循环,程序接着从 System 开始执行。40 Given:1. public class ForBar 2. public static void main(String args) 3. int i = 0, j = 5;4. tp: for (;) 5
11、. i +;6. for(;)7. if(i -j) break tp;8. 9. system.out.printIn(“i = ” + i + “, j = “+ j);10. 11. What is the result?A.The program runs and prints “i=1, j=0”B.The program runs and prints “i=1, j=4”C.The program runs and prints “i=3, j=4”D.The program runs and prints “i=3, j=0”E.An error at line 4 cause
12、s compilation to fail F.An error at line 7 causes compilation to failAnswer A在第五行 i 就变成了 1,第七行里 j 就一直往下减吧,然后退出最外层的 for41public class Foo public static void main(String args) trySystem.exit(0); finallySystem.out.println(“Finally“); what is the result? A.print out nothing B.print out “Finally“Answer:
13、ASystem.exit(0)可以强行终止 JVM,可见优先级最高啦,有了它,finally 就玩完了。Systtem.exit()中不只可以放 0,可以是任何整数和字符。42which four types of objects can be thrown use “throws“? A.Error B.Event C.Object D.Excption E.Throwable F.RuntimeException Answer: A,D,E,Fthrows 可以抛出各种异常,但 Event 和 Object 算哪门子的异常呀?431)public class Test 2) public
14、static void main(String args) 3) unsigned byte b=0; 4) b-; 5) 6) 7) what is the value of b at line 5? A.-1 B.255 C.127 D.compile fail E.compile succeeded but run error Answer: d unsigened byte?java 里怎么可以这样子定义变量哩?44public class ExceptionTest class TestException extends Exception public void runTest() throws TestException public void test() /* point x */ runTest();