Python Programming - Encapsulation

Exercise : Encapsulation - General Questions
  • Encapsulation - General Questions
31.
What is the purpose of the @property decorator in encapsulation?
To create a new instance of a class
To provide a getter method for a private variable
To access a global variable
To define a private variable
Answer: Option
Explanation:
The @property decorator in encapsulation is used to provide a getter method for a private variable, allowing controlled access.

32.
In Python, what is a potential drawback of using encapsulation?
Reduced code modularity
Increased code complexity
Enhanced data security
Improved code readability
Answer: Option
Explanation:
A potential drawback of using encapsulation in Python is increased code complexity, especially if not managed properly.

33.
Which of the following is a common guideline when using encapsulation?
Avoid using private variables altogether.
Make all variables public for ease of access.
Provide getter and setter methods for private variables as needed.
Use global variables extensively.
Answer: Option
Explanation:
A common guideline when using encapsulation is to provide getter and setter methods for private variables as needed to control access.

34.
How does encapsulation contribute to code maintenance?
By exposing all implementation details of an object
By combining data and methods into a single unit
By allowing unrestricted access to internal details of an object
By hiding the implementation details of an object
Answer: Option
Explanation:
Encapsulation contributes to code maintenance in Python by combining data and methods into a single unit, making the code more modular and easier to manage.

35.
Consider the following Python code:
class Product:
    def __init__(self, name, price):
        self._name = name
        self._price = price

    @property
    def get_price(self):
        return self._price
What is the purpose of the @property decorator in this code?
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 in this code is used to provide a getter method for the private variable _price.