ProgrammingVery hardIBM · Deloitte pattern

Which statement about this class is correct?

Which statement about this class is correct?
class Example<T> {
    void method(List<String> list) { System.out.println("string-list"); }
    void method(List<T> list) { System.out.println("t-list"); }
}
  1. A. It compiles fine; the two methods are distinguished by List<String> vs List<T>
  2. B. Compilation error: method(List<String>) and method(List<T>) erase to the same signature method(List)
  3. C. It compiles, but the second method is unreachable dead code
  4. D. It compiles only if T is bounded by T extends String

Correct answer: B. Compilation error: method(List<String>) and method(List<T>) erase to the same signature method(List)

After type erasure, both List<String> and List<T> become the raw type List, so the two method signatures erase to the identical method(List) — javac rejects this directly with a 'name clash' / 'erasure of method is the same' compile-time error, regardless of what T is later instantiated to.

More OOP with C++ and Java questions

Exams that ask this topic