ProgrammingVery hardWipro Elite · TCS NQT Prime

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 { A() { f(); } virtual void f() { cout << "A"; } };
struct B : A { B() { f(); } void f() override { cout << "B"; } };
int main() { B b; }
  1. A. BB
  2. B. AA
  3. C. BA
  4. D. AB

Correct answer: D. AB

While A's constructor runs the object is only an A, so the virtual call resolves to A::f; B's constructor then calls B::f.

More OOP with C++ and Java questions

Exams that ask this topic