ProgrammingModerateProduct-company placement · Java
What is printed?
What is printed?
interface Greeter {
String name();
default String greet() { return "Hi " + name(); }
}
class G implements Greeter {
public String name() { return "G"; }
public String greet() { return Greeter.super.greet() + "!"; }
}
// in main:
System.out.print(new G().greet());- A. Hi !
- B. G!
- C. Compile-time error at Greeter.super.greet()
- D. Hi G!
Correct answer: D. Hi G!
Interface.super.method() calls the interface's default implementation from a direct implementer; inside it, name() is dispatched to G.
More OOP with C++ and Java questions
- Which line fails to compile? java abstract class Shape { abstract double area(); } // in main: Shape a = new Shape(); // L1 Shape b = new…
- Which lines fail to compile? java class P { void f() { } static void g() { } } class C extends P { static void f() { } // L1 void g() { }…
- Which line fails to compile? java class P { Number get() { return 1; } int size() { return 0; } } class C extends P { Integer get() {…
- What is printed? java class T { int n; { n += 5; } T() { this(1); n = 2; } T(int k) { n += k; } } // in main: System.out.print(new T().n);
- What is printed? java class A { public String toString() { return "A"; } } class B extends A { public String toString() { return "B" +…
- What happens when Z is compiled? java interface X { int V = 1; } interface Y { int V = 2; } class Z implements X, Y { int get() { return…