ProgrammingHardIBM · Deloitte pattern

What is printed?

What is printed?
struct IWalk { virtual void move() = 0; };
struct ISwim { virtual void move() = 0; };
struct Duck : IWalk, ISwim {
    void move() override { std::cout << "waddle-swim"; }
};
int main() {
    Duck d;
    IWalk *w = &d;
    w->move();
}
  1. A. Compilation error: ambiguous move() between IWalk and ISwim
  2. B. waddle-swim
  3. C. waddlewaddle-swim
  4. D. Compilation error: Duck must implement move() twice

Correct answer: B. waddle-swim

Duck provides a single move() override that satisfies both IWalk's and ISwim's pure virtual requirements simultaneously (there's no diamond conflict here since neither interface shares a common virtual base with distinct storage — Duck::move is one function overriding both). Calling through either base pointer dispatches to that same Duck::move().

More OOP with C++ and Java questions

Exams that ask this topic