第121題:

Given:

11. public class Test{
12.     public enum Dogs {collie, harrier, shepherd},
13.    public static void main(String[] args){
14.         Dogs myDog = Dogs.shepherd;
15.         switch(myDog){
16.             case collie:
17.                 System.out.print("collie ");
18.             case default:
19.                 System.out.print("retriever "),
20.             case harrier:
21.                 System.out.print("harrier ");
22.         }
23.     }
24. }

What is the result?

A. harrier

B. shepherd

C. retriever

D. Compilation fails

E. retriever harrier

F. An exception is thrown at runtime,

答案:

題目範圍:基本觀念,switch

解析:

這題藏了三個錯誤,分別是:

Enum那行最後的逗號應該被刪掉

System.out.print("retriever "), 的逗號

還有case default: 應該改為default:

 

第122題:

Given:

11. public void genNumbers(){
12.     ArrayList numbers = new ArrayList();
13.     for(int i=0; i<10; i++){
14.         int value = i * ((int)Math.random());
15.         Integer intObj = new Integer(value);
16.         numbers.add(intObj);
17.     }
18.     System.out.println(numbers);
19. }

Which line of code marks the earliest point that an object referenced by intObj becomes a candidate for garbage collection?

A. Line 16

B. Line 17

C. Line 18

D. Line 19

E. The object is NOT a candidate for garbage collection.

答案:

題目範圍:物件與指標觀念,回收處理

解析:

如果沒有numbers.add(intObj);這個指令,intObj 會在17行每次回圈跑完時被回收掉

不過因為numbers.add(intObj);的關係,每次回圈產生的intObj 指標都會被放在numbers 中,直到19行才會被回收

 

第123題:

Given:

11. public static void parse(String str){
12.     try{
13.         float f = Float.parseFloat(str);
14.     }catch(NumberFormatException nfe){
15.         f = 0;
16.     }finally{
17.         System.out.println(f);
18.     }
19. }
20. public static void main(String[] args) {
21.     parse("invalid");
22. }

What is the result?

A. 0.0

B. Compilation fails.

C. A ParseException is thrown by the parse method at runtime.

D. A NumberFormatException is thrown by the parse method at runtime.

答案:

題目範圍:例外處理

解析:

出現了編譯錯誤,這是因為try/catch if for等等一樣,都是一個block,裡面宣告的區域變數外面都無法使用

這題如果把float 拿到try/catch 的外面宣告,並且賦予初始值,結果便會是0.0

 

第124題:

Given:

11. static void test() throws RuntimeException{
12.     try{
13.         System.out.print("test ");
14.         throw new RuntimeException();
15.     }
16.         catch(Exception ex){ System.out.print("exception ");}
17. }
18. public static void main(String[] args){
19.     try{test();}
20.     catch(RuntimeException ex){System.out.print("runtime ");}
21.     System.out.print("end ");
22. }

What is the result?

A. test end

B. Compilation fails.

C. test runtime end

D. test exception end

E. A Throwable is thrown by main at runtime.

答案:

題目範圍:例外處理

解析:

這是一個例外處理的演示,例外處理只會被"捕捉"一次,先抓先贏,最後一層的可以優先捕捉

RuntimeException是屬於Exception,因此捕捉成功,印出exception,而不印出runtime

 

第125題:

Given:

33. try{
34.    //some code here
35. }catch(NullPointerException el){
36.     System.out.print("a");
37. }catch(Exception e2){
38.     System.out.print("b");
39. }finally{
40.     System.out.print("c");
41. }

If some sort of exception is thrown at line 34, which output is possible?

A. a

B. b

C. c

D. ac

E. abc

答案:

題目範圍:例外處理

解析:

所謂排序會出現的例外,指的就是NullPointerException,可是個人覺得可能也會是ArrayIndexOutOfBoundsException…先不管

如果捕捉到了NullPointerException ,那麼下一個就不用看了,因為例外只能被捕捉一次

而不論是否有捕捉到例外,在try/catchblock結束後,就會執行finally的內容

arrow
arrow
    全站熱搜

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