Multiple choice technology programming languages

1.Set s = new TreeSet(); 2.s.add("JAVA"); 3.s.add(new Integer(5)); Line 3 will give ClassCast Exception because Two different types of object are getting added.

  1. True

  2. False

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

This is a duplicate of blocks 129672 and 129674. TreeSet requires elements to implement Comparable for natural ordering. Adding String then Integer fails with ClassCastException because the Integer cannot be compared with the existing String when TreeSet tries to position it.

AI explanation

A TreeSet keeps its elements in sorted order and relies on compareTo (natural ordering) or a supplied Comparator to place new elements. When it tries to compare the newly added Integer against the existing String already in the set, there is no defined ordering between unrelated types, so the JVM throws a ClassCastException at runtime.