Python Programming - Polymorphism

Exercise : Polymorphism - General Questions
  • Polymorphism - General Questions
16.
What is static polymorphism?
The ability of a function to take different types of arguments
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
The ability to resolve method calls at compile-time
Answer: Option
Explanation:
Static polymorphism in Python, also known as compile-time polymorphism, involves resolving method calls at compile-time based on the method signatures.

17.
Which of the following statements about polymorphism in Python is correct?
Polymorphism is achieved through method overloading only
Polymorphism is achieved through method overriding only
Polymorphism is achieved through both method overloading and overriding
Polymorphism is achieved through operator overloading only
Answer: Option
Explanation:
Polymorphism in Python is achieved through both method overloading and method overriding.

18.
Consider the following code:
class Shape:
    def area(self):
        pass

class Triangle(Shape):
    def area(self, base, height):
        return 0.5 * base * height

class Square(Shape):
    def area(self, side):
        return side * side
What concept is demonstrated in this code?
Operator overloading
Method overloading
Method overriding
Polymorphism
Answer: Option
Explanation:
This code demonstrates method overloading, where the subclass methods have the same name but different parameters than the superclass method.

19.
What is the purpose of the __call__() method in Python classes in the context of polymorphism?
To create a new instance of the class
To customize the behavior when an instance is called as a function
To customize the behavior when an instance is deleted
To access superclass attributes directly
Answer: Option
Explanation:
The __call__() method is used to customize the behavior when an instance is called as a function, allowing for polymorphic behavior.

20.
Which of the following is an example of polymorphism through operator overloading?
Using the + operator to concatenate strings
Using the + operator to add two numbers
Using the + operator to access a list element
Using the + operator to define a custom behavior in a class
Answer: Option
Explanation:
Polymorphism through operator overloading in Python involves defining custom behavior for operators in a class, such as using __add__() for the + operator.