ProgrammingModerateProduct-company placement · Java
What happens when this is compiled?
What happens when this is compiled?
class P { P(int x) { } P() { } }
class C extends P { }
// in main:
C c = new C(5);- A. It compiles and calls P(int)
- B. It compiles and calls P() then ignores 5
- C. Runtime error: no matching constructor
- D. Compile-time error: C has no constructor taking an int
Correct answer: D. Compile-time error: C has no constructor taking an int
Constructors are not inherited. C only gets the compiler-supplied default constructor C(), which calls super().
More OOP with C++ and Java questions
- What happens when C is compiled? java class P { static void s() { } } class C extends P { @Override static void s() { } }
- Which lazily-initialised singleton is thread-safe WITHOUT any explicit synchronization or volatile field?
- What is printed? java // static generic method of the main class: static <T extends Comparable<T>> T max(T a, T b) { return a.compareTo(b)…
- What is printed? java class A { private int v = 1; boolean same(A other) { return v == other.v; } void set(A other) { other.v = 9; } } //…
- What is printed? cpp struct M { M(const char s) { cout << s; } }; struct S { M x, y; S() : y("Y"), x("X") { cout << "S"; } }; int main() {…
- What is printed? cpp struct B { virtual ~B() { hi(); } virtual void hi() { cout << "B"; } }; struct D : B { ~D() { cout << "~D "; } void…