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

Unlike TreeSet, a HashSet does not sort its elements and does not compare them using Comparable. It uses hashCode() and equals(), which are defined on all objects. Therefore, adding a String and an Integer to a HashSet will not cause a ClassCastException.