ProgrammingHardGATE CS · TCS Digital · Advanced

What is printed? (C++17, assume <iostream> and using namespace std)

What is printed? (C++17, assume <iostream> and using namespace std)
struct P { P(const char* s) { cout << s; } };
struct Q {
    P y{"y"};
    P x{"x"};
    Q() : x("X"), y("Y") { }
};
int main() { Q q; }
  1. A. YX
  2. B. xyXY
  3. C. XY
  4. D. yxYX

Correct answer: A. YX

Members are initialised in declaration order (y, then x), regardless of the order in the mem-initialiser list; the mem-initialisers replace the default member initialisers, so 'Y' and 'X' (not 'y' or 'x') are printed.

More OOP with C++ and Java questions

Exams that ask this topic