【正文】
e 就好。 D 選項(xiàng): 判斷一個(gè) HashSet 中是否存在一個(gè) Person 對象的次數(shù)是常數(shù)次,和 map 的大小無關(guān) 。錯(cuò)誤,由于 Person 對象的 hashCode 返回的值都是 420,所以 HashSet 中的 Person 對象都在一個(gè) bucket 中,組成了一條 entry 鏈表,查詢速度與 entry 鏈表的大小息息相關(guān)。 A:選項(xiàng): 由 key 來查找 value 的次數(shù)與 map 的大小有關(guān) 。正確, map 越大,即 bucket 的個(gè)數(shù)越多, entry鏈的長度相應(yīng)的來說就越?。?hashcode 和桶個(gè)數(shù)取余后的數(shù)一樣的幾率就越?。? : 5. import .*。 6. public class SortOf { 7. public static void main(String[] args) { 8. ArrayListInteger a = new ArrayListInteger()。 9. (1)。 (5)。 (3)。 11. (a)。 12. (2)。 13. (a)。 14. (a)。 15. } 16. } What is the result? A. [1, 2, 3, 5] B. [2, 1, 3, 5] C. [2, 5, 3, 1] D. [5, 3, 2, 1] E. [1, 3, 5, 2] F. Compilation fails. G. An exception is thrown at runtime. Answer: C (a)。實(shí)現(xiàn)了排序,而 reverse 方法是反轉(zhuǎn)集合中的所有元素。 11. public interface Status { 12. /* insert code here */ int MY_VALUE = 10。 13. } Which three are valid on line 12? (Choose three. )A. final B. static C. native D. public E. private F. abstract G. protected Answer: A,B,D 接口中,變量默認(rèn)是 public static final,而方法默認(rèn)是 public,不能改變。 : 5. class Atom { 6. Atom() { (atom )。 } 7. } 8. class Rock extends Atom { 9. Rock(String type) { (type)。 } 10. } 11. public class Mountain extends Rock { 12. Mountain() { 13. super(granite )。 14. new Rock(granite )。 15. } 16. public static void main(String[] a) { new Mountain()。 } 17. } What is the result? A. Compilation fails. B. atom granite C. granite granite D. atom granite granite E. An exception is thrown at runtime. F. atom granite atom granite Answer: F 當(dāng)調(diào)用子類的構(gòu)造函數(shù)的時(shí)候,如果沒有顯式調(diào)用父類的構(gòu)造函數(shù),那么父類的默認(rèn)構(gòu)造函數(shù)將被調(diào)用,如果父類沒有默認(rèn)的構(gòu)造函數(shù),編譯器就會(huì)警告出錯(cuò)。 the Exhibit button. Which three statements are true? (Choose three.) A. Compilation fails. B. The code piles and the output is 2. C. If lines 16, 17 and 18 were removed, pilation would fail. D. If lines 24, 25 and 26 were removed, pilation would fail. E. If lines 16, 17 and 18 were removed, the code would pile and the output would be 2. F. If lines 24, 25 and 26 were removed, the code would pile and the output would be 1. Answer: B,E,F 注意這里有兩個(gè)內(nèi)部類, 一個(gè)是名為 A 的內(nèi)部類 A,另一個(gè)是名為 A 的局部內(nèi)部類 A,在testFoo 方法內(nèi)部,局部內(nèi)部類將屏蔽內(nèi)部類 A。 所以當(dāng)調(diào)用 Beta 類型對象的 testFoo 方法時(shí),將輸出第 28 行的內(nèi)容, new A()調(diào)用的是局部內(nèi)部類 A 的構(gòu)造函數(shù),返回一個(gè)局部內(nèi)部類 A 給 fubar()函數(shù),最終輸出 2。如果刪除 16, 17, 18 行,由于局部內(nèi)部類 A 在方法 testFoo()內(nèi)部屏蔽了外部的內(nèi)部類 A 可以看出不會(huì)對局部內(nèi)部產(chǎn)生任何影響。如果刪除24, 25, 26 行,那么本來被這個(gè)局部內(nèi)部類屏蔽的不可見的局部類 A 變得可見,輸出自然也是 1 了。 : 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 = () 。 B. p = () 。 C. Point p = (new Line()).getPoint() 。 D. p = (new Line()).getPoint() 。 Answer: D 在內(nèi)部類的外圍類外使用內(nèi)部類的時(shí)候,就像上面那個(gè) p = (new Line()).getPoint() 即 p = ()。//其中 method 方法返回 類型的對象 : 11. class Alpha { 12. public void foo() { (Afoo )。 } 13. } 14. public class Beta extends Alpha { 15. public void foo() { (Bfoo )。 } 16. public static void main(String[] args) { 17. Alpha a = new Beta()。 18. Beta b = (Beta)a。 19. ()。 20. ()。 21. } 22. } What is the result? A. Afoo Afoo B. Afoo Bfoo C. Bfoo Afoo D. Bfoo Bfoo E. Compilation fails. F. An exception is thrown at runtime. Answer: D 考察多態(tài)性,編譯器認(rèn)為 a 是 Alpha 類型, b 是 Beta 類型 ,但是虛擬機(jī)知道 a 和 b 的真實(shí)類型是 Beta,所以調(diào)用 Beta 類的函數(shù)。 the Exhibit button. Which statement is true about the classes and interfaces in the exhibit? A. Compilation will succeed for all classes and interfaces. B. Compilation of class C will fail because of an error in line 2. C. Compilation of class C will fail because of an error in line 6. D. Compilation of class AImpl will fail because of an error in line 2. Answer: C 考察多態(tài)性的動(dòng)態(tài)綁定,如果方法的簽名相同,則返回類型應(yīng)該 與超類的相同或是超類中返回類型的子類型 。上題中 C 類中有方法 Object execute() 以及 String execute(), Object不是 Strng 類型的子類型,所以錯(cuò)誤。 two code fragments correctly create and initialize a static array of int elements? (Choose two.) A. static final int[] a = { 100,200 }。 B. static final int[] a。 static { a=new int[2]。 a[0]=100。 a[1]=200。 } C. static final int[] a = new int[2]{ 100,200 } 。 D. static final int[] a。 static void init() { a = new int[3]。 a[0]=100。 a[1]=200。 } Answer: A,B 考察 static 修飾符。 A 選項(xiàng)是經(jīng)典定義。 B 是一個(gè)靜態(tài)初始化塊。類的初始化是按照這樣的順序進(jìn)行的:首先定義域 field,然后就會(huì)執(zhí)行靜態(tài)初始化塊,然后再執(zhí)行構(gòu)造函數(shù)。 C 錯(cuò)在既然你已經(jīng)用 { 100,200 }初始化了數(shù)組,就不能再定義維數(shù)了。 D 中 init()方法在初始化的時(shí)候根本就不會(huì)執(zhí)行,除非你在某個(gè)函數(shù)中顯式調(diào)用。 : 10. interface Foo { int bar()。 } 11. public class Sprite { 12. public int fubar( Foo foo ) { return ()。 } 13. public void testFoo() { 14. fubar( 15. // insert code here 16. )。 17. } 18. } Which code, inserted at line 15, allows the class Sprite to pile?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。 } Answer: C 匿名內(nèi)部類。 : 1. class Alligator { 2. public static void main(String[] args) { 3. int []x[] = {{1,2}, {3,4,5}, {6,7,8,9}}。 4. int [][]y = x。 5. (y[2][1])。 6. } 7. } What is the result?A. 2 B. 3 C. 4 D. 6 E. 7 F. Compilation fails. Answer: E 很簡單的數(shù)組題。 int []x[] 與 int [][]x 以及 int x[][]都是一樣的。 : 22. StringBuilder sb1 = new StringBuilder(123)。 23. String s1 = 123。 24. // insert code here 25. (sb1 + + s1) 。 Which code fragment, inserted at line 24, outputs 123abc 123abc ?A. (abc)。 (abc) 。 B. (abc)。 (abc) 。 C. (abc)。 (abc) 。 D. (abc)。 (abc) 。 E. (abc)。 s1 = (abc) 。 F. (abc)。 s1 = (abc) 。 G. (abc)。 s1 = s1 + (abc) 。 H. (abc)。 s1 = s1 + (abc) 。 Answer: E 考察 String 類和 StringBuilder 類。 String 類有一個(gè) concat 函數(shù), String str = (one)。用來把字符串添加到 s 的尾部,并把結(jié)果返回給 str,注意 String