Python Programming - Polymorphism
Exercise : Polymorphism - General Questions
- Polymorphism - General Questions
61.
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.
62.
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.
63.
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.
64.
In Python, what is the purpose of the
__getitem__()
method in the context of polymorphism?
Answer: Option
Explanation:
The
__getitem__()
method is used to customize the behavior when an item is accessed using square brackets on an instance of the class, allowing for polymorphic behavior.
65.
Consider the following code:
class Shape:
def draw(self):
return "Drawing a shape"
class Triangle(Shape):
def draw(self):
return "Drawing a triangle"
class Rectangle(Shape):
def draw(self):
return "Drawing a rectangle"
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