ProgrammingHardMicrosoft/Amazon SDE MCQ · GATE CS

What is printed?

What is printed?
struct B {
    void f(int)    { cout << "Bi"; }
    void f(double) { cout << "Bd"; }
};
struct D : B { void f(double) { cout << "Dd"; } };
int main() {
    D d;
    d.f(3);
    d.B::f(3);
}
  1. A. DdBi
  2. B. BiBi
  3. C. DdBd
  4. D. BiDd

Correct answer: A. DdBi

Declaring f in D hides every B::f, so d.f(3) sees only D::f(double) and converts 3 to double. The qualified call d.B::f(3) looks in B and picks the exact match f(int).

More OOP with C++ and Java questions

Exams that ask this topic