Page: 8
12. Collections and Generics
Q: 34 A programmer must create a generic class MinMax and the type parameter of MinMax must implement Comparable. Which implementation of MinMax will compile? A. class MinMax<E extends Comparable<E>> { E min = null; E max = null; public MinMax() {} public void put(E value) { /* store min or max */ } B. class MinMax<E implements Comparable<E>> { E min = null; E max = null; public MinMax() {} public void put(E value) { /* store min or max */ } C. class MinMax<E extends Comparable<E>> { <E> E min = null; <E> E max = null; public MinMax() {} public <E> void put(E value) { /* store min or max */ } D. class MinMax<E implements Comparable<E>> { <E> E min = null; <E> E max = null; public MinMax() {} public <E> void put(E value) { /* store min or max */ } Answer: A Q: 35 Given: int[] myArray = new int[] {1, 2, 3, 4, 5}; What allows you to create a list from this array? A. List myList = myArray.asList(); B. List myList = Arrays.asList(myArray); C. List myList = new ArrayList(myArray); D. List myList = Collections.fromArray(myArray); Answer: B Question: 36 Given: 1. public class Score implements Comparable<Score> { 2. private int wins, losses; 3. public Score(int w, int 1) { wins = w; losses = 1; } 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 complete this class? A. public int compareTo(Object o) {/*mode code here*/} B. public int compareTo(Score other) {/*more code here*/} C. public int compare(Score s1,Score s2){/*more code here*/} D. public int compare(Object o1,Object o2){/*more code here*/} Answer: B Question: 37 Click the Exhibit button. 1. import java.util.*; 2. class KeyMaster { 3. public int i; 4. public KeyMaster(int i) { this.i = i; } 5. public boolean equals(Object o) { return i == ((KeyMaster)o).i; } 6. public int hashCode() { return i; } 7. } 8. public class MapIt { 9. public static void main(String[] args) { 10. Set<KeyMaster> set = new HashSet<KeyMaster>(); 11. KeyMaster k1 = new KeyMaster(1); 12. KeyMaster k2 = new KeyMaster(2); 13. set.add(k1); set.add(k1); 14. set.add(k2); set.add(k2); 15. System.out.print(set.size() + “:”); 16. k2.i = 1; 17. System.out.print(set.size() + “:”); 18. set.remove(k1); 19. System.out.print(set.size() + “:”); 20. set.remove(k2); 21. System.out.print(set.size()); 22. } 23. } What is the result? A. 4:4:2:2 B. 4:4:3:2 C. 2:2:1:0 D. 2:2:0:0 E. 2:1:0:0 F. 2:2:1:1 G. 4:3:2:1 Answer: F Question: 38 Given: 1. import java.util.*; 2. public class Test { 3. public static void main(String[] args) { 4. List<String> strings = new ArrayList<String>(); 5. // insert code here 6. } 7. } Which four, inserted at line 5, will allow compilation to succeed? (Choose four.) A. String s = strings.get(0); B. Iterator i1 = strings.iterator(); C. String[] array1 = strings.toArray(); D. Iterator<String> i2 = strings.iterator(); E. String[] array2 = strings.toArray(new String[1]); F. Iterator<String> i3 = strings.iterator<String>(); Answer: ABDE Question: 39 Given: classA {} class B extends A {} class C extends A {} class D extends B {} Which three statements are true? (Choose three.) A. The type List<A> is assignable to List. B. The type List<B> is assignable to List<A>. C. The type List<Object> is assignable to List<?>. D. The type List<D> is assignable to List<? extends B>. E. The type List<? extends A> is assignable to List<A>. F. The type List<Object> is assignable to any List reference. G. The type List<? extends B> is assignable to List<? extends A>. Answer: CDG
Page: 8
1
2
3
4
5
6
7
8
9
10
11
12
|