ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
class A {
{ System.out.print("IB-A "); }
A() { System.out.print("CTOR-A "); }
}
class B extends A {
{ System.out.print("IB-B "); }
B() {
super();
System.out.print("CTOR-B ");
}
}
// in main:
new B();- A. IB-A CTOR-A IB-B CTOR-B
- B. IB-B CTOR-B IB-A CTOR-A
- C. IB-A IB-B CTOR-A CTOR-B
- D. CTOR-A IB-A CTOR-B IB-B
Correct answer: A. IB-A CTOR-A IB-B CTOR-B
super() must run first inside B's constructor (implicitly or, here, explicitly), which triggers A's own instance-init-block-then-constructor-body sequence first: IB-A, CTOR-A. Only after A's construction completes does B run its own instance initializer block and then its constructor body: IB-B, CTOR-B.
More OOP with C++ and Java questions
- What is printed? cpp void f(int x) { cout << "int"; } void f(double x) { cout << "double"; } int main() { f('a'); }
- What happens when this is compiled? java class A { final void m() { System.out.println("A"); } } class B extends A { void m() {…
- What happens when this is compiled? java class Picker { static void f(String s) { System.out.print("String"); } static void…
- What is printed? java class A { static void m() { System.out.println("A.static"); } } class B extends A { static void m() {…
- What happens when this is compiled? cpp struct A { virtual void f() final { cout << "A"; } }; struct B : A { void f() override { cout <<…
- What happens when this is compiled? java class A { int m() { return 1; } double m() { return 1.0; } }