Python Programming - Encapsulation
Exercise : Encapsulation - General Questions
- Encapsulation - General Questions
21.
What is the primary purpose of encapsulation?
Answer: Option
Explanation:
The primary purpose of encapsulation in Python is to combine data and methods into a single unit, promoting modularity.
22.
Which access specifier in Python is used to indicate that a variable or method should not be accessed from outside the class?
Answer: Option
Explanation:
Private access specifier in Python is used to indicate that a variable or method should not be accessed from outside the class.
23.
How does encapsulation contribute to code maintainability in object-oriented programming?
Answer: Option
Explanation:
Encapsulation contributes to code maintainability by combining data and methods into a single unit, making the code more modular and easier to manage.
24.
What is the role of the
@classmethod
decorator in encapsulation?
Answer: Option
Explanation:
The
@classmethod
decorator is used in encapsulation to provide a class method for a class, allowing functionality related to the class.
25.
Consider the following Python code:
class Car:
def __init__(self, model, year):
self.__model = model
self.__year = year
def display_info(self):
return f"Model: {self.__model}, Year: {self.__year}"
What concept of encapsulation is demonstrated in this code?Answer: Option
Explanation:
The double underscore prefix (
__
) in the variables __model
and __year
indicates that they are private variables, demonstrating encapsulation by hiding the implementation details.
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers