Python Programming - Polymorphism
Exercise : Polymorphism - General Questions
- Polymorphism - General Questions
26.
Consider the following code:
class Shape:
def area(self):
pass
class Circle(Shape):
def area(self, radius):
return 3.14 * radius * radius
class Rectangle(Shape):
def area(self, length, width):
return length * width
What concept is demonstrated in this code?
Answer: Option
Explanation:
This code demonstrates method overloading, where the subclass methods have the same name but different parameters than the superclass method.
27.
How does Python achieve polymorphism through method overriding?
Answer: Option
Explanation:
Polymorphism through method overriding in Python allows a subclass to provide a specific implementation for a method defined in the superclass.
28.
What is the purpose of the
__getitem__()
method in Python classes 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, allowing for polymorphic behavior.
29.
Which of the following statements about polymorphism in Python is correct?
Answer: Option
Explanation:
Polymorphism in Python is achieved through both method overloading and method overriding.
30.
What is the purpose of the
__add__()
method in Python classes in the context of polymorphism?
Answer: Option
Explanation:
The
__add__()
method is used to customize the behavior when an instance is added to another instance, allowing for polymorphic behavior.
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers