ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
struct Base    { virtual void show() { cout << "Base "; } };
struct Derived : Base { void show() override { cout << "Derived "; } };
int main() {
    Derived d;
    Base b = d;
    Base &r = d;
    b.show();
    r.show();
}
  1. A. Derived Base
  2. B. Base Derived
  3. C. Base Base
  4. D. Derived Derived

Correct answer: B. Base Derived

Base b = d copies only the Base part (object slicing), so b really is a Base and calls Base::show. A reference keeps the dynamic type, so r.show() dispatches to Derived::show.

More OOP with C++ and Java questions

Exams that ask this topic