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 V { V(int x) { cout << "V" << x; } V() { cout << "V0"; } };
struct A : virtual V { A() : V(1) { cout << "A"; } };
struct B : virtual V { B() : V(2) { cout << "B"; } };
struct D : A, B { D() : V(3) { cout << "D"; } };
int main() { D d; }- A. V1AV2BD
- B. V0ABD
- C. V3ABD
- D. V1ABD
Correct answer: C. V3ABD
The virtual base is built once, by the most derived class D with V(3); A's and B's V(1)/V(2) initializers are ignored, then A, B, D run.
More OOP with C++ and Java questions
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { A() { cout << "A"; } }; struct B {…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct I { int v; I& operator++() { ++v…
- What is printed? (also assume <typeinfo>) (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { virtual…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct G { const char n; G(const char s)…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { virtual void f() { cout << "A"; }…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp void f(bool) { cout << "b"; } void f(string) {…