第136題:

Given:

11. public static void main(String[] args){
12.     try{
13.         args = null;
14.         args[0] = "test";
15.         System.out.println(args[0]);
16.     }catch(Exception ex) {
17.         System.out.println("Exception");
18.     }catch(NullPointerException npe){
19.         System.out.println("NullPointerException");
20.     }
21. }

What is the result?

A. test
B. Exception
C. Compilation fails.
D. NullPointerException

答案:

題目範圍:例外處理

解析:

因為NullPointerException永遠不會被捕捉到

編譯階段會出現錯誤,原因是這個例外已經在先前被Exception捕捉了

 

第137題:

Given:

11. class X{public void foo(){System.out.print("X ");}}
12.
13. public class SubB extends X{
14.     public void foo() throws RuntimeException{
15.         super.foo();
16.         if(true) throw new RuntimeException();
17.         System.out.print("B ");
18.     }
19.     public static void main(String[] args){
20.         new SubB().foo();
21.     }
22. }

What is the result?

A. X, followed by an Exception.
B. No output, and an Exception is thrown.
C. Compilation fails due to an error on line 14.
D. Compilation fails due to an error on line 16.
E. Compilation fails due to an error on line 17.
F. X, followed by an Exception, followed by B.

答案:

題目範圍:例外處理

解析:

之前提到過Exception必須被處理,否則編譯階段會出錯

但這裡使用的是RuntimeException,這是一個unchecked exceptions
unchecked exceptions沒有強制必須被處理,跟Error 有點類似的感覺

 

第138題:

Which two code fragments are most likely to cause a StackOverflowError? (Choose two.)

A.    int[] x = {1, 2, 3, 4, 5};
    for(int y=0; y<6; y++)
    System.out.println(x[y]);

B.    static int[] x = {7, 6, 54};
    static{x[1] = 8; x[4] = 3;}

C.    for(int y=10; y<10; y++)
    doStuff(y);

D.    void doOne(int x){doTwo(x);}
    void doTwo(int y){doThree(y);}
    void doThree(int z){doTwo(z);}

E.    for(int x=0; x<10000000000; x++)
    doStuff(x);

F.    void counter(int i){counter(++i);}

答案:DF

題目範圍:例外處理

解析:

當呼叫方法的數量過多時(通常是出現在無限遞迴)就會發生StackOverflowError
會出錯的次數大約是10000次左右

A.出現的是java.lang.ArrayIndexOutOfBoundsException

B.是編譯錯誤

C.不做任何事情,也沒有錯誤

E.會出現編譯錯誤,數值過大超出範圍

 

第139題:

Given a method that must ensure that its parameter is not null:

11. public void someMethod(Object value){
12. //check for null value
...

20. System.out.println(value.getClass());
21. }

What, inserted at line 12, is the appropriate way to handle a null value?

A.    assert value == null;

B.    assert value != null : "value is null";

C.    if (value == null){
        throw new AssertionException("value is null");
    }

D.    if (value == null){
        throw new IllegalArgumentException("value is null");
    }

答案:

題目範圍:例外處理、Assert用法

解析:

雖然B C選項有達到告知的效果
Assert是測試程式時在用的
裡該使用
Exception來處理

 

第140題:

Given:

1. public class A{
2.     public void method1(){
3.         B b = new B();
4.         b.method2();
5.         //more code here
6.     }

7. }
1. public class B{
2.     public void method2(){
3.         C c = new C();
4.         c.method3();
5.         //more code here
6.     }

7. }
1. public class C{
2.     public void method3(){
3.         //more code here
4.         }

5. }

And given:
25. try{
26.     A a = new A();
27.     a.method1();
28. }catch(Exception e){
29.     System.out.print("an error occurred");
30. }

Which two statements are true if a NullPointerException is thrown on line 3 of class C? (Choose two.)

A. The application will crash.
B. The code on line 29 will be executed.
C. The code on line 5 of class A will execute.
D. The code on line 5 of class B will execute.
E. The exception will be propagated back to line 27.

答案:BE

題目範圍:例外處理

解析:

單純的例外傳遞

arrow
arrow
    全站熱搜

    yaya741228 發表在 痞客邦 留言(3) 人氣()