Python Programming - Polymorphism

Exercise : Polymorphism - General Questions
  • Polymorphism - General Questions
1.
In Python, what is polymorphism?
The ability of a class to inherit from multiple classes
The ability of a function to take different types of arguments
The ability of an object to take on multiple forms or types
The ability to create instances of a class
Answer: Option
Explanation:
Polymorphism in Python refers to the ability of an object to take on multiple forms or types.

2.
Which of the following statements about method overloading in Python is correct?
Python supports method overloading with different return types
Python supports method overloading with the same method name but different parameters
Method overloading is not allowed in Python
Python supports method overloading by explicitly specifying the data type of parameters
Answer: Option
Explanation:
Python does not support method overloading in the traditional sense as it is done in languages like Java. Methods with the same name in a class will override each other.

3.
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?
Operator overloading
Method overloading
Method overriding
Polymorphism
Answer: Option
Explanation:
This example illustrates method overriding, where a subclass provides a specific implementation for a method that is already defined in its superclass.

4.
How is polymorphism achieved?
Through method overloading
Through method overriding
Through both method overloading and overriding
Through the use of classes and inheritance
Answer: Option
Explanation:
Polymorphism in Python is achieved through both method overloading (not in the traditional sense) and method overriding.

5.
What is the purpose of the __len__() method in Python classes in the context of polymorphism?
To define class attributes
To customize the behavior when the len() function is called on an instance
To access superclass attributes directly
To create a new instance of the class
Answer: Option
Explanation:
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.