Java OCA OCP Practice Question 1208

Question

Given:

 3. import java.util.*;
 4. public class Mixup {
 5.   public static void main(String[] args) {
 6.     Object o = new Object();
 7.     // insert code here
 8.     s.add("o");
 9.     s.add(o);
10.   }
11. }

And these three fragments:

I.   Set s = new HashSet();
II.  TreeSet s = new TreeSet();
III. LinkedHashSet s = new LinkedHashSet();

When fragments I, II, or III are inserted independently at line 7, which are true?

Choose all that apply.

  • A. Fragment I compiles
  • B. Fragment II compiles
  • C. Fragment III compiles
  • D. Fragment I executes without exception
  • E. Fragment II executes without exception
  • F. Fragment III executes without exception


A, B, C, D, and F are all correct.

Note

Only E is incorrect. Elements of a TreeSet must in some way implement Comparable.




PreviousNext

Related