第111題:

Given:

15. public class Pass2{
16.     public void static main(String[] args){
17.         int x = 6;
18.         Pass2 p = new Pass2();
19.         p.doStuff(x);
20.         System.out.print(" main x = " + x);
21.     }
22.
23.     void doStuff(int x){
24.         System.out.print(" doStuff x = " + x++);
25.     }
26. }

And the command-line invocations:

javac Pass2.java

java Pass2 5

What is the result?

A. Compilation fails.

B. An exception is thrown at runtime.

C. doStuff x = 6 main x = 6

D. doStuff x = 6 main x = 7

E. doStuff x = 7 main x = 6

F. doStuff x = 7 main x = 7

答案:

題目範圍:基本觀念、參數傳遞

解析:

X是整數,基本資料型態,是值

因此在做參數傳遞時,方法內的x與之前的x是獨立的兩個值

 

第112題:

Given:

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

What is the result?

A. collie

B. harrier

C. Compilation fails.

D. collie harrier

E. An exception is thrown at runtime.

答案:

題目範圍:eurm列舉、switch的使用

解析:

列舉可以搭配switch來使用

這一題中,case結束時並沒有用break來結尾,因此collie會接下去執行harrier的部分

(Collie:牧羊犬;harrier:海鷂,一種獵鷹)

 

第113題:

Given:

10. public class Foo{
11.     static int[] a;
12.     static{ a[0] = 2;}
13.     public static void main( String[] args){}
14. }

Which exception or error will be thrown when a programmer attempts to run this code?

A. java.lang.StackOverflowError

B. java.lang.IllegalStateException

C. java.lang.ExceptionInInitializerError

D. java.lang.ArrayIndexOutOfBoundsException

答案:

題目範圍:基本觀念

解析:

這題重點是初始化,和static沒有甚麼關係,你可以試著把兩行static拿掉,並且在mainnew一個Foo,結果會是一樣的

跑出的例外是ExceptionInInitializerErrorNullPointerException,而不是ArrayIndexOutOfBoundsException

了解指標的話就可以知道,a只是一個陣列的指標,並且指向null(沒有指向任何東西)

因此在執行static{ a[0] = 2;}就會出現NullPointerException

要讓程式正確執行,可以修改為static int a[] = new int[1];

如果這時候執行的下一行改為static{ a[1] = 2;}會出現ArrayIndexOutOfBoundsException

 

第114題:

Given:

10. class Line{
11.     public class Point{public int x, y;}
12.     public Point getPoint(){return new Point();}
13. }
14. class Triangle{
15.     public Triangle(){
16.         //insert code here
17.     }

18. }

Which code, inserted at line 16, correctly retrieves a local instance of a Point object?

A. Point p = Line.getPoint();

B. Line.Point p = Line.getPoint();

C. Point p = (new Line()).getPoint();

D. Line.Point p = (new Line()).getPoint();

答案:

題目範圍:innerClass

解析:

Line.Point p = new Line().new Point();

結果相同

順便複習,如果Pointstatic的話,就這樣

Line.Point p = new Line.Point();

 

第115題:

Given:

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

12.     for(int i = 0; i <= 10; i++){

13.         if(i > 6) break;

14.     }

15.     System.out.println(i);

16. }

What is the result?

A. 6

B. 7

C. 10

D. 11

E. Compilation fails.

F. An exception is thrown at runtime.

答案:

題目範圍:基本觀念

解析:

請注意i 是在for迴圈中宣告的,在迴圈外不可以使用

arrow
arrow
    全站熱搜

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