ProgrammingVery hardMicrosoft/Amazon SDE MCQ · GATE CS
What is printed?
What is printed?
struct A { A(int v) { cout << v; } };
struct B : virtual A { B() : A(1) { } };
struct C : virtual A { C() : A(2) { } };
struct D : B, C { D() : A(3) { } };
int main() { D d; }- A. 3
- B. 12
- C. 123
- D. 312
Correct answer: A. 3
A virtual base is initialised once, by the most-derived class. D's A(3) runs and the A(1) and A(2) mem-initializers in B and C are ignored when they act as bases of D.
More OOP with C++ and Java questions
- What is printed? cpp struct V { V() { cout << 'V'; } }; struct X { X() { cout << 'X'; } }; struct M { M() { cout << 'm'; } }; struct D…
- 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…