ProgrammingHardProduct-company placement · C++

What is printed?

What is printed?
struct S {
    int a = 1;
    int b = 2;
    S() : a(10) { }
    S(int) { }
};
int main() { S x; S y(0); cout << x.a << x.b << y.a; }
  1. A. 1021
  2. B. 1011
  3. C. 121
  4. D. 10210

Correct answer: A. 1021

A default member initializer is used only by constructors that do not initialise that member in their own initializer list: S() overrides a with 10 but keeps b = 2; S(int) keeps both defaults.

More OOP with C++ and Java questions

Exams that ask this topic