Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q4 of 15
Python - Classes and Object Lifecycle
What will be the output of this code?
class Cat:
    def __init__(self):
        self.sound = 'meow'

c = Cat()
print(c.sound)
c.sound = 'purr'
print(c.sound)
AError at assignment
Bmeow purr
Cpurr purr
Dmeow meow
Step-by-Step Solution
Solution:
  1. Step 1: Understand initial attribute value

    The constructor sets self.sound to 'meow', so c.sound initially prints 'meow'.
  2. Step 2: Attribute modification

    After assignment c.sound = 'purr', the attribute value changes, so printing again outputs 'purr'.
  3. Final Answer:

    meow purr -> Option B
  4. Quick Check:

    Attribute change reflected in output = meow purr [OK]
Quick Trick: Changing attribute updates its value immediately [OK]
Common Mistakes:
  • Assuming attribute is immutable after init
  • Expecting error on attribute reassignment
  • Confusing print outputs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes