ProgrammingHardGATE CS · TCS NQT Prime · Product Company SDE

java interface Greet { default String hello() { return "Hi"; } } class English implements Greet {} class French implements Greet { public String hello() {…

interface Greet {
    default String hello() { return "Hi"; }
}
class English implements Greet {}
class French implements Greet {
    public String hello() { return "Bonjour"; }
}
// main:
Greet g1 = new English();
Greet g2 = new French();
System.out.print(g1.hello() + g2.hello());
What is printed?
  1. A. HiBonjour
  2. B. Compile error
  3. C. HiHi
  4. D. BonjourBonjour

Correct answer: A. HiBonjour

English does not override hello(), so it inherits the interface's default ("Hi"); French explicitly overrides it to return "Bonjour", so the concatenation is HiBonjour.

More OOP with C++ and Java questions

Exams that ask this topic