ProgrammingVery hardProduct-company placement · C++
What is printed?
What is printed?
struct A { A(int x) { cout << x; } A() { cout << 0; } };
struct B : virtual A { B() : A(1) { cout << "B"; } };
struct C : virtual A { C() : A(2) { cout << "C"; } };
struct D : B, C { D() : A(3) { cout << "D"; } };
int main() { D d; }- A. 1BCD
- B. 12BCD
- C. 0BCD
- D. 3BCD
Correct answer: D. 3BCD
A virtual base is constructed exactly once, by the MOST derived class, before any non-virtual base. D's A(3) is used and the A(...) initialisers in B and C are ignored.
More OOP with C++ and Java questions
- What is printed? cpp void f(long) { cout << "L"; } void f(double) { cout << "D"; } int main() { f(5L); f(2.5f); f(5); }
- What is printed? cpp struct B { int b; B(int x) : b(x) { } }; struct D : B { int d; D(int x, int y) : B(x), d(y) { } }; int main() { D…
- How many of these statements about C++ dynamic memory are correct? (i) Memory obtained with new[] must be released with delete[]. (ii)…
- What happens when this is compiled? cpp struct K { const int c; int& r; K(int& x) { c = 1; r = x; } };
- Which line fails to compile? cpp struct A { A(int) { } }; struct B { B(A) { } }; void f(B) { } int main() { f(A(1)); // L1 f(1); // L2 }
- In C++, a destructor throws an exception while the stack is already being unwound because of another exception. What happens?