ProgrammingVery hardProduct-company placement · Java
What is printed?
What is printed?
interface A { default String hi() { return "A"; } }
interface B extends A { default String hi() { return "B"; } }
class C implements A, B { }
// in main:
System.out.print(new C().hi());- A. B
- B. A
- C. Compile-time error: C inherits two defaults for hi()
- D. AB
Correct answer: A. B
There is no conflict because B is a subinterface of A and overrides its default: the most specific default (B's) wins. A conflict arises only between unrelated interfaces.
More OOP with C++ and Java questions
- What is printed? java // static methods of the main class: static void f(Object o) { System.out.print("O"); } static void f(Number n) {…
- A base type must hold common fields, have a constructor that validates them, and share some method bodies, while subclasses form a strict…
- java.io.BufferedReader wraps another Reader, is itself a Reader, and adds buffering on top of it; such wrappers can be stacked at run…
- Collections.sort(list, comparator) lets the caller pass the comparison algorithm as an object, and the same sort code works with any…
- Square extends Rectangle and overrides setWidth and setHeight so that both always change together. Client code written for Rectangle does…
- A Department has Professors who continue to exist if the department is closed, while a House is made of Rooms that cannot exist without…