ProgrammingVery hardIBM · Deloitte pattern

What is printed?

What is printed?
struct Base {
    virtual void f() const { cout << "Base "; }
};
struct Derived : Base {
    void f() const override { cout << "Derived "; }
    void f(int x) { cout << "Derived-int "; }
};
int main() {
    Base *p = new Derived;
    p->f();
}
  1. A. Derived
  2. B. Base
  3. C. Derived-int
  4. D. Compilation error: ambiguous overload between f() and f(int)

Correct answer: A. Derived

Overload resolution for p->f() only considers signatures visible through the static type Base (just f() const), so there's no ambiguity with Derived's extra f(int); among that single candidate, virtual dispatch then picks the actual override, Derived::f() const, printing "Derived".

More OOP with C++ and Java questions

Exams that ask this topic