Python Programming - Classes
Exercise : Classes - General Questions
- Classes - General Questions
6.
What is the purpose of the
@classmethod
decorator?
Answer: Option
Explanation:
The
@classmethod
decorator in Python is used to define a class method. A class method takes the class as its first parameter, conventionally named cls
, allowing it to access and modify class-level attributes.
7.
What is the purpose of the
@staticmethod
decorator?
Answer: Option
Explanation:
The
@staticmethod
decorator is used to define a static method in a class. Static methods are not bound to an instance and can be called on the class itself without creating an object.
8.
In Python, what is the purpose of the
super()
function within a subclass method?
Answer: Option
Explanation:
The
super()
function is used in a subclass to call a method or constructor from its superclass. It is commonly used within the constructor (__init__
) to invoke the constructor of the parent class.
9.
What is encapsulation in the context of object-oriented programming?
Answer: Option
Explanation:
Encapsulation is one of the pillars of object-oriented programming and involves bundling the data (attributes) and the methods that operate on the data into a single unit. It helps in hiding the internal implementation details of an object and exposing only what is essential.
10.
What is the purpose of the
__del__
method?
Answer: Option
Explanation:
The
__del__
method is a special method in Python classes that is called when an object is about to be destroyed and its memory is released. It is often used to perform cleanup operations before an object is removed from memory.
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers