Bird
0
0

Identify the problem in this code:

medium📝 Debug Q7 of 15
Python - Inheritance and Code Reuse

Identify the problem in this code:

class Base:
    def info(self):
        print('Base info')

class Derived(Base):
    def info(self):
        print('Derived info')
    def info(self):
        print('Another info')

obj = Derived()
obj.info()
AParent class info method is called.
BBoth info methods run in sequence.
CSecond info method overwrites the first in Derived class.
DError due to duplicate method names in Derived.
Step-by-Step Solution
Solution:
  1. Step 1: Check method definitions in Derived

    Derived class defines info method twice; second definition replaces the first.
  2. Step 2: Effect on method call

    Calling obj.info() runs only the last defined info method, printing 'Another info'.
  3. Final Answer:

    Second info method overwrites the first in Derived class. -> Option C
  4. Quick Check:

    Duplicate method names in class overwrite previous ones [OK]
Quick Trick: Later method definitions overwrite earlier ones in same class [OK]
Common Mistakes:
  • Expecting both methods to run
  • Thinking duplicate methods cause error
  • Assuming parent method runs instead

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes