ProgrammingVery hardProduct-company MCQ pattern

What is printed?

What is printed?
struct A { virtual void f() { cout << "A"; } };
struct B : virtual A { void f() override { cout << "B"; } };
struct C : virtual A {};
struct D : B, C {};
int main() {
    D d;
    A *p = &d;
    p->f();
}
  1. A. A
  2. B. B
  3. C. Compilation error: ambiguous base A
  4. D. AB

Correct answer: B. B

Virtual inheritance means D has exactly one shared A subobject, so there is no ambiguity. Among the two paths that could provide f(), only B actually overrides it; that override is the one visible in D's final overrider set, so dispatch through any A* to the shared subobject calls B::f.

More OOP with C++ and Java questions

Exams that ask this topic