CS FundamentalsHardProduct-company placement · Python interview

What does this code print?

What does this code print?
from abc import ABC, abstractmethod
class Shape(ABC):
    @abstractmethod
    def area(self): ...
    def describe(self): return "shape"
class Square(Shape):
    def __init__(self, s): self.s = s
    def describe(self): return "square"
try:
    print(Square(2).describe())
except TypeError:
    print("TypeError")
print(Shape.describe(None))
  1. A. TypeError, then shape
  2. B. square, then shape
  3. C. square, then TypeError
  4. D. TypeError, then TypeError

Correct answer: A. TypeError, then shape

Square does not implement the abstract area(), so it is still abstract and cannot be instantiated: TypeError. Calling the concrete method through the class with None as self needs no instance, so it prints 'shape'.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic