ProgrammingVery hardWipro Elite · TCS NQT Prime
What does new D().w() return?
What does new D().w() return?
interface A { default String w() { return "A"; } }
interface B extends A { default String w() { return "B"; } }
interface C extends A { }
class D implements B, C { }- A. "A"
- B. Compile error: conflicting defaults
- C. Runtime AbstractMethodError
- D. "B"
Correct answer: D. "B"
B overrides A.w, and C merely inherits A.w, so there is no conflict: the more specific B.w wins.
More OOP with C++ and Java questions
- 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))…
- What is printed? java Object[] o = new String[1]; try { o[0] = 1; System.out.print("T"); } catch (ArrayStoreException e) {…
- What is printed? java int i = 1; i = i++ + ++i; int j = 5; j += j++ 2; System.out.print(i + " " + j);
- What is printed? java short s = 1; s = (short) (s + 1); char c = 'A'; c += 1; c++; System.out.print(s + " " + c + " " + (c + 1));