Page: 6
12. Collections and Generics
Q: 24 Given: 10. interface A { void x(); } 11. class B implements A { public void x() {} public void y() {} } 12. class C extends B { public void x() {} } And: 20. java.util.List<A> list = new java.util.ArrayList<A>(); 21. list.add(new B()); 22. list.add(new C()); 23. for (A a : list) { 24. a.x(); 25. a.y(); 26. } What is the result? A. The code runs with no output. B. An exception is thrown at runtime. C. Compilation fails because of an error in line 20. D. Compilation fails because of an error in line 21. E. Compilation fails because of an error in line 23. F. Compilation fails because of an error in line 25. Answer: F Q: 25 Click the Task button. 
Solution: - Compilation of the first statement succeeds ,but compilation fails due to an error in the second statement.
- Compilation fails due to an error in the first statement
- Compilation succeeds
- Compilation succeeds
Q: 26 Click the Task button. 
Solution: 1. list.add("foo"); ----------- Compilation fails 2. list = new ArrayList<String>(); ------Compilation succeeds 3.list=new ArrayList<Object>( ); ---- Compilation fails 4. String s = list.get(0); ------ Compilation succeeds 5. Object o = list; ----- Compilation succeeds Q: 27 Given: 1. public class Drink implements Comparable { 2. public String name; 3. public int compareTo(Object o) { 4. return 0; 5. } 6. } and: 20. Drink one = new Drink(); 21. Drink two = new Drink(); 22. one.name= "Coffee"; 23. two.name= "Tea"; 23. TreeSet set = new TreeSet(); 24. set.add(one); 25. set.add(two); A programmer iterates over the TreeSet and prints the name of each Drink object. What is the result? A. Tea B. Coffee C. Coffee Tea D. Compilation fails. E. The code runs with no output. F. An exception is thrown at runtime. Answer: B Q:28 Click the Task button. 
Solutions: compilation fails: Public void takeList(ArrayList<Animal> list) { } Public void takeList(ArrayList<Object> list) { } compilation Succeeds All the remaining
Page: 6
1
2
3
4
5
6
7
8
9
10
11
12
|