Exercise: Polymorphism
Questions for: Polymorphism
Which of the following statements about polymorphism in Python is correct?
A:
Polymorphism is achieved through method overloading only
B:
Polymorphism is achieved through method overriding only
C:
Polymorphism is achieved through both method overloading and overriding
D:
Polymorphism is achieved through operator overloading only
Answer: C
Polymorphism in Python is achieved through both method overloading and method overriding.
What is static polymorphism?
A:
The ability of a function to take different types of arguments
B:
The ability of an object to take on multiple forms or types at runtime
C:
The ability to define custom behavior for operators in a class
D:
The ability to resolve method calls at compile-time
Answer: D
Static polymorphism in Python, also known as compile-time polymorphism, involves resolving method calls at compile-time based on the method signatures.
Discuss About this Question.
What is the purpose of the
__getitem__() method in Python classes in the context of polymorphism?
A:
To define class attributes
B:
To customize the behavior when an instance is deleted
C:
To customize the behavior when an item is accessed using square brackets on an instance
D:
To create a new instance of the class
Answer: C
The
__getitem__() method is used to customize the behavior when an item is accessed using square brackets on an instance, allowing for polymorphic behavior.Discuss About this Question.
How does Python achieve polymorphism through method overloading?
A:
By allowing a function to take different types of arguments
B:
By automatically resolving conflicts using the first defined method
C:
By using the C3 linearization algorithm to determine method resolution order
D:
Python does not support method overloading
Answer: D
Python does not support method overloading in the traditional sense, where multiple methods with the same name have different parameters.
Discuss About this Question.
Consider the following code:
class Vehicle:
def drive(self):
pass
class Car(Vehicle):
def drive(self):
return "Car is driving"
class Bicycle(Vehicle):
def drive(self):
return "Bicycle is pedaling"
What concept is demonstrated in this code?
A:
Method overloading
B:
Method overriding
C:
Operator overloading
D:
Polymorphism
Answer: B
This code demonstrates method overriding, where the subclasses provide specific implementations for a method defined in the superclass.
Discuss About this Question.
Ad Slot (Above Pagination)
Discuss About this Question.