Python Programming - Polymorphism

Exercise : Polymorphism - General Questions
  • Polymorphism - General Questions
31.
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.

32.
Consider the following code:
class Animal:
    def sound(self):
        pass

class Dog(Animal):
    def sound(self):
        return "Woof!"

class Cat(Animal):
    def sound(self):
        return "Meow!"
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.

33.
How is polymorphism related to the concept of "duck typing"?
Polymorphism in Python is based on the idea of explicitly specifying data types
"Duck typing" 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
"Duck typing" refers to the use of explicit type declarations in Python
Answer: Option
Explanation:
"Duck typing" in Python refers to the ability of objects to take on multiple forms or types based on their behavior, aligning with the concept of polymorphism.

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

35.
In Python, what is operator overloading?
Defining multiple methods with the same name but different parameters in a class
Overriding a method in a subclass with the same name as in the superclass
Defining custom behavior for operators in a class
Defining a method with the same name but different return types in a class
Answer: Option
Explanation:
Operator overloading in Python involves defining custom behavior for operators in a class, such as using methods like __add__() for the + operator.