Java OCA OCP Practice Question 2963

Question

Given:

2. import java.util.*;  
3. public class Main {  
4.   public static void main(String[] args) {      
5.     Set s1 = new HashSet();  
6.     s1.add(0);  /*from   w w w  . ja  v a2s .com*/
7.     s1.add("1");  
8.     doShape(s1);  
9.   }   
10.   static void doShape(Set<Number> s) {  
11.     do2(s);  
12.     Iterator i = s.iterator();  
13.     while(i.hasNext())  System.out.print(i.next() + " ");  
14.     Object[] oa = s.toArray();  
15.     for(int x = 0; x < oa.length; x++)  
16.       System.out.print(oa[x] + " ");  
17.     System.out.println(s.contains(1));  
18.   }  
19.   static void do2(Set s2) { System.out.print(s2.size() + " "); }      
20. } 

What is the most likely result?

  • A. 2 0 1 0 1 true
  • B. 2 0 1 0 1 false
  • C. Compilation fails.
  • D. An exception is thrown at line 8.
  • E. An exception is thrown at line 13.
  • F. An exception is thrown at line 14.
  • G. An exception is thrown at line 19.


B is correct.

Note

Set s1 (which contains a String and an Integer) can be iterated over, copied to an Object[], sized, and searched.

A is incorrect because s1 doesn't contain an Integer with a value of 1.

C, D, E, F, and G are incorrect because the code is all legal and runs without exception.




PreviousNext

Related