ProgrammingHardHCL · TCS NQT

What is printed? (C++, assume <iostream> and using namespace std)

What is printed? (C++, assume <iostream> and using namespace std)
struct B {
    virtual void f() = 0;
};
void B::f() { cout << "B"; }
struct D : B {
    void f() override { B::f(); cout << "D"; }
};
int main() { D d; d.f(); }
  1. A. D
  2. B. B
  3. C. Compile error: a pure virtual function cannot have a body
  4. D. BD

Correct answer: D. BD

A pure virtual function may still have a definition; the derived override calls it explicitly and then prints D.

More OOP with C++ and Java questions

Exams that ask this topic