Page: 7
12. Collections and Generics
Q: 29 Click the Task button.
 Solution: import java.util.*; public class TestGenericConversion { public static void main(String s[ ]){ List<String> list=new LinkedList<String>( ); list.add("one"); list.add("two"); System.out.println(list.get(0).length(); } } } Q: 30 Given: 10. abstract public class Employee { 11. protected abstract double getSalesAmount(); 12. public double getCommision() { 13. return getSalesAmount() * 0.15; 14. } 15. } 16. class Sales extends Employee { 17. // insert method here 18. } Which two methods, inserted independently at line 17, correctly complete the Sales class? (Choose two.) A. double getSalesAmount() { return 1230.45; } B. public double getSalesAmount() { return 1230.45; } C. private double getSalesAmount() { return 1230.45; } D. protected double getSalesAmount() { return 1230.45; } Answer: B, D Q: 31 Given: 13. public static void search(List<String> list) { 14. list.clear(); 15. list.add("b"); 16. list.add("a"); 17. list.add("c"); 18. System.out.println(Collections.binarySearch(list, "a")); 19. } What is the result of calling search with a valid List implementation? A. 0 B. 1 C. 2 D. a E. b F. c G. The result is undefined. Answer: G Q: 32 Given: 11. public static void append(List list) { list.add("0042"); } 12. public static void main(String[] args) { 13. List<Integer> intList = new ArrayList<Integer>(); 14. append(intList); 15. System.out.println(intList.get(0)); 16. } What is the result? A. 42 B. 0042 C. An exception is thrown at runtime. D. Compilation fails because of an error in line 13. E. Compilation fails because of an error in line 14. Answer: B Q: 33 Given: 11. public class Person { 12. private name; 13. public Person(String name) { 14. this.name = 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
Page: 7
1
2
3
4
5
6
7
8
9
10
11
12
|