ProgrammingVery hardProduct-company placement · Java
What happens?
What happens?
interface Show { String toString(); }
class C implements Show { }
// in main:
System.out.print(new C() instanceof Show);- A. Compile-time error: C must implement toString()
- B. It compiles but calling toString() throws AbstractMethodError
- C. It compiles and prints true: Object's public toString() implements Show.toString()
- D. Compile-time error: an interface cannot redeclare toString()
Correct answer: C. It compiles and prints true: Object's public toString() implements Show.toString()
An interface may redeclare a public Object method as abstract. Every class inherits a public toString() from Object, which satisfies it, so C is concrete.
More OOP with C++ and Java questions
- What is printed? java System.out.print((Double.NaN == Double.NaN) + " " + Double.valueOf(Double.NaN).equals(Double.NaN) + " " + (0.0 ==…
- A class overrides equals correctly, but its hashCode() returns 42 for every object. What happens when many such objects are stored in a…
- What happens? java class P { int v; P(int v) { this.v = v; } } // in main: Set<P> s = new TreeSet<>(); s.add(new P(1)); s.add(new P(2))…
- Which constructors cause compile-time errors? java class F { final int x; F() { } // constructor 1 F(int v) { x = v; x = v + 1; } //…
- What is printed? java Map<String, Integer> m = new HashMap<>(); m.compute("a", (k, v) -> v == null ? 1 : v + 1); m.compute("a", (k, v) ->…
- What is printed? java Map<String, String> h = new HashMap<>(); h.put("k", null); System.out.print(h.size() + " "); Map<String, String> c =…