ProgrammingHardHCL · Tech Mahindra pattern
What is printed?
What is printed?
interface Flyer { default String move() { return "fly"; } }
interface Swimmer { default String move() { return "swim"; } }
class Duck implements Flyer, Swimmer {
public String move() { return Flyer.super.move() + "+" + Swimmer.super.move(); }
}
// in main:
System.out.println(new Duck().move());- A. fly
- B. swim
- C. fly+swim
- D. Compile-time error: cannot call two different interfaces' super methods in one expression
Correct answer: C. fly+swim
Duck resolves the diamond conflict by explicitly invoking both parents' defaults with InterfaceName.super.method(), concatenating their results, so move() returns "fly+swim".
More OOP with C++ and Java questions
- What is printed? java class Counter { protected int count = 0; void inc() { count++; } } class LoggingCounter extends Counter { @Override…
- What is printed? cpp struct Base { virtual void f() const { cout << "Base-const"; } }; struct Derived : Base { void f() const override {…
- What is printed? java class Shape { double area() { return 0; } } class Circle extends Shape { double radius; Circle(double r) { radius =…
- What is printed? java class Node { Node next; int val; Node(int v) { val = v; } @Override public String toString() { return "N(" + val +…
- What is printed? java class Employee { double bonus() { return 1000; } double pay() { return 50000 + bonus(); } } class Manager extends…
- What is printed? cpp struct Base { Base() { cout << "CtorBase "; } virtual ~Base() { cout << "DtorBase "; } }; struct Derived : Base {…