Bird
Raised Fist0

What will be the output of this code?

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

car1 = Car()
print(car1.brand, car1.year)
AToyota 2020
BToyota None
CNone 2020
DError: Missing arguments
Step-by-Step Solution
Solution:
  1. Step 1: Check constructor default values

    brand defaults to 'Toyota', year defaults to 2020 if no arguments are passed.
  2. Step 2: Object creation and attribute values

    car1 is created without arguments, so brand='Toyota' and year=2020 are assigned.
  3. Final Answer:

    Toyota 2020 -> Option A
  4. Quick Check:

    Defaults used when no args passed = Toyota 2020 [OK]
Quick Trick: No args means defaults are assigned automatically [OK]
Common Mistakes:
MISTAKES
  • Expecting error without arguments
  • Assuming attributes become None
  • Confusing default values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes