Python Programming - Polymorphism
Exercise : Polymorphism - General Questions
- Polymorphism - General Questions
66.
How does Python achieve polymorphism through "method overloading"?
Answer: Option
Explanation:
Polymorphism through method overloading in Python involves allowing a function to be defined with the same name but different parameters.
67.
Which of the following is an example of polymorphism through "operator overloading"?
Answer: Option
Explanation:
Polymorphism through operator overloading in Python involves defining custom behavior for operators in a class, such as using
__add__()
for the +
operator.
68.
What is the output of the following Python code?
class Vehicle:
def start(self):
return "Generic vehicle starting"
class Car(Vehicle):
def start(self):
return "Car starting"
class Bike(Vehicle):
def start(self):
return "Bike starting"
def drive(vehicle):
return vehicle.start()
car = Car()
bike = Bike()
print(drive(car))
print(drive(bike))
Answer: Option
Explanation:
The
drive()
function demonstrates polymorphism, accepting both Car and Bike instances and producing different outputs based on their specific implementations of the start()
method.
69.
In Python, what is the purpose of the
__call__()
method in the context of polymorphism?
Answer: Option
Explanation:
The
__call__()
method is used to customize the behavior when an instance is called as a function, allowing for polymorphic behavior.
70.
Consider the following code:
class Animal:
def sound(self):
return "Generic animal sound"
class Duck(Animal):
def sound(self):
return "Quack!"
class Lion(Animal):
def sound(self):
return "Roar!"
What concept is demonstrated in this code?
Answer: Option
Explanation:
This code demonstrates method overriding, where the subclasses provide specific implementations for a method defined in the superclass.
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers