Bird
0
0

Given these classes:

hard📝 Application Q15 of 15
Python - Inheritance and Code Reuse
Given these classes:
class Vehicle:
    def __init__(self, brand):
        self.brand = brand
    def info(self):
        return f"Vehicle brand: {self.brand}"

class Car(Vehicle):
    def __init__(self, brand, model):
        super().__init__(brand)
        self.model = model
    def info(self):
        return f"Car brand: {self.brand}, model: {self.model}"

What will print(Car('Toyota', 'Corolla').info()) output?
ACar brand: Toyota, model: Corolla
BVehicle brand: Toyota
CCar brand: , model: Corolla
DTypeError due to missing argument
Step-by-Step Solution
Solution:
  1. Step 1: Understand constructor chaining

    Car's __init__ calls super().__init__(brand) to set brand in Vehicle.
  2. Step 2: Analyze info method override

    Car overrides info to include both brand and model.
  3. Step 3: Predict output

    Calling info() on Car instance returns "Car brand: Toyota, model: Corolla".
  4. Final Answer:

    Car brand: Toyota, model: Corolla -> Option A
  5. Quick Check:

    Child overrides method and calls parent's init [OK]
Quick Trick: Use super() to inherit parent init, override methods as needed [OK]
Common Mistakes:
  • Forgetting super() call in child __init__
  • Expecting parent info output instead of child's
  • Confusing missing arguments error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes