Python Programming - Polymorphism
Exercise : Polymorphism - General Questions
- Polymorphism - General Questions
6.
What is operator overloading?
Answer: Option
Explanation:
Operator overloading in Python allows you to define custom behavior for operators in a class by implementing special methods like
__add__()
, __sub__()
, etc.
7.
Which of the following is an example of method overloading?
Answer: Option
Explanation:
Method overloading in Python is achieved by defining a method with the same name but different parameters in a class.
8.
Consider the following code:
class Animal:
def speak(self):
pass
class Dog(Animal):
def speak(self):
return "Woof!"
class Cat(Animal):
def speak(self):
return "Meow!"
What concept is demonstrated in this code?
Answer: Option
Explanation:
This code demonstrates method overriding, where the subclasses provide specific implementations for a method defined in the superclass.
9.
How is polymorphism different from encapsulation?
Answer: Option
Explanation:
Polymorphism is about the ability of objects to take on multiple forms or types, while encapsulation is about bundling data and methods within a single unit and controlling access to details.
10.
What is the purpose of the
__iter__()
method in Python classes in the context of polymorphism?
Answer: Option
Explanation:
The
__iter__()
method is used to customize the behavior when the iter()
function is called on an instance of the class, allowing for polymorphic behavior.
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers