Learn with Yasir

Share Your Feedback

Python Encapsulation MCQs – Test Your OOP Knowledge


Challenge your understanding of Python's Object-Oriented Programming with these multiple-choice questions on encapsulation. Ideal for beginners, students, and job seekers to reinforce key OOP concepts.

📝 Multiple Choice Questions

🟢 Beginner

Q1. What is encapsulation in Python OOP?

  • 🟢 A. A way to hide internal implementation details and expose only necessary features
  • 🔵 B. A method to combine multiple functions into one
  • 🟠 C. A technique to make code run faster
  • 🔴 D. A way to remove unused variables
Answer

A way to hide internal implementation details and expose only necessary features

Encapsulation helps in restricting access to certain components and prevents unintended modifications.


Q2. How do you make an attribute private in Python?

  • 🟢 A. By using the `private` keyword
  • 🔵 B. By prefixing the attribute name with a single underscore `_`
  • 🟠 C. By prefixing the attribute name with double underscores `__`
  • 🔴 D. By using the `hidden` keyword
Answer

By prefixing the attribute name with double underscores `__`

In Python, double underscores `__` make an attribute name-mangled, restricting direct access.


Q3. What is the purpose of getter and setter methods in encapsulation?

  • 🟢 A. To directly modify private attributes
  • 🔵 B. To control access and modification of private attributes
  • 🟠 C. To delete unused variables
  • 🔴 D. To improve code execution speed
Answer

To control access and modification of private attributes

Getters and setters allow controlled access to private attributes, enabling validation or modification checks.


Q4. What is the output of the following code?

class BankAccount:
    def __init__(self):
        self.__balance = 100

    def get_balance(self):
        return self.__balance

acc = BankAccount()
print(acc.__balance)
  • 🟢 A. 100
  • 🔵 B. Error: AttributeError (private attribute access)
  • 🟠 C. None
  • 🔴 D. 0
Answer

Error: AttributeError (private attribute access)

Double underscores make `__balance` private, so direct access outside the class raises an error.


Q5. What is the correct way to access a private attribute from outside its class?

  • 🟢 A. By using the `public` keyword
  • 🔵 B. By calling `object._attribute`
  • 🟠 C. By using a getter method
  • 🔴 D. By using `object.__attribute`
Answer

By using a getter method

The safest way is to use a getter method (e.g., `get_balance()`) instead of direct access.


Q6. What is the purpose of the single underscore `_` before a variable name?

  • 🟢 A. It makes the variable private
  • 🔵 B. It indicates that the variable is for internal use (convention)
  • 🟠 C. It makes the variable global
  • 🔴 D. It prevents the variable from being modified
Answer

It indicates that the variable is for internal use (convention).

A single underscore is a naming convention to suggest that a variable is for internal use (not enforced by Python). It should be accessed only within the class or its subclasses.


Q7. What is the output of the following code?

class Student:
    def __init__(self):
        self.__name = "Alice"

s = Student()
print(s.__name)
  • 🟢 A. Alice
  • 🔵 B. None
  • 🟠 C. Error: AttributeError (private attribute access)
  • 🔴 D. '' (empty string)
Answer

Error: AttributeError (private attribute access)

Private attributes (prefixed with `__`) cannot be accessed directly outside the class.


Q8. What is the output of the following code?

class Student:
    def __init__(self):
        self.__name = "Alice"
    
    def get_name(self):
        return self.__name

s = Student()
print(s.get_name())
  • 🟢 A. Alice
  • 🔵 B. Error: Method not defined
  • 🟠 C. None
  • 🔴 D. '' (empty string)
Answer

Alice

The `get_name()` method provides controlled access to the private `__name` attribute.


Q9. What is the output of the following code?

class Temperature:
    def __init__(self):
        self.__celsius = 30
    
    def get_fahrenheit(self):
        return (self.__celsius * 9/5) + 32

t = Temperature()
print(t.get_fahrenheit())
  • 🟢 A. 30
  • 🔵 B. 86
  • 🟠 C. Error: AttributeError
  • 🔴 D. None
Answer

86

The private `__celsius` is used internally to compute Fahrenheit (30°C = 86°F).


🟡 Intermediate

Q1. Which of the following is true about name mangling in Python?

  • 🟢 A. It makes an attribute completely inaccessible
  • 🔵 B. It renames the attribute to `_ClassName__attribute`
  • 🟠 C. It deletes the attribute after use
  • 🔴 D. It is done using the `@mangle` decorator
Answer

It renames the attribute to `_ClassName__attribute`

Name mangling changes the attribute name to `_ClassName__attribute` to avoid accidental access.


Q2. Which keyword is used to define a read-only property in Python?

  • 🟢 A. `@readonly`
  • 🔵 B. `@property`
  • 🟠 C. `@getter`
  • 🔴 D. `@final`
Answer

`@property`

The `@property` decorator allows defining a method as a read-only attribute.


Q3. What is the output of the following code?

class Account:
    def __init__(self):
        self.__balance = 0
    
    def deposit(self, amount):
        self.__balance += amount
    
    def withdraw(self, amount):
        if amount <= self.__balance:
            self.__balance -= amount
        else:
            print("Insufficient funds!")
    
    def get_balance(self):
        return self.__balance

acc = Account()
acc.deposit(100)
acc.withdraw(30)
print(acc.get_balance())
  • 🟢 A. 0
  • 🔵 B. 70
  • 🟠 C. 100
  • 🔴 D. Error
Answer

70

The private `__balance` is modified only via methods (`deposit()` and `withdraw()`).


Q4. What is the output of the following code?

class Person:
    def __init__(self):
        self.__age = 25
    
    @property
    def age(self):
        return self.__age
    
    @age.setter
    def age(self, value):
        if value > 0:
            self.__age = value
        else:
            print("Invalid age!")

p = Person()
p.age = -5
print(p.age)
  • 🟢 A. -5
  • 🔵 B. 25
  • 🟠 C. 0
  • 🔴 D. Invalid age!
Answer

25

The setter rejects negative values, so `__age` remains unchanged (25).


Q5. What is the output of the following code?

class Car:
    def __init__(self):
        self.__speed = 0
    
    def accelerate(self):
        self.__speed += 10
    
    def brake(self):
        self.__speed = max(0, self.__speed - 10)
    
    def get_speed(self):
        return self.__speed

c = Car()
c.accelerate()  # Typo in method name
print(c.get_speed())
  • 🟢 A. 0
  • 🔵 B. 10
  • 🟠 C. Error: AttributeError
  • 🔴 D. Error: Method not found
Answer

Error: Method not found

The typo `accelerate()` → `accelerate()` causes an `AttributeError`.


🔴 Advanced

Q1. What is the output of the following code?

class Secret:
    def __init__(self):
        self.__key = "1234"
    
    def _get_key(self):
        return self.__key[::-1]  # Reversed

s = Secret()
print(s._get_key())
  • 🟢 A. 1234
  • 🔵 B. 4321
  • 🟠 C. Error: Private method access
  • 🔴 D. None
Answer

4321

Single underscore `_get_key()` is



🧠 Practice & Progress

Explore More Topics

Python Fundamentals

Flow Control Statements


Python Functions


Fundamentals more ...




🧠 Python Advanced

Object-Oriented Programming in Python (OOP)

More...

🧠 Modules