ProgrammingHardProduct-company interview pattern

What happens when this Java class is compiled?

What happens when this Java class is compiled?
class Util {
    static void f(List<String> l)  { }
    static void f(List<Integer> l) { }
}
  1. A. It compiles; the call is chosen by the element type
  2. B. Compilation error: both methods have the same erasure
  3. C. It compiles with an unchecked warning only
  4. D. It compiles, but calling f throws an exception at run time

Correct answer: B. Compilation error: both methods have the same erasure

Generic type arguments are erased at compile time, so both methods become f(List) in the bytecode. Two methods with the same erased signature clash, and the compiler rejects the class.

More OOP with C++ and Java questions

Exams that ask this topic