ProgrammingVery hardAmazon/Microsoft SDE MCQ · Java placement

Why does this class fail to compile?

Why does this class fail to compile?
class Util {
    static void f(List<String> l)  { }
    static void f(List<Integer> l) { }
}
  1. A. Overloads may not differ only in a type argument, since both erase to f(List)
  2. B. It compiles; the compiler picks the overload from the type argument
  3. C. A static method may not take a generic parameter
  4. D. List<Integer> is not allowed because int is primitive

Correct answer: A. Overloads may not differ only in a type argument, since both erase to f(List)

Type arguments are erased, so both methods have the same erasure f(List) and would clash in the class file; the compiler reports a name clash. Generic parameters are otherwise allowed in static methods.

More OOP with C++ and Java questions

Exams that ask this topic