ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
struct Base {
virtual void greet() const { cout << "Base "; }
};
struct Derived : Base {
void greet() const override { Base::greet(); cout << "Derived "; }
};
struct MoreDerived : Derived {
void greet() const override { Derived::greet(); cout << "MoreDerived"; }
};
int main() {
Base *p = new MoreDerived;
p->greet();
}- A. Base Derived MoreDerived
- B. MoreDerived Derived Base
- C. Base MoreDerived Derived
- D. MoreDerived
Correct answer: A. Base Derived MoreDerived
p->greet() dispatches virtually to MoreDerived::greet, which explicitly calls Derived::greet() first (a non-virtual, qualified call), which itself explicitly calls Base::greet() first — unwinding the explicit chain gives Base, then Derived, then MoreDerived's own print.
More OOP with C++ and Java questions
- What is printed? cpp class Base { public: Base(const Base&) = delete; Base() = default; virtual void show() { cout << "Base"; } }; class…
- What is printed? java class Vehicle { void move() { System.out.print("Vehicle moves "); } } class Car extends Vehicle { @Override void…
- What is printed? java class Animal { String category() { return "Animal"; } } class Mammal extends Animal { @Override String category() {…
- What is printed? java class Shape { double area() { return 0; } @Override public String toString() { return getClass().getSimpleName() +…
- What is printed? cpp class Base { public: Base() { cout << "Base "; } Base& operator=(const Base&) { cout << "Base= "; return this; } }…
- What is printed? cpp struct Base { Base() { cout << "B "; } virtual ~Base() { cout << "~B "; } }; struct Derived : Base { int data…