【正文】
QUESTION 1 a pregenerics implementation of a method: 11. public static int sum(List list) { 12. int sum = 0。 13. for ( Iterator iter = ()。 ()。 ) { 14. int i = ((Integer)()).intValue()。 15. sum += i。 16. } 17. return sum。 18. } What three changes allow the class to be used with generics and avoid an unchecked warning? (Choose three.) A. Remove line 14. B. Replace line 14 with int i = ()。. C. Replace line 13 with for (int i : intList) {. D. Replace line 13 with for (Iterator iter : intList) {. E. Replace the method declaration with sum(Listint intList). F. Replace the method declaration with sum(ListInteger intList). Answer: A,C,F 考察把原始類型轉(zhuǎn)換成泛型。則把 List 轉(zhuǎn)換成 ListInteger,由于類型參數(shù)不能為基本類型,所以 E 不正確,而第 13 行中 Iterator 轉(zhuǎn)換成 IteratorInteger, Integer 可以自動裝箱和拆箱,所以第 14 行不需要。 programmer has an algorithm that requires a that provides an efficient implementation of add(0, object), but does NOT need to support quick random access. What supports these requirements.? A. B. C. D. Answer: D 實現(xiàn)了 接口,并且需要高效率在首部進行插入操作,選項中僅有 LinkedList符合 : 11. // insert code here 12. private N min, max。 13. public N getMin() { return min。 } 14. public N getMax() { return max。 } 15. public void add(N added) { 16. if (min == null || () ()) 17. min = added。 18. if (max == null || () ()) 19. max = added。 20. } 21. } Which two, inserted at line 11, will allow the code to pile? (Choose two. )A. public class MinMax? { The safer , easier way to help you pass any IT exams. B. public class MinMax? extends Number { C. public class MinMaxN extends Object { D. public class MinMaxN extends Number { E. public class MinMax? extends Object { F. public class MinMaxN extends Integer { Answer: D,F 必須有 N 這個泛型,所以從 CDF 中選擇,而 N 需要實現(xiàn)了 doubleValue 方法, Object 肯定不行,而 Number 和 Integer 都實現(xiàn)了這個方法。 : 12. import .*。 13. public class Explorer2 { 14. public static void main(String[] args) { 15. TreeSetInteger s = new TreeSetInteger()。 16. TreeSetInteger subs = new TreeSetInteger()。 17. for(int i = 606。 i 613。 i++) 18. if(i%2 == 0) (i)。 19. subs = (TreeSet)(608, true, 611, true)。 20. (629)。 21. (s + + subs)。 22. } 23. } What is the result?A. Compilation fails. B. An exception is thrown at runtime. C. [608, 610, 612, 629] [608, 610] D. [608, 610, 612, 629] [608, 610, 629] E. [606, 608, 610, 612, 629] [608, 610] F. [606, 608, 610, 612, 629] [608, 610, 629] Answer: E 考察 TreeSet 以及它的 subSet 方法,兩個 true 表示包括 608 和 611。 TreeSet 是一個有序集,所以其中的元素必須是有序的。 public SortedSetE subSet(E fromElement, E toElement) Returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive. (If fromElement and toElement are equal, the returned set is empty.) The returned set is backed by this set, so changes in the returned set are reflected in this set, and viceversa. The returned set supports all optional set operations that this set supports. subSet 返回的是一個 view,而不是 copy,所以對 subSet 的改變會反映到原 set 里面,反之,在原 set 中的變化也會反映到 subSet 里面。 看下面的例子,首先移除一個 15,發(fā)現(xiàn)原始的 treeSet和 subset里面的 15 都沒了,而 treeSet加上 15 之后,原始的 treeSet 和 subset 里面的 15 又都出現(xiàn)了。 [java] view plain copy 1. package 。 2. import 。 3. 4. public class Explorer1 { 5. public static void main(String[] args) { 6. TreeSetInteger treeSet = new TreeSetInteger()。 7. TreeSetInteger subSet = new TreeSetInteger()。 8. for(int i = 10。 i = 20。 i++) 9. (i)。 10. subSet = (TreeSetInteger) (13, true, 16, true)。 11. (subSet)。//[13, 14, 15, 16] 12. (15)。 13. (subSet)。//[13, 14, 16] 14. (treeSet)。//[10, 11, 12, 13, 14, 16, 17, 18, 19, 20] 15. (15)。 16. (subSet)。//[13, 14, 15, 16] 17. (treeSet)。//[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] 18. } 19. } : 1. public class Score implements ComparableScore { 2. private int wins, losses。 3. public Score(int w, int l) { wins = w。 losses = l。 } 4. public int getWins() { return wins。 } 5. public int getLosses() { return losses。 } 6. public String toString() { 7. return + wins + , + losses + 。 8. } 9. // insert code here 10. } Which method will plete this class? A. public int pareTo(Object o){/*more code here*/ } B. public int pareTo(Score other){/*more code here*/ } C. public int pare(Score s1,Score s2){/*more code here*/ } D. public int pare(Object o1,Object o2){/*more code here*/ } Answer: B 實現(xiàn) ComparableScore 接口必須實現(xiàn) parToScore 方法!注意與 Compartor 接口區(qū)分開來, Compartor 接口要求實現(xiàn) Compare 方法。 11. public class Person { 12. private name。 13. public Person(String name) { 14. = name。 15. } 16. public int hashCode() { 17. return 420。 18. } 19. } Which statement is true? A. The time to find the value from HashMap with a Person key depends on the size of the map. B. Deleting a Person key from a HashMap will delete all map entries for all keys of type Person. C. Inserting a second Person object into a HashSet will cause the first Person object to be removed as a duplicate. D. The time to determine whether a Person object is contained in a HashSet is constant and does NOT depend on the size of the map. Answer: A B 選項: 刪除 HashMap 中一個 Person 對象對應的鍵將會刪除這個散列映射表中 Person 類的全部條目 。錯誤, HashMap 中 Person 對象的鍵值不是由 Person 對象決定的,而是程序員給定的鍵,例如(123345, bob),就是把鍵為 123456的 bob對象添加到名為 staff的 HashMap中,因而 HashMap允許添加相同的對象。所以說,刪除一個鍵對應的 Person 對象并不會刪除所有的條目,他們的 key 都不同嘛。 C 選項: 向 HashSet 中插入另外一個 Person 對象將會引起第二個對象覆蓋第一個對象 。錯誤,雖然 Person對象的 hashCode 方法返回的值都是 420,這僅 僅表明兩個 Person 對象在一個 entry 鏈表中,接下來要調(diào)用 equals 方法,由于 Person 類沒有 equals 方法,所以調(diào)用 Object 的 equals 方法返回對象的存儲地址,很明顯兩個 Person 對象的存儲地址是不同的。綜上, HashSet 中可以添加不同的 Person 對象,只要 equals方法返回值為 fals