Bird
0
0

What will be the output of this code?

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

car1 = Car('Tesla')
print(car1.model)
Acar1
BTesla
Cmodel
DError: model not defined
Step-by-Step Solution
Solution:
  1. Step 1: Understand the constructor's role

    The constructor sets the model attribute of car1 to 'Tesla'.
  2. Step 2: Check the print statement

    Printing car1.model outputs the string stored in model, which is 'Tesla'.
  3. Final Answer:

    Tesla -> Option B
  4. Quick Check:

    Object attribute value = Tesla [OK]
Quick Trick: Constructor sets attributes accessed by object [OK]
Common Mistakes:
  • Expecting print(car1) to show model
  • Confusing variable names
  • Forgetting to pass argument to constructor

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes