ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
class Base {
public:
    virtual void op() { cout << "Base::op "; }
    void callOp() { this->op(); }
};
class Derived : public Base {
public:
    void op() override { cout << "Derived::op "; }
    void callBaseOp() { Base::op(); }
};
int main() {
    Derived d;
    d.callOp();
    d.callBaseOp();
}
  1. A. Derived::op Derived::op
  2. B. Base::op Base::op
  3. C. Derived::op Base::op
  4. D. Base::op Derived::op

Correct answer: C. Derived::op Base::op

callOp() calls this->op() virtually, dispatching to Derived::op on a real Derived object: "Derived::op ". callBaseOp() explicitly qualifies the call as Base::op(), bypassing virtual dispatch entirely and calling the base version directly: "Base::op ".

More OOP with C++ and Java questions

Exams that ask this topic