Bird
Raised Fist0

What will be printed when the following code is executed?

medium📝 Predict Output Q4 of Q15
Python - Encapsulation and Data Protection
What will be printed when the following code is executed?
class Container:
    def __init__(self):
        self.__volume = 10
    def volume(self):
        return self.__volume
c = Container()
print(c.volume())
A0
BAttributeError
CNone
D10
Step-by-Step Solution
Solution:
  1. Step 1: Understand private attribute access

    The private attribute __volume is accessed inside the class method volume().
  2. Step 2: Calling the method

    Calling c.volume() returns the value of __volume, which is 10.
  3. Final Answer:

    10 -> Option D
  4. Quick Check:

    Private attributes accessible inside class methods [OK]
Quick Trick: Private attributes accessible via class methods [OK]
Common Mistakes:
MISTAKES
  • Expecting AttributeError when accessing via method
  • Confusing private attribute with inaccessible outside
  • Assuming default value is None or 0

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes