ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
List<String> strings = new ArrayList<>();
List<Integer> ints = new ArrayList<>();
System.out.print(strings.getClass() == ints.getClass());
  1. A. Compilation error: cannot compare getClass() of different generic types
  2. B. false
  3. C. It depends on the JVM
  4. D. true

Correct answer: D. true

Generics are erased at compile time: at runtime, List<String> and List<Integer> are both represented by the exact same raw class, ArrayList. Their getClass() objects are identical, so == is true — the classic evidence of type erasure.

More OOP with C++ and Java questions

Exams that ask this topic