Python Programming - Inheritance
Exercise : Inheritance - General Questions
- Inheritance - General Questions
81.
What is the purpose of the
__str__() method in Python classes?
Answer: Option
Explanation:
The
__str__() method is used to provide a human-readable string representation of the object.
82.
Consider the following code:
class Animal:
def __init__(self, species):
self.species = species
class Cat(Animal):
def __init__(self, name, species):
super().__init__(species)
self.name = name
What is the primary purpose of using super().__init__(species) in the Cat class constructor?
Answer: Option
Explanation:
super().__init__(species) is used to call the constructor of the superclass Animal and initialize the species attribute.
83.
In Python, what is the purpose of the
@property decorator?
Answer: Option
Explanation:
The
@property decorator is used to define a method that can be accessed as a read-only attribute, providing a getter method.
84.
How can you achieve method overloading?
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.
85.
What is the purpose of the
__setattr__() method in Python classes?
Answer: Option
Explanation:
The
__setattr__() method is used to customize the behavior when an attribute is set in a class.
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers