ProgrammingVery hardTCS NQT Prime · Infosys SP
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 A {
virtual ~A() { f(); }
virtual void f() { cout << "A"; }
};
struct B : A {
~B() { cout << "b"; }
void f() override { cout << "B"; }
};
int main() { A* p = new B; delete p; }- A. bA
- B. bB
- C. B
- D. Ab
Correct answer: A. bA
~B runs first and prints b; by the time ~A runs the B part is destroyed, so the virtual call f() resolves to A::f.
More OOP with C++ and Java questions
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct T { T() { cout << "d"; } T(const T&) {…
- What is printed? (no move constructor is declared) (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct T {…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct C { static int n; int id; C() : id(++n)…
- What is the result? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { void f(int) { cout << "Ai"; }…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { virtual int g(int x = 10) { return…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct V { V(int x) { cout << "V" << x; } V()…