ProgrammingHardProduct-company placement · C++
What is printed?
What is printed?
struct M { M(const char* s) { cout << s; } };
struct S {
M x, y;
S() : y("Y"), x("X") { cout << "S"; }
};
int main() { S s; }- A. YXS
- B. SXY
- C. SYX
- D. XYS
Correct answer: D. XYS
Members are always initialised in the order they are DECLARED in the class (x, then y), whatever order the initializer list is written in; the constructor body runs last. Compilers only warn about the mismatch.
More OOP with C++ and Java questions
- What is printed? cpp struct B { virtual ~B() { hi(); } virtual void hi() { cout << "B"; } }; struct D : B { ~D() { cout << "~D "; } void…
- What is printed? cpp struct E { virtual const char what() const { return "E"; } }; struct F : E { const char what() const override {…
- What is printed? cpp struct E { }; struct F : E { }; int main() { try { throw F(); } catch (E&) { cout << "E"; } catch (F&) { cout << "F"…
- What is printed? cpp struct E { virtual void id() const { cout << "E"; } }; struct F : E { void id() const override { cout << "F"; } }…
- What is printed? cpp struct T { char c; T(char x) : c(x) { cout << c; } ~T() { cout << c << c; } }; void f() { T a('a'); T b('b'); throw…
- What is printed? cpp struct A { A() { cout << "A"; } ~A() { cout << "a"; } }; struct B { B() { cout << "B"; throw 0; } ~B() { cout << "b"…