Multiple choice technology programming languages

1.Set s = new HashSet(); 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
B Correct answer
Explanation

HashSet uses hashing, not natural ordering. It doesn't require elements to be comparable. Adding a String and then an Integer works fine - they coexist in the same HashSet. No ClassCastException occurs. The exception only happens with TreeSet (as in block 129672) because TreeSet attempts to compare elements.

AI explanation

A HashSet stores elements based on hash code and equality, with no requirement that elements be mutually comparable or even of the same type. Adding a String and then an Integer to a HashSet works fine because HashSet never tries to order or compare the elements against each other, so no ClassCastException occurs.