ProgrammingVery hardProduct-company placement · Java
What happens when this is compiled?
What happens when this is compiled?
interface I { default void f() { System.out.print("I"); } }
abstract class A { public abstract void f(); }
class C extends A implements I { }- A. It compiles; new C().f() prints I
- B. Compile-time error: A and I conflict
- C. Compile-time error: C must implement f()
- D. It compiles; calling f() throws AbstractMethodError
Correct answer: C. Compile-time error: C must implement f()
'Class wins': a method inherited from the superclass, even an abstract one, takes precedence over an interface default. A.f() is abstract, so the concrete class C must implement it.
More OOP with C++ and Java questions
- What is printed? java int[] arr = {1, 2, 3}; for (int x : arr) x = 2; System.out.print(Arrays.toString(arr));
- What is printed? java Set<String> s = new LinkedHashSet<>(List.of("a", "b", "c")); s.remove("a"); s.add("a"); s.add("b")…
- What is printed? java TreeMap<String, Integer> m = new TreeMap<>(); for (String k : new String[]{"9", "10", "100", "2"}) m.put(k…
- What is printed? java String r = Collections.max(List.of("pear", "fig", "banana", "kiwi"), Comparator.comparing(String::length))…
- Which line fails to compile? java int[] a = {3, 1, 2}; Integer[] b = {3, 1, 2}; Arrays.sort(b, Collections.reverseOrder()); // L1…
- Which line fails to compile? java class P { void hi() { } } class C extends P { void a() { super.hi(); } // L1 static void b() {…