Bird
Raised Fist0

What will be the output of this code?

medium📝 Predict Output Q13 of Q15
Python - Constructors and Object Initialization
What will be the output of this code?
class Car:
    def __init__(self, brand):
        self.brand = brand

my_car = Car('Toyota')
print(my_car.brand)
ACar
BToyota
Cbrand
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand object creation

    Creating my_car = Car('Toyota') calls __init__ with 'Toyota' as brand.
  2. Step 2: Check attribute assignment and print

    The brand attribute of my_car is set to 'Toyota', so printing my_car.brand outputs 'Toyota'.
  3. Final Answer:

    Toyota -> Option B
  4. Quick Check:

    Attribute value prints 'Toyota' [OK]
Quick Trick: Print attribute after init to see assigned value [OK]
Common Mistakes:
MISTAKES
  • Expecting class name instead of attribute value
  • Confusing attribute name with value
  • Thinking print causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes