ProgrammingHardHCL · Tech Mahindra pattern

What is printed?

What is printed?
struct Base {
    Base() { cout << "Base() "; }
    Base(int x) { cout << "Base(int) "; }
};
struct Derived : Base {
    Derived() { cout << "Derived() "; }
};
int main() { Derived d; }
  1. A. Base(int) Derived()
  2. B. Base() Derived()
  3. C. Derived() Base()
  4. D. Compile-time error: Derived() must explicitly choose which Base constructor to call

Correct answer: B. Base() Derived()

Derived()'s member-initializer list is empty, so the compiler implicitly inserts a call to Base's default constructor, Base(). It runs first (printing Base()), and then Derived's own body runs, printing Derived(). Base(int) is simply never used.

More OOP with C++ and Java questions

Exams that ask this topic