第111題: |
Given: 15. public class Pass2{ 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 |
答案:C |
題目範圍:基本觀念、參數傳遞 |
解析: X是整數,基本資料型態,是值 因此在做參數傳遞時,方法內的x與之前的x是獨立的兩個值 |
第112題: |
Given: 12. public class Test{ |
What is the result? A. collie B. harrier C. Compilation fails. D. collie harrier E. An exception is thrown at runtime. |
答案:D |
題目範圍:eurm列舉、switch的使用 |
解析: 列舉可以搭配switch來使用 這一題中,case結束時並沒有用break來結尾,因此collie會接下去執行harrier的部分 (Collie:牧羊犬;harrier:海鷂,一種獵鷹) |
第113題: |
Given: 10. public class Foo{ |
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 |
答案:C |
題目範圍:基本觀念 |
解析: 這題重點是初始化,和static沒有甚麼關係,你可以試著把兩行static拿掉,並且在main裡new一個Foo,結果會是一樣的 跑出的例外是ExceptionInInitializerError的NullPointerException,而不是ArrayIndexOutOfBoundsException 了解指標的話就可以知道,a只是一個陣列的指標,並且指向null(沒有指向任何東西) 因此在執行static{ a[0] = 2;}時就會出現NullPointerException了 要讓程式正確執行,可以修改為static int a[] = new int[1]; 如果這時候執行的下一行改為static{ a[1] = 2;}才會出現ArrayIndexOutOfBoundsException |
第114題: |
Given: 10. class Line{ |
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(); |
答案:D |
題目範圍:innerClass |
解析: Line.Point p = new Line().new Point(); 結果相同 順便複習,如果Point是static的話,就這樣 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. |
答案:E |
題目範圍:基本觀念 |
解析: 請注意i 是在for迴圈中宣告的,在迴圈外不可以使用 |
留言列表