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))- A. TypeError, then shape
- B. square, then shape
- C. square, then TypeError
- 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'.
Practise more Software Engineering, Git and Cloud Basics questionsSoftware Engineering, Git and Cloud Basics: notes and key points
More Software Engineering, Git and Cloud Basics questions
- Under design by contract, a method sqrt(x) has the precondition x ≥ 0. A caller passes -4 and the method fails. Whose bug is it, and which…
- A service catches every exception in a broad try/except, logs nothing and returns an empty list, so a database outage looks like 'no…
- How many of these defend against SQL injection in a login query: (1) parameterised queries / prepared statements, (2) escaping quotes by…
- A document-editor framework defines an abstract Application class with a method newDocument() that calls createDocument(); each concrete…
- An object adapter and a class adapter both make class Legacy fit interface Target. Which statement is correct?
- Both Facade and Mediator sit between groups of objects. What is the essential difference?