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?- A. HiBonjour
- B. Compile error
- C. HiHi
- 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
- Why is operator<< for printing a user-defined type usually written as a free function (often declared a friend), rather than as a member…
- java class R implements AutoCloseable { public void close() { System.out.print("close"); } } try (R r = new R()) {…
- Which statement about C++ vtables is correct for a class with multiple virtual functions?
- Class A's constructor declares throws IOException. Class B extends A. What must every constructor of B do about this checked exception?
- What access modifier and finality does a field declared directly inside a Java interface implicitly have?
- java Function<Integer,Integer> sq = x -> x x; Function<Integer,Integer> addOne = x -> x + 1; Function<Integer,Integer> combo =…