Bird
0
0

Given this code, what will print(obj._Outer__Inner__val) output?

hard📝 Application Q9 of 15
Python - Encapsulation and Data Protection
Given this code, what will print(obj._Outer__Inner__val) output?
class Outer:
    class __Inner:
        def __init__(self):
            self.__val = 99
    def __init__(self):
        self.__Inner = Outer.__Inner()
        self.__Inner.__val = 100
obj = Outer()
A100
BAttributeError
C99
DNone
Step-by-Step Solution
Solution:
  1. Step 1: Understand nested class mangling

    __Inner is mangled to _Outer__Inner; __val inside Inner is mangled to _Inner__val.
  2. Step 2: Check attribute access

    obj has _Outer__Inner attribute, but _Outer__Inner__val does not exist because __val is mangled inside Inner, not Outer.
  3. Final Answer:

    AttributeError -> Option B
  4. Quick Check:

    Nested mangling requires correct class names for access [OK]
Quick Trick: Nested mangling uses each class name separately [OK]
Common Mistakes:
  • Assuming combined mangling with both class names
  • Expecting direct access to nested mangled attribute
  • Ignoring separate mangling for nested classes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes