Page: 3
12. Collections and Generics
Q: 09 Given: 11. public static Collection get() { 12. Collection sorted = new LinkedList(); 13. sorted.add("B"); sorted.add("C"); sorted.add("A"); 14. return sorted; 15. } 16. public static void main(String[] args) { 17. for (Object obj: get()) { 18. System.out.print(obj + ", "); 19. } 20. } What is the result? A. A, B, C, B. B, C, A, C. Compilation fails. D. The code runs with no output. E. An exception is thrown at runtime. Answer: B Q:10 given 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
4. Compilation succeeds Q: 11 Given: 11. public static Iterator reverse(List list) { 12. Collections.reverse(list); 13. return list.iterator(); 14. } 15. public static void main(String[] args) { 16. List list = new ArrayList(); 17. list.add("1"); list.add("2"); list.add("3"); 18. for (Object obj: reverse(list)) 19. System.out.print(obj + ", "); 20. } What is the result? A. 3, 2, 1, B. 1, 2, 3, C. Compilation fails. D. The code runs with no output. E. An exception is thrown at runtime. Answer: C Q: 12 Click the Task button. 
============does not compile================ 1.m1(listO ); 2.m2(listB); 3.m2(listO); =====compiles and runs with out error========= 1.m1(listA); 2.m1(listB); 3.m2(listA); Q: 13 Given: 1. import java.util.*; 2. public class PQ { 3. public static void main(String[] args) { 4. PriorityQueue<String> pq = new PriorityQueue<String>(); 5. pq.add("carrot"); 6. pq.add("apple"); 7. pq.add("banana"); 8. System.out.println(pq.poll() + ":" + pq.peek()); 9. } 10. } What is the result? A. apple:apple B. carrot:apple C. apple:banana D. banana:apple E. carrot:carrot F. carrot:banana Answer: C
Page: 3
1
2
3
4
5
6
7
8
9
10
11
12
|