ProgrammingHardWipro · Cognizant technical pattern
What is printed?
What is printed?
interface I { default String who() { return "I"; } }
class P { public String who() { return "P"; } }
class C extends P implements I { }
// in main:
System.out.print(new C().who());- A. I
- B. Compilation error: conflicting who()
- C. PI
- D. P
Correct answer: D. P
Rule: “class wins”. A method inherited from a superclass takes priority over an interface default method with the same signature, so there is no conflict and P’s version runs. A conflict error arises only between two interface defaults.
More OOP with C++ and Java questions
- What is printed? java StringBuilder a = new StringBuilder("hi"); StringBuilder b = new StringBuilder("hi"); System.out.print(a.equals(b) +…
- What happens when this Java code is compiled and run? java Object o = new ArrayList<String>(); List<String> l = (List<String>) o; String s…
- What is printed? cpp struct A { A() { cout << "D"; } A(const A&) { cout << "C"; } A& operator=(const A&) { cout << "="; return this; } }…
- What is printed? java String s = "b"; switch (s) { case "a": System.out.print("A"); case "b": System.out.print("B"); case "c"…
- What happens when this Java code is compiled? java try { FileReader r = new FileReader("a.txt"); } catch (Exception e) {…
- This class is meant to be immutable. What change is still needed? java public final class Team { private final List<String> members…