Learn with Yasir

Share Your Feedback

Encapsulation in Python – True or False Questions with Answers


Test your understanding of Object-Oriented Programming (OOP) in Python with these carefully crafted True or False questions on encapsulation. Great for beginners and intermediate learners to reinforce OOP concepts.

🔍 True or False

🟢 Beginner

  1. In Python, encapsulation is achieved only by using private variables (names starting with `__`).
  2. Answer

    ❌ False – Encapsulation can also be achieved using protected variables (`_`) and getter/setter methods, not just strict private variables.


  3. A class attribute marked with a single underscore (e.g., `_variable`) is treated as private and cannot be accessed outside the class.
  4. Answer

    ❌ False – A single underscore is a convention for "protected" members, but Python does not enforce access restrictions—it’s still accessible.


  5. Encapsulation helps prevent accidental modification of internal class data.
  6. Answer

    ✅ True – Encapsulation restricts direct access to data, reducing unintended changes.


  7. In Python, all attributes are public by default unless explicitly marked private.
  8. Answer

    ✅ True – Python does not enforce strict private members; privacy is convention-based (`_` and `__`).


  9. Encapsulation is only useful for security purposes.
  10. Answer

    ❌ False – Encapsulation also improves maintainability, reduces bugs, and helps in modular design—not just security.


🟡 Intermediate

  1. Name mangling (e.g., `__variable`) in Python makes an attribute completely inaccessible from outside the class.
  2. Answer

    ❌ False – Name mangling (`__var`) makes it harder to access, but it can still be accessed using `_ClassName__var`.


  3. Getter and setter methods are necessary in Python to enforce encapsulation.
  4. Answer

    ❌ False – Python properties (`@property`) can be used to control access without explicit getter/setter methods.


  5. A class that exposes all its attributes directly (without getters/setters) violates encapsulation.
  6. Answer

    ✅ True – Encapsulation is about controlling access to data, and exposing all attributes breaks this principle.


  7. The `@property` decorator in Python helps maintain encapsulation by allowing controlled attribute access.
  8. Answer

    ✅ True – `@property` lets you add logic when getting/setting attributes while keeping the interface clean.


🔴 Advanced

  1. A subclass cannot access the private attributes (with `__`) of its parent class.
  2. Answer

    ✅ True – Due to name mangling, a subclass cannot directly access `__var` from a parent, but it can via `_ParentClass__var`.


📚 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