第101題: |
Given: package com.sun.scjp; |
Which two correctly access the DIAMETER member of the Geodetics class? (Choose two.) A. B. C. D. |
答案:AC |
題目範圍:import 匯入、import 靜態成員 |
解析: A是一般的匯入做法 B和C選項只差了一個".*"而已,因為匯入靜態成員時,必須指定靜態成員或是全部靜態成員因此必須寫 import static com.sun.scjp.Geodetics.*; 或者 import static com.sun.scjp.Geodetics.DIAMETER; |
第102題: |
Given: 10. interface Foo{int bar();} |
Which code, inserted at line 15, allows the class Sprite to compile? A. Foo{public int bar(){return 1;}} B. new Foo{public int bar(){return 1;}} C. new Foo(){public int bar(){return 1;}} D. new class Foo{public int bar(){return 1;}} |
答案:C |
題目範圍:innerClass |
解析: 在testFoo方法中呼叫了fubar方法 fubar方法必須傳入一個Foo物件 C選項在生成物件同時做了一個innerClass |
第103題: |
Given: 11. public enum Title{ 12. MR("Mr."), MRS("Mrs."), MS("Ms."); 13. private final String title; 14. private Title(String t){title = t;} 15. public String format(String last, String first){ 16. return title + " " + first + " " + last; 17. } 18. } 19. public static void main(String[] args){ 20. System.out.println(Title.MR.format("Doe", "John")); 21. } |
What is the result? A. Mr. John Doe B. An exception is thrown at runtime. C. Compilation fails because of an error in line 12. D. Compilation fails because of an error in line 15. E. Compilation fails because of an error in line 20. |
答案:A |
題目範圍:enum 列舉 |
解析: 如果把列舉看成是一個傳入參數被限制的類別,就會清楚很多 例如這裡的Title列舉,傳入的參數被限制在這三種 "MR", "MRS"和 "MS" "MR"會變成"Mr."傳入建構子,"MRS"會變成"Mrs.","MS"會變成"Ms." 如果Title是個類別,要這樣寫 new Title("Mr.").format("Doe", "John"); Title是列舉,就這樣寫 Title.MR.format("Doe", "John")
有些人或許對final的title有些疑問:為什麼不用初始化,而且還可以賦予值? 其實final的變數可以不用初始化,不過賦予值只限一次 |
第104題: |
Given: 10. class Line{ |
Which code, inserted at line 15, creates an instance of the Point class defined in Line? A. Point p = new Point(); B. Line.Point p = new Line.Point(); C. The Point class cannot be instatiated at line 15. D. Line l = new Line(); l.Point p = new l.Point(); |
答案:B |
題目範圍:innerClass |
解析: 從外部實體化innerClass的方法 這裡的Point是一個靜態類別,因此只要寫Line.Point p = new Line.Point();就可以 如果Point不是靜態的,那麼就要像下面這樣寫: Line l = new Line(); Line.Point p = l.new Point(); 如果要寫成一行的話,這樣也可以: Line.Point p = new Line().new Point(); |
第105題: |
Given: 1. public class A { And given this code from Class B: 25. A a1 = new A(); |
What is the result? A. Compilation of class A fails. B. Line 28 prints the value 3 to System.out. C. Line 28 prints the value 1 to System.out. D. A runtime error occurs when line 25 executes. E. Compilation fails because of an error on line 28. |
答案:A |
題目範圍:靜態元素 |
解析: 如果你能夠照著題目來思考執行的過程,那麼大概就能夠了解為什麼靜態方法中必須全都是靜態元素了 這裡的getInstanceCount方法是靜態的,但是裡面的counter不是靜態的 這樣運行起來會有很多矛盾,因此是不被允許的 |
留言列表