Java OCA OCP Practice Question 2307

Question

What will be the result of attempting to compile and run the following code?

public class Main {
  public static void main(String[] args) {
    Set set = new TreeSet<String>();
    set.add("one");
    set.add(2);
    set.add("three");
    System.out.println(set);
  }
}

Select the one correct answer.

  • (a) The program does not compile.
  • (b) The program compiles with unchecked warnings and prints the elements in the set.
  • (c) The program compiles without unchecked warnings and prints the elements in the set.
  • (d) The program compiles with unchecked warnings and throws an exception at runtime.
  • (e) The program compiles without unchecked warnings and throws an exception at runtime.


(d)

Note

The compiler issues unchecked warnings for calls to the add() method.

The TreeSet class orders elements according to their natural ordering.

A ClassCastException is thrown at runtime, as Strings are not comparable to Integers.




PreviousNext

Related