Multiple choice technology programming languages

public static void before() { Set set = new TreeSet(); set.add("2"); set.add(3); set.add("1"); Iterator it = set.iterator(); while (it.hasNext()) System.out.print(it.next() + " "); }

  1. The before() method will print 1 2

  2. The before() method will print 1 2 3

  3. The before() method will print three numbers, but the order cannot be determined

  4. The before() method will not compile

  5. The before() method will throw an exception at runtime

Reveal answer Fill a bubble to check yourself
E Correct answer
Explanation

TreeSet requires its elements to be mutually Comparable. The code adds String "2", Integer 3, and String "1" to a TreeSet without a Comparator. When TreeSet tries to sort these mixed types, it attempts to compare String with Integer using natural ordering, which throws ClassCastException at runtime because String and Integer are not mutually comparable.