ProgrammingVery hardProduct-company MCQ pattern

What is printed?

What is printed?
struct Animal {
    virtual std::string sound() const { return "..."; }
};
struct Dog : Animal {
    std::string sound() const override { return "Woof"; }
};
void printSound(Animal a) { std::cout << a.sound(); }
int main() {
    Dog d;
    printSound(d);
}
  1. A. Woof
  2. B. Compilation error: cannot pass Dog where Animal expected
  3. C. Woof...
  4. D. ...

Correct answer: D. ...

printSound takes Animal BY VALUE, so passing a Dog slices off the Dog-specific part, copying only the Animal subobject into the parameter a. a is a true Animal at that point, so a.sound() calls Animal::sound(), printing "..." — this is the classic object-slicing bug.

More OOP with C++ and Java questions

Exams that ask this topic