ProgrammingHardProduct-company placement · C++
What is printed?
What is printed?
struct A { A() { cout << "A"; } ~A() { cout << "a"; } };
struct B { B() { cout << "B"; } ~B() { cout << "b"; } };
struct M { M() { cout << "M"; } ~M() { cout << "m"; } };
struct C : B, A {
M m;
C() { cout << "C"; }
~C() { cout << "c"; }
};
int main() { C c; }- A. ABMCcmba
- B. BAMCcmab
- C. BAMCcbam
- D. MBACcabm
Correct answer: B. BAMCcmab
Bases are constructed in the order they are LISTED in the class head (B, then A), then members, then the constructor body. Destruction is the exact reverse: body, member, A, B.
More OOP with C++ and Java questions
- Which line fails to compile? cpp string a = "a" + "b"; // L1 string b = string("a") + "b"; // L2 string c = "a" + string("b"); // L3…
- What is printed? cpp int main() { string s = "hi"; try { cout << s.at(1); s.at(5); cout << "X"; } catch (const outofrange&) { cout << "R"…
- What is printed? cpp int main() { map<string, int> m; if (m["x"] == 0) cout << m.size(); cout << m.count("y") << m.size(); }
- What is printed? cpp int main() { set<int, greater<int>> s{3, 1, 3, 2}; for (int x : s) cout << x; cout << s.size(); }
- What is printed? cpp int main() { vector<int> v{1, 2, 3, 2, 4}; v.erase(remove(v.begin(), v.end(), 2), v.end()); cout << v.size() << ":"…
- What is wrong with this loop? cpp vector<int> v{1, 2, 3}; for (int x : v) if (x == 1) v.pushback(1);