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();
}- A. Compilation error: ambiguous move() between IWalk and ISwim
- B. waddle-swim
- C. waddlewaddle-swim
- 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
- What is printed? cpp struct Base { static void hello() { std::cout << "Base::hello"; } }; struct Derived : Base { static void hello() {…
- What is printed? cpp struct Base { virtual Base clone() { std::cout << "Base::clone "; return new Base(this); } }; struct Derived : Base {…
- What is printed? cpp class Base { public: Base() { std::cout << "Base() "; } virtual void show() { std::cout << "Base::show"; } }; class…
- What is printed? cpp class Complex { double re, im; public: Complex(double r, double i) : re(r), im(i) {} Complex operator+(const Complex&…
- What is printed? java interface Calculator { int calculate(int a, int b); } public class Main { public static void main(String[] args) {…
- What is printed? java class Matrix { int[][] data; Matrix(int[][] d) { data = d; } Matrix add(Matrix other) { int[][] r = new…