ProgrammingHardProduct-company placement · Java
What happens when Z is compiled?
What happens when Z is compiled?
interface X { int V = 1; }
interface Y { int V = 2; }
class Z implements X, Y {
int get() { return V; }
}- A. get() returns 1
- B. get() returns 2
- C. Compile-time error: a class cannot implement two interfaces with the same field
- D. Compile-time error: the reference to V is ambiguous
Correct answer: D. Compile-time error: the reference to V is ambiguous
Inheriting two constants with the same name is allowed, but a simple-name use of V is ambiguous; writing X.V or Y.V fixes it.
More OOP with C++ and Java questions
- What happens when this is compiled? java interface A { default void hello() { System.out.print("A"); } } interface B extends A { void…
- How many of these statements about Java exceptions are correct? (i) An overriding method may throw any unchecked exception, even if the…
- What happens when this code is compiled? java try { new java.io.FileReader("x.txt"); } catch (java.io.IOException | Exception e) {…
- What is printed? java try { throw null; } catch (NullPointerException e) { System.out.print("NPE"); } catch (Exception e) {…
- What is printed? java int i = 0; while (true) { try { i++; if (i == 3) break; } finally { System.out.print(i); } }
- What is printed? java String s = null; switch (s) { case "a": System.out.print("A"); break; default: System.out.print("D"); }