Python Programming - Encapsulation

Exercise : Encapsulation - General Questions
  • Encapsulation - General Questions
11.
What is the purpose of the single underscore prefix in Python, as in _variable?
To indicate a protected variable
To indicate a private variable
To indicate a global variable
To indicate a constant variable
Answer: Option
Explanation:
The single underscore prefix in Python, as in _variable, is used to indicate a protected variable.

12.
In Python, what is a getter method used for in the context of encapsulation?
To set the value of a private variable
To delete a private variable
To retrieve the value of a private variable
To define a new private variable
Answer: Option
Explanation:
In encapsulation, a getter method is used to retrieve the value of a private variable.

13.
Which access specifier in Python is used to indicate that a variable or method should be accessible from anywhere, both within and outside the class?
Public
Private
Protected
Global
Answer: Option
Explanation:
Public access specifier in Python is used to indicate that a variable or method should be accessible from anywhere, both within and outside the class.

14.
What is the role of the @property decorator in encapsulation?
To define a private variable
To create a class instance
To provide a getter method for a private variable
To access a global variable
Answer: Option
Explanation:
The @property decorator is used in encapsulation to provide a getter method for a private variable, allowing controlled access.

15.
How does encapsulation contribute to the concept of information hiding in object-oriented programming?
By exposing all implementation details of an object
By combining data and methods into a single unit
By hiding the implementation details of an object and exposing only the necessary functionalities
By allowing unrestricted access to internal details of an object
Answer: Option
Explanation:
Encapsulation contributes to information hiding by hiding the implementation details of an object and exposing only the necessary functionalities, promoting abstraction.