Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q13 of 15
Python - Methods and Behavior Definition
What will be the output of this code?
class Box:
    def __init__(self):
        self.size = 5

    def enlarge(self):
        self.size += 3

b = Box()
b.enlarge()
print(b.size)
A5
B8
C3
DError
Step-by-Step Solution
Solution:
  1. Step 1: Initial attribute value

    The Box object b starts with size = 5 from __init__.
  2. Step 2: Method enlarge changes size

    Calling b.enlarge() adds 3 to size, so size becomes 5 + 3 = 8.
  3. Final Answer:

    8 -> Option B
  4. Quick Check:

    5 + 3 = 8 [OK]
Quick Trick: Add changes inside method to attribute value [OK]
Common Mistakes:
  • Forgetting method changes attribute
  • Thinking size resets after method call
  • Expecting error due to method call

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes