ProgrammingHardHCL · Tech Mahindra pattern

What is printed?

What is printed?
struct Base {
    virtual void f() = 0;
    void greet() { cout << "greet "; f(); }
};
struct Derived : Base {
    void f() override { cout << "Derived::f"; }
};
int main() {
    Derived d;
    d.greet();
}
  1. A. Compilation error: cannot call pure virtual f() from greet()
  2. B. greet Derived::f
  3. C. greet
  4. D. Compilation error: Base is abstract so greet() cannot be called

Correct answer: B. greet Derived::f

Base is abstract only because it has a pure virtual f(); non-pure methods like greet() are perfectly callable once a concrete derived type exists. Calling the pure virtual f() from within a non-constructor/destructor context dispatches normally via vtable to Derived::f, so output is "greet Derived::f".

More OOP with C++ and Java questions

Exams that ask this topic