Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q5 of 15
Python - Methods and Behavior Definition
What will be the output of this code?
class Counter:
    def __init__(self):
        self.count = 0
    def increment(self):
        self.count += 1

c = Counter()
c.increment()
c.increment()
print(c.count)
A0
B1
CError
D2
Step-by-Step Solution
Solution:
  1. Step 1: Initial count value

    Counter object starts with count = 0.
  2. Step 2: Increment method calls

    Each increment call adds 1 to count. Two calls add 2 total.
  3. Step 3: Print final count

    Print outputs 2, the updated count value.
  4. Final Answer:

    2 -> Option D
  5. Quick Check:

    Increment method updates attribute correctly [OK]
Quick Trick: Each method call can change object state incrementally [OK]
Common Mistakes:
  • Forgetting to call increment method
  • Assuming count stays 0
  • Expecting error from += operation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes