Bird
0
0

What will be the output of the following code?

medium📝 Predict Output Q13 of 15
Python - Encapsulation and Data Protection
What will be the output of the following code?
class Person:
    def __init__(self, name):
        self._name = name

    @property
    def name(self):
        return self._name

    @name.setter
    def name(self, value):
        self._name = value.upper()

p = Person('alice')
p.name = 'bob'
print(p.name)
Abob
BError
Calice
DBOB
Step-by-Step Solution
Solution:
  1. Step 1: Understand setter behavior

    Setter converts the assigned value to uppercase before storing it.
  2. Step 2: Trace code execution

    Initially name is 'alice', then set to 'bob' which setter changes to 'BOB'. Printing returns 'BOB'.
  3. Final Answer:

    BOB -> Option D
  4. Quick Check:

    Setter uppercases value, output = BOB [OK]
Quick Trick: Setter modifies value before storing, output reflects change [OK]
Common Mistakes:
  • Expecting original lowercase 'bob'
  • Thinking print shows initial 'alice'
  • Assuming code raises error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes