Bird
0
0

What is the output of this code?

medium📝 Predict Output Q4 of 15
Python - Methods and Behavior Definition
What is the output of this code?
class Car:
    def __init__(self):
        self.speed = 0

car = Car()
car.speed = 50
print(car.speed)
A0
B50
CNone
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand initial attribute value

    The Car object starts with speed = 0 from __init__.
  2. Step 2: Attribute modification before print

    car.speed is set to 50, changing the attribute value.
  3. Step 3: Print statement output

    Printing car.speed outputs the updated value 50.
  4. Final Answer:

    50 -> Option B
  5. Quick Check:

    Modified attribute prints updated value [OK]
Quick Trick: Changing attribute before print shows new value [OK]
Common Mistakes:
  • Assuming initial value prints despite modification
  • Thinking print shows None if changed
  • Expecting error from attribute change

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes