Well done!
You have completed Advanced Concepts in Object-Oriented Programming: Overriding, Inheritance, and Polymorphism Quiz!
Quiz Question 1 of 5
Given the following classes:
`class Animal: def sound(self): return "Generic sound"
class Dog(Animal): def sound(self): return "Bark"
class Cat(Animal): def sound(self): return "Meow"`
If we have a list of animals [Dog(), Cat()]
, and iterate through this list calling the sound()
method on each object, which of the following statements best explains the outcome?
Choose the correct answer below:
-
A
The method
sound()
will return "Bark" twice, asDog()
is the first instance. -
B
Both objects will call the
sound()
method of theAnimal
class, returning "Generic sound" twice. -
C
The iteration will cause a conflict because the
sound()
method is ambiguous, resulting in an error. -
D
The method
sound()
in each subclass overrides the method inAnimal
, returning "Bark" and "Meow" respectively.