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();
  1. A. IB-A CTOR-A IB-B CTOR-B
  2. B. IB-B CTOR-B IB-A CTOR-A
  3. C. IB-A IB-B CTOR-A CTOR-B
  4. 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

Exams that ask this topic