Classes & Objects · Question 6 · Easy

Square overrides the area method of its parent Shape. What is printed for a square of side 4?

class Shape:
    def area(self):
        return 0

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

print(Square(4).area())
  1. 0
  2. 8
  3. 16
  4. TypeError
Answer and explanation

Answer: C. 16

Explanation: The method in the child class replaces the parent's version for Square objects. 4 ** 2 is 4 squared, which is 16.

Previous: Manager inherits from Employee and defines nothing of its own. What does… · Next: The method on() in the Lamp class is written without a self parameter. What…

Practise classes & objects

Try a fresh set of classes & objects questions with an explanation after every answer, free and without signing up: Classes & Objects practice. Or sit a full placement mock test.

More classes & objects questions

More python practice

Exceptions & Files · Built-ins & Standard Library · Variables, Types & Operators · Python Strings · Lists & Tuples · Dictionaries & Sets · Conditions & Loops · Functions & Scope

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge