Bird
0
0

Identify the error in this code:

medium📝 Debug Q6 of 15
Python - Polymorphism and Dynamic Behavior
Identify the error in this code:
class Person:
    def greet(self):
        return "Hello"

class Student(Person):
    def greet():
        return "Hi"

s = Student()
print(s.greet())
AThe greet method in Student is missing the self parameter
BThe greet method in Person should be static
CStudent class should not inherit from Person
DThe print statement is incorrect
Step-by-Step Solution
Solution:
  1. Step 1: Check method signature

    In Python, instance methods must include self as the first parameter.
  2. Step 2: Identify error

    Student's greet() lacks self, so calling s.greet() raises a TypeError.
  3. Final Answer:

    The greet method in Student is missing the self parameter -> Option A
  4. Quick Check:

    Instance methods require self parameter [OK]
Quick Trick: Instance methods must include self parameter [OK]
Common Mistakes:
  • Omitting self in subclass methods
  • Thinking static methods are needed here
  • Misunderstanding inheritance requirements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes