Python Programming - Polymorphism
Exercise : Polymorphism - General Questions
- Polymorphism - General Questions
76.
How does Python achieve polymorphism through "operator overloading"?
Answer: Option
Explanation:
Python achieves polymorphism through operator overloading by allowing the definition of custom behavior for operators in a class, such as using methods like
__add__()
for the +
operator.
77.
Which of the following is an example of polymorphism through "function overriding"?
Answer: Option
Explanation:
Polymorphism through function overriding in Python involves defining a function with the same name in a module, where a subclass provides a specific implementation for the function.
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.
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers