Bird
0
0

Identify the problem in this code:

medium📝 Debug Q7 of 15
Python - Classes and Object Lifecycle
Identify the problem in this code:
class Tree:
    height = 10

t = Tree()
print(t.height)
Tree.height = 15
print(t.height)
AOutput will be 10 then 10, class attribute change ignored
BError because instance attribute height is missing
CError because class attribute cannot be changed
DOutput will be 10 then 15, no problem
Step-by-Step Solution
Solution:
  1. Step 1: Understand attribute access

    Initially, t.height accesses class attribute 10.
  2. Step 2: Class attribute update effect

    Changing Tree.height to 15 updates class attribute; t.height reflects this change.
  3. Final Answer:

    Output will be 10 then 15, no problem -> Option D
  4. Quick Check:

    Class attribute changes reflect in instances without instance override [OK]
Quick Trick: Instance uses updated class attribute if no instance attribute exists [OK]
Common Mistakes:
  • Expecting errors on class attribute change
  • Thinking instance attribute must exist
  • Assuming old value prints twice

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes