Python Programming - Encapsulation

Exercise : Encapsulation - General Questions
  • Encapsulation - General Questions
46.
What does the term "encapsulation" mean in the context of object-oriented programming?
Combining multiple classes into one
Hiding the implementation details of an object and bundling data and methods into a single unit
Inheriting attributes from multiple classes
Exposing all internal details of an object
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?
Public
Private
Protected
Global
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?
Increased code complexity
Improved code maintainability
Reduced data security
Unrestricted access to internal details
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?
To set the value of a private variable
To retrieve the value of a private variable
To create a new instance of a class
To delete a private variable
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?
To set the salary of an employee
To delete the salary of an employee
To retrieve the salary of an employee
To create a new instance of the class
Answer: Option
Explanation:
The get_salary() method is designed to retrieve the salary of an employee, demonstrating encapsulation by providing controlled access.