Python Programming - Polymorphism

Exercise : Polymorphism - General Questions
  • Polymorphism - General Questions
11.
What is dynamic polymorphism?
The ability of a function to take different types of arguments
The ability of a class to inherit from multiple classes
The ability of an object to take on multiple forms or types at runtime
The ability to define custom behavior for operators in a class
Answer: Option
Explanation:
Dynamic polymorphism in Python refers to the ability of an object to take on multiple forms or types at runtime, often achieved through method overriding.

12.
Which of the following is an example of operator overloading?
Defining a method with the same name in a class
Defining a method with the same name but different parameters in a class
Defining a method with the same name but different return types in a class
Defining the __add__() method in a class
Answer: Option
Explanation:
Operator overloading in Python involves defining methods like __add__() to provide custom behavior for operators.

13.
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?
Method overloading
Method overriding
Operator overloading
Polymorphism
Answer: Option
Explanation:
This code demonstrates method overriding, where the subclasses provide specific implementations for a method defined in the superclass.

14.
How does Python achieve polymorphism through method overloading?
By allowing a function to take different types of arguments
By automatically resolving conflicts using the first defined method
By using the C3 linearization algorithm to determine method resolution order
Python does not support method overloading
Answer: Option
Explanation:
Python does not support method overloading in the traditional sense, where multiple methods with the same name have different parameters.

15.
What is the purpose of the __getitem__() method in Python classes in the context of polymorphism?
To define class attributes
To customize the behavior when an instance is deleted
To customize the behavior when an item is accessed using square brackets on an instance
To create a new instance of the class
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.