Learn with Yasir

Share Your Feedback

Python Encapsulation Fill-in-the-Blanks Quiz – Practice OOP Concepts


Enhance your Python OOP skills with this fill-in-the-blanks quiz on encapsulation. Ideal for students, beginners, and those preparing for interviews. Learn with Yasir Bhutta and reinforce your understanding of access modifiers, getter/setter methods, and more.

🔍 Fill in the Blanks

🟢 Beginner

  1. In Python, using a single underscore prefix (e.g., `_variable`) indicates a ________ variable by convention.
  2. Answer

    protected

    A single underscore suggests the variable is for internal use, but Python doesn't enforce this restriction.


  3. Double underscore prefix (e.g., `__variable`) triggers name ________ in Python.
  4. Answer

    mangling

    Name mangling changes the variable name to `_ClassName__variable` to avoid naming conflicts in subclasses.


  5. Encapsulation helps ________ the internal state of objects from direct external access.
  6. Answer

    protect

    Encapsulation prevents direct access to an object's internal state, requiring controlled access through methods.


  7. A ________ method is used to modify the value of a private attribute with validation.
  8. Answer

    setter

    Setter methods allow controlled modification of private attributes with validation logic.


🟡 Intermediate

  1. The `@property` decorator allows you to define a ________ method to control attribute access.
  2. Answer

    getter

    The `@property` decorator creates a getter method that's called when accessing the attribute.


  3. To create a setter for a property named 'age', you would use the `@________.setter` decorator.
  4. Answer

    age

    The setter decorator must match the property name (e.g., `@age.setter` for a property named 'age').


  5. The Python built-in function ________ can check if an object has a particular attribute.
  6. Answer

    hasattr()

    `hasattr(obj, 'attribute')` returns True if the object has the specified attribute.


  7. The ________ decorator allows you to define read-only properties in Python.
  8. Answer

    @property

    Using `@property` without a setter creates a read-only attribute.


🔴 Advanced

  1. To access a name-mangled variable `__var` from class `Person` outside the class, you would use `_Person________`.
  2. Answer

    __var

    Name-mangled variables can be accessed externally using `_ClassName__variablename` format.


  3. Encapsulation is one of the four OOP principles, along with inheritance, polymorphism, and ________.
  4. Answer

    abstraction

    The four main OOP principles are encapsulation, abstraction, inheritance, and polymorphism.



🧠 Example-Based Fill in the Blanks

  1. class BankAccount:
        def __init__(self, balance):
            self._balance = balance
    
        def get_balance(self):
            return self._balance
    

    ✍️ The underscore prefix in `_balance` indicates this is a ________ variable.

    Answer

    protected

    A single underscore is a naming convention indicating protected (internal-use) members.

  2. class Temperature:
        def __init__(self):
            self.__celsius = 0
    
        @property
        def celsius(self):
            return self.__celsius
    
        @celsius.setter
        def celsius(self, value):
            if value < -273.15:
                raise ValueError("Temperature too low")
            self.__celsius = value
    

    ✍️ The `@property` decorator allows ________ access to the private `__celsius` attribute.

    Answer

    controlled

    Properties provide controlled access to private attributes with validation logic.

  3. class Student:
        def __init__(self, name):
            self.__name = name
    
        def get_name(self):
            return self.__name
        
        def set_name(self, new_name):
            if len(new_name) < 2:
                raise ValueError("Name too short")
            self.__name = new_name
    

    ✍️ This example demonstrates encapsulation by using ________ to access private attributes.

    Answer

    getter/setter methods

    Getter/setter methods enforce validation while hiding implementation details.

  4. class SecretVault:
        def __init__(self):
            self.__password = "12345"
    
    vault = SecretVault()
    # This will print the mangled name
    print(dir(vault))
    

    ✍️ The double underscores in `__password` trigger name ________.

    Answer

    mangling

    Name mangling changes the attribute name to `_SecretVault__password`.

  5. class Smartphone:
        def __init__(self):
            self.__imei = "1234567890"
        
        @property
        def imei(self):
            return "ACCESS DENIED"
    

    ✍️ This property makes the `imei` attribute ________.

    Answer

    read-only

    By not providing a setter, the property becomes read-only.

  6. class Employee:
        def __init__(self, salary):
            self._salary = salary
    
        def _calculate_bonus(self):
            return self._salary * 0.1
    

    ✍️ Both the `_salary` attribute and `_calculate_bonus` method use the ________ convention.

    Answer

    protected

    Single underscore prefix indicates protected members (internal API).

  7. class GameCharacter:
        def __init__(self):
            self.__health = 100
        
        def take_damage(self, amount):
            self.__health = max(0, self.__health - amount)
    

    ✍️ The `__health` attribute is ________ from direct external access.

    Answer

    hidden

    Double underscore prefix makes it harder to access directly (name mangling).


📚 Related Resources


🧠 Practice & Progress

Explore More Topics

Python Fundamentals

Flow Control Statements


Python Functions


Fundamentals more ...




🧠 Python Advanced

Object-Oriented Programming in Python (OOP)

More...

🧠 Modules