Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q5 of 15
Python - Polymorphism and Dynamic Behavior
What will be the output of this code?
class Shape:
    def area(self):
        return 0

class Square(Shape):
    def __init__(self, side):
        self.side = side
    def area(self):
        return self.side * self.side

shapes = [Shape(), Square(4)]
for shape in shapes:
    print(shape.area())
A0\n16
B0\n4
CError: missing side attribute
D16\n0
Step-by-Step Solution
Solution:
  1. Step 1: Analyze class methods and objects

    Shape.area returns 0; Square overrides area to return side squared.
  2. Step 2: Loop through shapes and call area()

    Shape() returns 0; Square(4) returns 4*4=16; prints in order.
  3. Final Answer:

    0\n16 -> Option A
  4. Quick Check:

    Polymorphism area output = 0 then 16 [OK]
Quick Trick: Subclass method overrides base method output [OK]
Common Mistakes:
  • Expecting side attribute in base class
  • Mixing order of outputs
  • Assuming area returns side, not side squared

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes