ProgrammingHardWipro · Cognizant pattern

What is printed?

What is printed?
List<String> strings = new ArrayList<>();
List<Integer> ints = new ArrayList<>();
System.out.println(strings.getClass() == ints.getClass());
  1. A. false
  2. B. Compilation error
  3. C. Throws a ClassCastException at runtime
  4. D. true

Correct answer: D. true

Type erasure means List<String> and List<Integer> both compile down to the same raw ArrayList class at runtime; there is no separate runtime class per generic instantiation, so the getClass() comparison is true.

More OOP with C++ and Java questions

Exams that ask this topic