ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
// in main:
List<String> ls = new ArrayList<>();
List<Integer> li = new ArrayList<>();
System.out.println(ls.getClass() == li.getClass());
  1. A. false
  2. B. true
  3. C. Compilation error: incompatible generic types
  4. D. It depends on the JVM

Correct answer: B. true

Due to type erasure, both List<String> and List<Integer> compile down to the same raw ArrayList class at run time — the generic type parameter exists only at compile time for static checking and is gone from the bytecode, so getClass() returns the identical Class object for both: true.

More OOP with C++ and Java questions

Exams that ask this topic