ProgrammingVery hardMicrosoft/Amazon SDE MCQ · GATE CS
What is printed?
What is printed?
struct V { V() { cout << 'V'; } };
struct X { X() { cout << 'X'; } };
struct M { M() { cout << 'm'; } };
struct D : X, virtual V {
M m;
D() { cout << 'D'; }
};
int main() { D d; }- A. XVmD
- B. mXVD
- C. VXmD
- D. XmVD
Correct answer: C. VXmD
Virtual bases are constructed before all non-virtual bases, regardless of the order in the base list. Then the non-virtual base X, then members (m), and finally the constructor body.
More OOP with C++ and Java questions
- What is printed? cpp struct R { R() { cout << "r"; } ~R() { cout << "~r"; } }; struct T { R r; T() { cout << "t"; throw 1; } ~T() { cout…
- What is printed? cpp struct E { virtual const char w() const { return "E"; } }; struct F : E { const char w() const override { return "F"…
- What is printed? cpp struct E { }; struct F : E { }; int main() { try { try { throw F(); } catch (E& e) { throw e; } } catch (F&) { cout…
- What is printed? cpp struct S { void f() { cout << "n"; } void f() const { cout << "c"; } }; int main() { S a; const S b{}; a.f(); b.f()…
- What is printed? cpp template <class T> void f(T) { cout << "T"; } template <> void f<int>(int) { cout << "S"; } template <class T> void…
- What is printed? cpp struct T { T() { } T(const T&) { cout << "C"; } T& operator=(const T&) { cout << "A"; return this; } }; int main() {…