Java OCA OCP Practice Question 2683

Question

Which of the following statements are true? (Select two.)

3:    Set<Number> numbers = new HashSet<>(); 
4:    numbers.add(new Integer(86)); 
5:    numbers.add(75); 
6:    numbers.add(new Integer(86)); 
7:    numbers.add(null); 
8:    numbers.add(309L); 
9:    Iterator iter = numbers.iterator(); 
10:   while (iter.hasNext()) 
11:      System.out.print(iter.next()); 
  • A. The code compiles successfully.
  • B. The output is 8675null309.
  • C. The output is 867586null309.
  • D. The output is indeterminate.
  • E. There is a compiler error on line 3.
  • F. There is a compiler error on line 9.
  • G. An exception is thrown.


A, D.

Note

The code compiles fine.

It uses the diamond operator, and it allows any implementation of Number to be added.

HashSet does not guarantee any iteration order, making A and D correct.




PreviousNext

Related