0
0
LLDsystem_design~10 mins

Encapsulation and information hiding in LLD - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a private attribute in a class.

LLD
class User:
    def __init__(self, name):
        self.[1]name = name
Drag options to blanks, or click blank then click option'
A__
B_
Cpublic_
Dprivate_
Attempts:
3 left
💡 Hint
Common Mistakes
Using a single underscore only signals 'protected' but does not enforce privacy.
Using 'public_' or 'private_' prefixes are not Python conventions.
2fill in blank
medium

Complete the code to provide controlled access to a private attribute using a getter method.

LLD
class Account:
    def __init__(self, balance):
        self.__balance = balance
    def get_[1](self):
        return self.__balance
Drag options to blanks, or click blank then click option'
Aamount
Bbalance
Cvalue
Dmoney
Attempts:
3 left
💡 Hint
Common Mistakes
Naming the getter method differently from the attribute can confuse users.
Trying to access the private attribute directly instead of via getter.
3fill in blank
hard

Fix the error in the setter method to update the private attribute safely.

LLD
class Employee:
    def __init__(self, salary):
        self.__salary = salary
    def set_salary(self, amount):
        if amount > 0:
            self.[1]salary = amount
Drag options to blanks, or click blank then click option'
Apublic_
B_
Cprivate_
D__
Attempts:
3 left
💡 Hint
Common Mistakes
Using a single underscore or no underscore causes the setter to create a new attribute instead of updating the private one.
Using 'public_' or 'private_' prefixes is incorrect.
4fill in blank
hard

Fill both blanks to implement a property decorator for controlled access to a private attribute.

LLD
class Product:
    def __init__(self, price):
        self.__price = price

    @property
    def [1](self):
        return self.__price

    @[2].setter
    def price(self, value):
        if value >= 0:
            self.__price = value
Drag options to blanks, or click blank then click option'
Aprice
Ccost
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for getter and setter breaks the property.
Forgetting the @property decorator on the getter.
5fill in blank
hard

Fill all three blanks to implement encapsulation with private attribute, getter, and setter using property decorators.

LLD
class BankAccount:
    def __init__(self, balance):
        self.[1]balance = balance

    @property
    def [2](self):
        return self.__balance

    @[3].setter
    def balance(self, amount):
        if amount >= 0:
            self.__balance = amount
Drag options to blanks, or click blank then click option'
A__
Bbalance
D_
Attempts:
3 left
💡 Hint
Common Mistakes
Using single underscore for private attribute weakens encapsulation.
Getter and setter names not matching property name causes errors.