ProgrammingHardTCS NQT Prime · Infosys SP
What happens?
What happens?
static void f(String s) { System.out.print("S"); }
static void f(Integer i) { System.out.print("I"); }
// main:
f(null);- A. Compile error: ambiguous method call
- B. Prints S
- C. Prints I
- D. Throws NullPointerException
Correct answer: A. Compile error: ambiguous method call
null fits both String and Integer, neither is a subtype of the other, so no most specific method exists and compilation fails.
More OOP with C++ and Java questions
- What is printed? java System.out.print('a' + 'b' + "c" + 'a' + 'b');
- What is printed? java class K { int v; K(int v) { this.v = v; } public boolean equals(K o) { return o != null && v == o.v; } public int…
- What does new D().w() return? java interface A { default String w() { return "A"; } } interface B extends A { default String w() { return…
- What is printed? java class A { int a = f("a"); { f("i"); } A() { f("c"); } int f(String s) { System.out.print(s); return 1; } int b =…
- What is printed? java class A { static int x = init("Ax"); static int init(String s) { System.out.print(s); return 1; } } class B extends…
- What is printed? java List<Integer> l = new ArrayList<>(List.of(10, 20, 30, 1)); l.remove(1); l.remove(Integer.valueOf(1))…