Java OCA OCP Practice Question 2768

Question

Given:

2. import java.util.*;  
3. public class Main {  
4.   public static void main(String[] args) {  
5.     Set<Integer> s = new TreeSet<Integer>();  
6.     s.add(23); s.add(42); s.add(new Integer(5));     
7.     Iterator i = s.iterator();  
8.     // while(System.out.print(i.next())) { }  
9.     // for(Integer i2: i) System.out.print(i2);  
10.     // for(Integer i3: s) System.out.print(i3);   
11.     // while(i.hasNext()) System.out.print(i.get());   
12.     // while(i.hasNext()) System.out.print(i.next());  
13. } } /*from   w  w  w  .  ja  va2  s .  co  m*/

If lines 8-12 are uncommented, independently, which are true? (Choose all that apply.)

  • A. Line 8 will compile.
  • B. Line 9 will compile.
  • C. Line 10 will compile.
  • D. Line 11 will compile.
  • E. Line 12 will compile.
  • F. Of those that compile, the output will be 23425
  • G. Of those that compile, the output will be 52342


C, E, and G are correct.

Note

Line 10 is a legal for-each loop.

Line 12 uses legal syntax to iterate over the Set.

Because this is a TreeSet, the output will be sorted.




PreviousNext

Related