ProgrammingHardTCS NQT · Infosys pattern
Which statement about this code is correct?
Which statement about this code is correct?
class P {
P(int x) { System.out.print("P" + x + " "); }
}
class C extends P {
C() { System.out.print("C "); }
}- A. It fails to compile: C()'s implicit super() call needs a no-argument P constructor, which doesn't exist
- B. It compiles because Java always supplies a default super constructor with zero-initialized arguments
- C. It compiles and prints 'P0 C ' when new C() runs
- D. It compiles, but calling new C() throws a runtime NoSuchMethodError
Correct answer: A. It fails to compile: C()'s implicit super() call needs a no-argument P constructor, which doesn't exist
Every constructor's first statement is implicitly super() unless written otherwise. P only declares P(int), not a no-arg constructor, so C()'s implicit super() fails to resolve — a compile-time error, not a runtime one.
More OOP with C++ and Java questions
- What is printed? cpp struct Base { virtual void show() { cout << "Base "; } }; struct Derived : Base { void show() override { cout <<…
- What is printed? java // in main: List<String> ls = new ArrayList<>(); List<Integer> li = new ArrayList<>()…
- Which statement about this class is correct? java class Example<T> { void method(List<String> list) { System.out.println("string-list"); }…
- What is printed? java class Util { static <T> T firstNonNull(T a, T b) { return a != null ? a : b; } } // in main: String s =…
- What is printed? java class Container<T extends Comparable<T>> { T max(T a, T b) { return a.compareTo(b) >= 0 ? a : b; } } // in main…
- What is printed? java class Node<T> { T data; static int count; { count++; } Node(T d) { data = d; } } // in main: new Node<Integer>(1)…