Exercise: Polymorphism
Questions for: Polymorphism
Which of the following is an example of method overloading?
A:
Defining a method with the same name in a class
B:
Defining a method with the same name but different parameters in a class
C:
Defining a method with the same name but different return types in a class
D:
Defining a method with different access modifiers in a class
Answer: B
Method overloading in Python is achieved by defining a method with the same name but different parameters in a class.
What is operator overloading?
A:
The ability of a function to take different types of arguments
B:
The ability of a class to inherit from multiple classes
C:
The ability of an object to take on multiple forms or types
D:
The ability to define custom behavior for operators in a class
Answer: D
Operator overloading in Python allows you to define custom behavior for operators in a class by implementing special methods like
__add__(), __sub__(), etc.Discuss About this Question.
What is the purpose of the
__len__() method in Python classes in the context of polymorphism?
A:
To define class attributes
B:
To customize the behavior when the
len() function is called on an instance
C:
To access superclass attributes directly
D:
To create a new instance of the class
Answer: B
The
__len__() method is used to customize the behavior when the len() function is called on an instance of the class, allowing for polymorphic behavior.Discuss About this Question.
How is polymorphism achieved?
A:
Through method overloading
B:
Through method overriding
C:
Through both method overloading and overriding
D:
Through the use of classes and inheritance
Answer: C
Polymorphism in Python is achieved through both method overloading (not in the traditional sense) and method overriding.
Discuss About this Question.
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 is this example illustrating?
A:
Operator overloading
B:
Method overloading
C:
Method overriding
D:
Polymorphism
Answer: C
This example illustrates method overriding, where a subclass provides a specific implementation for a method that is already defined in its superclass.
Discuss About this Question.
Ad Slot (Above Pagination)
Discuss About this Question.