ProgrammingVery hardProduct-company placement · Java
What happens when this is compiled?
What happens when this is compiled?
interface I { int f(); }
abstract class A { abstract long f(); }
class C extends A implements I {
public long f() { return 1; }
}- A. It compiles; C.f() implements both
- B. It compiles; calls through I return 1 cast to int
- C. Compile-time error only in class A
- D. Compile-time error: f() cannot satisfy both int f() and long f()
Correct answer: D. Compile-time error: f() cannot satisfy both int f() and long f()
C inherits two abstract methods with the same signature but incompatible return types (int and long). No single method can override both, so the class cannot be compiled.
More OOP with C++ and Java questions
- What happens when this is compiled? java interface I { default void f() { System.out.print("I"); } } abstract class A { public abstract…
- 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…