Python Programming - Polymorphism - Discussion
Discussion Forum : Polymorphism - General Questions (Q.No. 78)
78.
What is the output of the following Python code?
class Animal:
def make_sound(self):
return "Generic animal sound"
class Dog(Animal):
def make_sound(self):
return "Woof!"
class Cat(Animal):
def make_sound(self):
return "Meow!"
def pet_sounds(animals):
for animal in animals:
print(animal.make_sound())
dog = Dog()
cat = Cat()
pet_sounds([dog, cat])
Answer: Option
Explanation:
The
pet_sounds()
function demonstrates polymorphism, printing different sounds based on the specific implementations of the make_sound()
method in the Dog and Cat classes.
Discussion:
Be the first person to comment on this question !
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers