ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
class Base {
public:
    void show()          { cout << "B1"; }
    virtual void vshow() { cout << "B2"; }
};
class Derived : public Base {
public:
    void show()  { cout << "D1"; }
    void vshow() { cout << "D2"; }
};
int main() {
    Base *b = new Derived;
    b->show();
    b->vshow();
}
  1. A. B1B2
  2. B. D1B2
  3. C. B1D2
  4. D. D1D2

Correct answer: C. B1D2

show is not virtual, so the call is bound at compile time by the pointer type Base*: B1. vshow is virtual, so the call goes through the vtable to the actual object, a Derived: D2.

More OOP with C++ and Java questions

Exams that ask this topic