Python Programming - Polymorphism

Exercise : Polymorphism - General Questions
  • Polymorphism - General Questions
41.
How is polymorphism related to the concept of "parametric polymorphism"?
Polymorphism in Python is based on explicit type declarations
"Parametric polymorphism" refers to the ability of objects to take on multiple forms, which is a form of polymorphism
Polymorphism in Python is achieved through static typing
"Parametric polymorphism" refers to defining functions or classes that can operate on different data types
Answer: Option
Explanation:
"Parametric polymorphism" in Python refers to defining functions or classes that can operate on different data types, contributing to polymorphic behavior.

42.
Which of the following is an example of polymorphism through method overloading?
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 a method with the same name in a class
Defining a method with different access modifiers in a class
Answer: Option
Explanation:
Polymorphism through method overloading in Python involves defining a method with the same name but different parameters in a class.

43.
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.

44.
What is the purpose of the __mul__() method in Python classes in the context of polymorphism?
To define class attributes
To customize the behavior when an instance is multiplied by another instance
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 __mul__() method is used to customize the behavior when an instance is multiplied by another instance, allowing for polymorphic behavior.

45.
Consider the following code:
class Guitar:
    def play(self):
        pass

class ElectricGuitar(Guitar):
    def play(self):
        return "Distorted sound!"

class AcousticGuitar(Guitar):
    def play(self):
        return "Harmonious sound!"
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.