ProgrammingHardMicrosoft/Google SDE MCQ · GATE CS

What is printed?

What is printed?
struct A { int x; A(int v) : x(v) { } };
struct B : A {
    int y;
    B(int v) : A(v * 2), y(x + 1) { }
};
int main() {
    B b(5);
    cout << b.x << " " << b.y;
}
  1. A. 10 6
  2. B. 5 6
  3. C. 10 11
  4. D. 5 11

Correct answer: C. 10 11

The base A is initialised first with v * 2 = 10, so by the time y is initialised, x is already 10 and y = 11. Using x here is safe because base subobjects are constructed before members.

More OOP with C++ and Java questions

Exams that ask this topic