PCPP 32 10x PCPP Certified Professional Python Programmer 1 · Free Practice Question Easy
Question 40
Consider the following class hierarchy in Python:
- class Animal:
- def __init__(self, name):
- self.name = name
- def eat(self):
- print(f"{self.name} is eating.")
- class Mammal(Animal):
- def __init__(self, name, sound):
- super().__init__(name)
- self.sound = sound
- def make_sound(self):
- print(f"{self.name} makes a {self.sound} sound.")
- class Dog(Mammal):
- def __init__(self, name, sound, breed):
- super().__init__(name, sound)
- self.breed = breed
- def wag_tail(self):
- print(f"{self.name} is wagging its tail.")
Given the class hierarchy above, which of the following statements is true?
-
A
The
Animalclass inherits from theMammalclass. -
B
An instance of the
Dogclass will have access to theeat()method. -
C
The
Mammalclass has access to thewag_tail()method. -
D
The
Dogclass is a superclass of theMammalclass.
Reveal correct answer
Correct answer: B
A.
In the given class hierarchy, the Animal class is the superclass of the Mammal class. It is the other way around—the Mammal class inherits from the Animal class.
B.
Since the Dog class inherits from the Mammal class, an instance of the Dog class will also inherit the eat() method from the Animal class through the Mammal class. Therefore, an instance of Dog will have access to the eat() method.
C.
The wag_tail() method is defined in the Dog class, not in the Mammal class. Instances of the Mammal class do not have access to the wag_tail() method.
D.
The Dog class is not a superclass of the Mammal class. Instead, the Mammal class is the superclass of the Dog class.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
