ProgrammingVery hardProduct-company placement · Java
What happens when this is compiled?
What happens when this is compiled?
interface A { default void hello() { System.out.print("A"); } }
interface B extends A { void hello(); }
class C implements B { }- A. It compiles; new C().hello() prints A
- B. Compile-time error: C must implement hello()
- C. Compile-time error in B: a default method cannot be redeclared abstract
- D. It compiles; calling hello() throws AbstractMethodError
Correct answer: B. Compile-time error: C must implement hello()
A sub-interface may re-abstract an inherited default method. B's hello() is abstract again, so the concrete class C must provide it.
More OOP with C++ and Java questions
- 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"); }
- What is printed? java class Box<T> { static int count = 0; Box() { count++; } } // in main: new Box<String>(); new Box<Integer>(); new…