Multiple choice technology programming languages

  1. import java.util.*; 4. class Dog { int size; Dog(int s) { size = s; } } 5. public class FirstGrade { 6. public static void main(String[] args) { 7. TreeSet i = new TreeSet(); 8. TreeSet d = new TreeSet(); 9. 10. d.add(new Dog(1)); d.add(new Dog(2)); d.add(new Dog(1)); 11. i.add(1); i.add(2); i.add(1); 12. System.out.println(d.size() + " " + i.size()); 13. } 14. }

  1. 1 2

  2. 2 2

  3. 2 3

  4. Compilation fails

  5. Exception occurs at runtime

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

TreeSet fails because the Dog class doesn't implement Comparable interface and no Comparator is provided. When TreeSet tries to insert Dog objects, it needs to compare them for ordering, which requires either Comparable implementation or a custom Comparator. Since Dog doesn't implement Comparable, it throws ClassCastException at runtime when attempting to cast Dog to Comparable for comparison.