第126題:

Given:

31. //some code here
32. try{
33.     //some code here
34. }catch(Some Exception se) {
35.     //some code here
36. }finally{
37.     //some code here
38. }

Under which three circumstances will the code on line 37 be executed? (Choose three.)

A. The instance gets garbage collected.

B. The code on line 33 throws an exception.

C. The code on line 35 throws an exception.

D. The code on line 31 throws an exception.

E. The code on line 33 executes successfully

答案:BCE

題目範圍:例外處理

解析:

只要進入了try/catch 裡面,不論如何都會在最後執行finally的內容

 

第127題:

Given:

1. public class Donkey2{
2.     public static void main(String[] args){
3.         boolean assertsOn = true;
4.         assert(assertsOn): assertsOn = true;
5.         if(assertsOn){
6.             System.out.println("assert is on");
7.         }
8.     }
9. }

If class Donkey2 is invoked twice, the first time without assertions enabled, and the second time with assertions enabled, what are the results?

A. no output

B. no output assert is on

C. assert is on

D. no output

 An Assertion Error is thrown.

答案:

題目範圍:assert (斷言),程式除錯階段

解析:

題意為:第一次執行把assertions設為運作,第二次設為不運作。Assert 是一個在除錯階段時才會使用到的關鍵字,他可以藉由編譯器設定來開關這個功能

assert的判別式成立,則不會觸發任何事情;如果不成立,則會觸AssertionError,或是冒號後面的程序

要注意的是:AssertionError或是冒號後面的程序並不會馬上被觸發,如果把assertsOn初始直設為false,則不會印出任何東西,因為執行到if判別式時,assertsOn的值還依然是false

assert關鍵字是提供工程師在除錯階段使用,工程師可以將此關鍵字加在某個片段中測試某個數值是否有如預期。assert並不能取代if 來使用

 

第128題:

Given:

1. public class A{
2.     public void method1(){
3.         try{
4.             B b = new B();
5.             b.Method2();
6.             //more code here
7.         }catch(TestException te){
8.             throw new RuntimeException(te);
9.         }
10.    }
11. }

1. public class B{
2.     public void method2() throws TestException{
3.         //more code here
4.     }

5. }

1. public class TestException extends Exception{
2. }

And given:

31. public void method(){
32.     A a = new A();
33.     a.method1();
34. }

Which statement is true if a TestException is thrown on line 3 of class B?

A. Line 33 must be called within a try block.

B. The exception thrown by method1 in class A is not required to be caught.

C. The method declared on line 31 must be declared to throw a RuntimeException.

D. On line 5 of class A, the call to method2 of class B does not need to be placed in a try/catch block.

答案:

題目範圍:例外處理

解析:

A:不一定要放在try/catch 中,方法本身丟出例外也是可以的

B:用throw new產生出來的例外不用做處理

C:沒有必要擲出任何例外,因為method1中已經做了捕捉例外

D:不一定要放在try/catch 中,方法本身丟出例外也是可以的,不過這個選項是錯的,不知道是怎麼回事,待查證…

 

第129題:

Given:

11. Float pi = new Float(3.14f);
12. if(pi > 3){
13.     System.out.print("pi is bigger than 3. ");
14. }
15. else{
16.     System.out.print("pi is not bigger than 3. ");
17. }
18. finally{
19.     System.out.println("Have a nice day ");
20. }

What is the result?

A. Compilation fails.

B. pi is bigger than 3.

C. An exception occurs at runtime.

D. pi is bigger than 3. Have a nice day.

E. pi is not bigger than 3. Have a nice day.

答案:

題目範圍:基本觀念

解析:

只有做try/catch 時才會出現finally

 

第130題:

Given:

11. static void test(){
12.     try{
13.         String x = null;
14.         System.out.print(x.toString() + " ");
15.     }
16.     finally{System.out.print("finally ");}
17. }
18. public static void main(String[] args){
19.     try{test();}
20.     catch(Exception ex){System.out.print("exception ");}
21. }

What is the result?

A. null

B. finally

C. null finally

D. Compilation fails.

E. finally exception

答案:

題目範圍:例外處理

解析:

Try必須跟catchfinally至少一個搭配使用

因為x.toString()會出現例外,因此直接跳到例外處理,首先印出了finally

之後這個例外被傳到上一層被捕捉到,印出exception

arrow
arrow
    全站熱搜

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