Bird
0
0

Find the error in this code:

medium📝 Debug Q14 of 15
Python - Inheritance and Code Reuse
Find the error in this code:
class Animal:
    def sound(self):
        return "Some sound"

class Dog(Animal):
    def sound():
        return "Bark"

d = Dog()
print(d.sound())
Aprint statement syntax error
BDog class should not inherit Animal
CCannot override methods in child class
DMissing self parameter in Dog's sound method
Step-by-Step Solution
Solution:
  1. Step 1: Check method definitions

    In Python, instance methods must have self as first parameter.
  2. Step 2: Identify error in Dog's sound

    Dog's sound method lacks self, causing a TypeError when called on instance.
  3. Final Answer:

    Missing self parameter in Dog's sound method -> Option D
  4. Quick Check:

    Instance methods need self parameter [OK]
Quick Trick: Always include self as first method parameter [OK]
Common Mistakes:
  • Forgetting self in child method
  • Thinking inheritance disallows overriding
  • Assuming print syntax is wrong

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes