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) { }
}- A. It compiles; the call is chosen by the element type
- B. Compilation error: both methods have the same erasure
- C. It compiles with an unchecked warning only
- 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
- How many of the following statements about equals() and hashCode() in Java are correct? (i) If a.equals(b) is true, a.hashCode() must…
- On a typical 64-bit compiler (8-byte pointers, 4-byte int, natural alignment), what does sizeof(P) print? cpp class P { int id; public…
- What is printed? java class P { static { System.out.print("1"); } { System.out.print("2"); } P() { System.out.print("3"); } } class C…
- What is printed? java class P { P() { show(); } void show() { } } class C extends P { final int a = 5; int b = 7; final String s = "s"…
- What is printed? java // static methods of the main class: static int f() { int x = 1; try { x = 2; throw new IllegalStateException(); }…
- What is printed? java Long l = 127L; Integer i = 127; System.out.print(l.equals(127) + " "); System.out.print(l.equals(127L) + " ")…