Bird
Raised Fist0

What will be the output of this code?

medium📝 Predict Output Q5 of Q15
Python - Object-Oriented Programming Foundations
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

sq = Square(4)
print(sq.area())
A0
B4
C16
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand class Square initialization and area method

    Square stores side length 4 and calculates area as side * side.
  2. Step 2: Calculate area

    4 * 4 = 16, so sq.area() returns 16.
  3. Final Answer:

    16 -> Option C
  4. Quick Check:

    Square area = side² = 16 [OK]
Quick Trick: Child class can override methods and use attributes [OK]
Common Mistakes:
MISTAKES
  • Using parent area method returning 0
  • Confusing side length with area
  • Syntax errors in __init__

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes