笔试之SCJP5

 

1、public class X {

2. public static void main(String [] args) {

3. try {

4. badMethod();

5. System.out.print("A");

6. }

7. catch (Exception ex) {

8. System.out.print("C");

9. }

10. finally {

11. System.out.print("B");

12. }

13. System.out.print("D");

14. }

15. public static void badMethod() {

16. throw new Error();

17. }

18. }

What is the result?

解析:   运行打印出“B” 同时抛了异常(运行时异常)。 因为Error对象是集成自Throwable而非Excpetion, 所以其不能被Exception类给捕获。如果将代码catch中的Exception修改为Throwable则打印出“CBD”。

 

2、

 class Exc0 extends Exception { }

2. class Exc1 extends Exc0 { }

3. public class Test {

4. public static void main(String args[]) {

5. try {

6. throw new Exc1();

7. } catch (Exc0 e0) {

8. System.out.println("Ex0 caught");

9. } catch (Exception e) {

10. System.out.println("exception caught");

11. }

12. }

13. }

What is the result?

 

 

 

 解析: 异常处理的规则是如果出现了异常则按照catch顺序捕获, 但是如果前面的已经捕获了则后续的catch语句就不会被执行了  

3、

 

 

 

解析:  此题一直到将其copy到eclipse下时我才知道原因是什么, 因为Line20 的assert flase前有一个while(true){}语句块, 这也就意味着其会一直循环到达不了Line20。 ^_^  ^_^ 

4、

Given:

 

1. public class Outer{

2. public void someOuterMethod() {

3. // Line 3

4. }

5. public class Inner{}

6. public static void main( String[]argv ) {

7. Outer o = new Outer();

8. // Line 8

9. }

10. }

Which instantiates an instance of Inner?

 

A. new Inner(); // At line 3

B. new Inner(); // At line 8

C. new o.Inner(); // At line 8

D. new Outer.Inner(); // At line 8

 

 

 

解析:  此题主要是因为main方法是一个静态方法, 也即在加载好外部类时便调用了的方法, 而内部类相当于外部类的一个普通成员, 内部类是基于外部类对象存在的, 所以B选项错误。至于CD选项则都是写法错误。

 

 

 

5、

What allows the programmer to destroy an object x? A. x.delete()

B. x.finalize()

C. Runtime.getRuntime().gc()

D. Explicitly setting the object's reference to null. E. Ensuring there are no references to the object.

F. Only the garbage collection system can destroy an object.

 

 

 

解析:此题主要是容易出错,除此之外没什么好说的

 

6、

Given:

 

12. float f[][][] = new float[3][][];

13. float f0 = 1.0f;

14. float[][] farray = new float[1][1]; What is valid?

 

A. f[0] = f0;

B. f[0] = farray;

C. f[0] = farray[0];

D. f[0] = farray[0][0];

 

 

 此题我刚开始做错了, 其实会编译报错,因为i是局部变量最后那航打印语句肯定会报错撒。粗心呀  ....   .... 

8、 一个测试 短路与 的问题

Given:

11. int i = 0, j = 1;

12. if ((i++ == 1) && (j++ == 2)) {

13. i = 42;

14 }

15. System.out.println("i = " + i + ", j = " + j); What is the result?

 

A. i = 1, j = 2

B. i = 1, j = 1

C. i = 42, j = 2

D. i = 42, j = 1

E. Compilation fails.

 

 

7、

Given:

11. for (int i =0; i < 4; i +=2) {

12. System.out.print(i + "");

13. }

14. System.out.println(i); What is the result?

Given:

11. public class Test {

12. public void foo() {

13. assert false;

14. assert false;

15. }

16. public void bar(){

17. while(true){

18. assert false;

19. }

20. assert false;

21. }

22. }

What causes compilation to fail?

 

A. Line 13

B. Line 14

C. Line 18

  D. Line 20

 

A. Ex0 caught

B. exception caught

C. Compilation fails because of an error at line 2. D. Compilation fails because of an error at line 6.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值