ProgrammingModerateGATE CS · Product-company placement

What is printed?

What is printed?
class Shape:
    sides = 0
    def describe(self):
        return f"{type(self).__name__}:{self.sides}"
class Tri(Shape):
    sides = 3
print(Shape().describe(), Tri().describe(), Tri.describe is Shape.describe)
  1. A. Shape:0 Tri:3 True
  2. B. Shape:0 Shape:3 True
  3. C. Shape:0 Tri:0 True
  4. D. Shape:0 Tri:3 False

Correct answer: A. Shape:0 Tri:3 True

The inherited method looks up self.sides and type(self) on the actual object, so a Tri reports Tri:3. Accessing the function through either class gives the same plain function object.

More Python Programming questions

Exams that ask this topic