ProgrammingHardProduct-company placement · C++

What is printed?

What is printed?
struct P {
    int a, b;
    P() : P(1)               { cout << "x"; }
    P(int a) : P(a, 2)       { cout << "y"; }
    P(int a, int b) : a(a), b(b) { cout << "z"; }
};
int main() { P p; cout << p.a << p.b; }
  1. A. zyx12
  2. B. xyz12
  3. C. xyz00
  4. D. zyx10

Correct answer: A. zyx12

With delegating constructors (C++11) the target constructor runs completely first, then the delegating constructor's body: P(int, int) prints z, then P(int) prints y, then P() prints x.

More OOP with C++ and Java questions

Exams that ask this topic