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"); }
}- A. It compiles fine; the two methods are distinguished by List<String> vs List<T>
- B. Compilation error: method(List<String>) and method(List<T>) erase to the same signature method(List)
- C. It compiles, but the second method is unreachable dead code
- 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
- What is printed? java class Util { static <T> T firstNonNull(T a, T b) { return a != null ? a : b; } } // in main: String s =…
- What is printed? java class Container<T extends Comparable<T>> { T max(T a, T b) { return a.compareTo(b) >= 0 ? a : b; } } // in main…
- What is printed? java class Node<T> { T data; static int count; { count++; } Node(T d) { data = d; } } // in main: new Node<Integer>(1)…
- What is printed? java class Base { protected void show() { System.out.println("Base.show"); } } class Derived extends Base { @Override…
- What is printed? java class Calculator { int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } int…
- What is printed? java class Shape { void draw() { System.out.println("Shape.draw"); } } class Circle extends Shape { void draw() {…