ProgrammingVery hardTCS NQT Digital · Accenture Advanced
What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std)
What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std)
struct X { string n; X(const char* s) : n(s) { cout << "+" << n; } ~X() { cout << "-" << n; } };
struct H {
X a{"a"}; X b{"b"};
H() : b("B"), a("A") { cout << "h"; }
~H() { cout << "~"; }
};
int main() { H h; }- A. +B+Ah~-A-B
- B. +A+Bh-B-A~
- C. +A+Bh~-B-A
- D. +B+Ah~-B-A
Correct answer: C. +A+Bh~-B-A
Members are constructed in declaration order (a then b) regardless of the mem-initializer order; destruction runs the destructor body then members in reverse.
More OOP with C++ and Java questions
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { A() { f(); } virtual void f() {…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { virtual ~A() { f(); } virtual void…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct T { T() { cout << "d"; } T(const T&) {…
- What is printed? (no move constructor is declared) (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct T {…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct C { static int n; int id; C() : id(++n)…
- What is the result? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { void f(int) { cout << "Ai"; }…