ProgrammingModerateMicrosoft/Google SDE MCQ · GATE CS

What is printed?

What is printed?
struct B { }; struct D : B { };
void f(B&) { cout << "B"; }
void f(D&) { cout << "D"; }
int main() {
    D d;
    B& b = d;
    f(b);
    f(d);
}
  1. A. DD
  2. B. BB
  3. C. BD
  4. D. DB

Correct answer: C. BD

Overload resolution uses the static type of each argument: b is a B&, so f(B&) is chosen even though it refers to a D. Only virtual member functions dispatch on the dynamic type.

More OOP with C++ and Java questions

Exams that ask this topic