Python Programming - Encapsulation

Exercise : Encapsulation - General Questions
  • Encapsulation - General Questions
81.
Consider the following Python code:
class BankAccount:
    def __init__(self, balance):
        self._balance = balance

    def withdraw(self, amount):
        if amount <= self._balance:
            self._balance -= amount
            return amount
        else:
            return "Insufficient funds"
What encapsulation concept is demonstrated in this code?
Public access specifier
Private access specifier
Protected access specifier
Global variable
Answer: Option
Explanation:
The single underscore prefix before 'balance' indicates that it is a public variable, allowing access outside the class.

82.
In Python, what is the benefit of using a private variable with a double underscore prefix, such as __quantity?
class Inventory:
    __quantity = 0

    def update_quantity(self, quantity):
        Inventory.__quantity += quantity
It improves code maintainability by hiding implementation details
It allows unrestricted access to __quantity
It exposes all internal details of __quantity
It creates a global variable
Answer: Option
Explanation:
Encapsulation with a double underscore prefix improves code maintainability by hiding the implementation details of the class attribute __quantity.

83.
What is the primary purpose of the following Python class?
class Wallet:
    def __init__(self, owner, __balance):
        self.owner = owner
        self.__balance = __balance

    def get_balance(self):
        return self.__balance
To create a new instance of the class
To provide controlled access to the 'balance' variable
To define a public variable 'balance'
To allow unrestricted access to the 'balance' variable
Answer: Option
Explanation:
The get_balance() method provides controlled and read-only access to the private variable '__balance', demonstrating encapsulation.

84.
How can encapsulation be enforced in Python to make a variable 'account_number' accessible only within its own class?
class BankAccount:
    def __init__(self, account_number, __balance):
        self.account_number = account_number
        self.__balance = __balance
Use a getter method for 'account_number'
Add a single underscore prefix before 'account_number'
Make 'account_number' a global variable
Add a double underscore prefix before 'account_number'
Answer: Option
Explanation:
Adding a double underscore prefix before 'account_number' makes it a private variable, enforcing encapsulation within the class.

85.
Consider the following Python code:
class Temperature:
    def __init__(self, __celsius):
        self.__celsius = __celsius

    def to_fahrenheit(self):
        return (self.__celsius * 9/5) + 32
What is the purpose of the to_fahrenheit() method?
To retrieve the temperature in Fahrenheit
To set a new temperature in Fahrenheit
To convert the temperature to Fahrenheit
To expose all internal details of the class
Answer: Option
Explanation:
The to_fahrenheit() method converts the temperature from Celsius to Fahrenheit, demonstrating encapsulation.