Bird
0
0

What will be printed when this code runs?

medium📝 Predict Output Q4 of 15
Python - Polymorphism and Dynamic Behavior
What will be printed when this code runs?
class X:
    def info(self):
        print('Info from X')

class Y(X):
    def info(self):
        print('Info from Y')

obj = Y()
obj.info()
AInfo from Y
BInfo from X
CInfo from X\nInfo from Y
DNo output, error occurs
Step-by-Step Solution
Solution:
  1. Step 1: Identify method overriding

    Class Y overrides the info method of class X.
  2. Step 2: Method call on Y instance

    Calling info on an instance of Y invokes Y's version.
  3. Final Answer:

    Info from Y -> Option A
  4. Quick Check:

    Subclass method called, parent method ignored [OK]
Quick Trick: Subclass method output replaces parent output [OK]
Common Mistakes:
  • Expecting both methods to print
  • Thinking parent method runs automatically
  • Assuming error due to overriding

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes