Multiple choice technology architecture

import java.util.Iterator; import java.util.Set; import java.util.TreeSet; public class TreeTypeSet { public static void main(String[] args) { // TODO Auto-generated method stub 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()+" "); } } } Output is

  1. 1 2

  2. 1 2 3

  3. Compile error

  4. Runtime exception

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

TreeSet requires elements to be mutually Comparable or a Comparator to be provided. Adding String '2' and '1' works, but adding Integer 3 causes ClassCastException at runtime when TreeSet tries to compare Integer with String. The TreeSet uses natural ordering which fails for mixed types.