Bird
Raised Fist0

What will be the output of this code?

medium📝 Predict Output Q13 of Q15
Python - Encapsulation and Data Protection
What will be the output of this code?
class MyClass:
    def __init__(self):
        self._value = 10

obj = MyClass()
print(obj._value)
AAttributeError
BNone
C10
DSyntaxError
Step-by-Step Solution
Solution:
  1. Step 1: Understand protected attribute access

    Protected attributes can be accessed outside the class, though it is discouraged.
  2. Step 2: Check code behavior

    The attribute _value is set to 10 and printed directly, so output is 10.
  3. Final Answer:

    10 -> Option C
  4. Quick Check:

    Protected attribute accessible outside class = 10 [OK]
Quick Trick: Protected attributes can be read outside class [OK]
Common Mistakes:
MISTAKES
  • Expecting AttributeError when accessing protected attribute
  • Confusing protected with private attributes
  • Thinking protected attributes are hidden

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes