Python Programming - Encapsulation
Exercise : Encapsulation - General Questions
- Encapsulation - General Questions
46.
What does the term "encapsulation" mean in the context of object-oriented programming?
Answer: Option
Explanation:
Encapsulation in Python involves hiding the implementation details of an object and bundling data and methods into a single unit for better control.
47.
Which of the following access specifiers in Python is used to indicate that a variable or method should be accessible only within its own class and not from outside?
Answer: Option
Explanation:
The private access specifier in Python (indicated by a single underscore or double underscore prefix) restricts access to variables and methods only within their own class.
48.
What is a potential advantage of using encapsulation?
Answer: Option
Explanation:
Encapsulation in Python can lead to improved code maintainability by organizing code into a more modular and understandable structure.
49.
In Python, what is the role of a getter method in encapsulation?
Answer: Option
Explanation:
A getter method in encapsulation is used to retrieve the value of a private variable.
50.
Consider the following Python code:
class Employee:
def __init__(self, name, salary):
self._name = name
self._salary = salary
def get_salary(self):
return self._salary
What is the purpose of the get_salary()
method in this code?
Answer: Option
Explanation:
The
get_salary()
method is designed to retrieve the salary of an employee, demonstrating encapsulation by providing controlled access.
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers