ProgrammingVery hardGATE CS · TCS Digital · Advanced
What is printed?
What is printed?
interface A { default String m() { return "A"; } }
interface B extends A { default String m() { return "B"; } }
interface C extends A { }
class D implements B, C { }
class E implements B, C {
public String m() { return B.super.m() + "!"; }
}
// in main:
System.out.print(new D().m() + new E().m());- A. compile error
- B. BA!
- C. BB!
- D. AB!
Correct answer: C. BB!
B.m overrides A.m, so A.m is not inherited through C in the presence of B; D simply inherits B.m and there is no conflict. E calls B's default explicitly with B.super.m().
More OOP with C++ and Java questions
- What is printed? java class P { String n = "P"; static String s() { return "Ps"; } String t() { return "Pt"; } } class C extends P {…
- What is printed? (C++17, assume <iostream> and using namespace std) cpp struct M { const char n; M(const char s) : n(s) { } ~M() { cout <<…
- What is printed? (C++17, assume <iostream> and using namespace std) cpp struct P { P(const char s) { cout << s; } }; struct Q { P y{"y"}…
- What is printed? java String s1 = "hi"; String s2 = new String("hi"); System.out.print((s1 == s2) + " " + s1.equals(s2));
- What is printed? (C++, assume <iostream> and using namespace std) cpp struct C { static int n; C() { n++; } ~C() { n--; } }; int C::n = 0…
- Which statement about abstract classes in Java is correct?