Bird
0
0

You want to create a child class Car that inherits from Vehicle and adds a new method honk. Which code correctly implements this?

hard📝 Application Q8 of 15
Python - Inheritance and Code Reuse
You want to create a child class Car that inherits from Vehicle and adds a new method honk. Which code correctly implements this?
Aclass Car: def honk(self): return "Beep beep!" class Vehicle(Car): pass
Bclass Car(Vehicle): def honk(): return "Beep beep!"
Cclass Car(Vehicle): def honk(self): return "Beep beep!"
Dclass Car(Vehicle): def honk(self): print("Beep beep!") def Vehicle(self): pass
Step-by-Step Solution
Solution:
  1. Step 1: Check inheritance syntax

    class Car(Vehicle): def honk(self): return "Beep beep!" correctly defines Car inheriting Vehicle with proper syntax.
  2. Step 2: Verify method definition

    honk method has self parameter and returns string, which is correct.
  3. Final Answer:

    class Car(Vehicle):\n def honk(self):\n return "Beep beep!" -> Option C
  4. Quick Check:

    Correct inheritance and method syntax = class Car(Vehicle): def honk(self): return "Beep beep!" [OK]
Quick Trick: Define methods with self and inherit using parentheses [OK]
Common Mistakes:
  • Missing self parameter in method
  • Wrong inheritance direction
  • Adding unrelated methods incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes