Dataclasses & ABCs — Series 3

Preview — 3 of 10 questions

What is the output of the following code?

javascript
from dataclasses import dataclass

@dataclass
class Point:
    x: int
    y: int

p = Point(1, 2)
print(p)
print(repr(p))
APoint(x=1, y=2) (twice)
BPoint(1, 2) (twice)
C{'x': 1, 'y': 2} (twice)
D<__main__.Point object at 0x...> (twice)

What is the output of the following code?

javascript
from dataclasses import dataclass

@dataclass
class Point:
    x: int
    y: int

p1 = Point(1, 2)
p2 = Point(1, 2)
print(p1 == p2)
print(p1 is p2)
ATrue, True
BTrue, False
CFalse, True
DFalse, False

What happens when the following code runs?

javascript
from abc import ABC, abstractmethod

class Shape(ABC):
    @abstractmethod
    def area(self):
        ...
    @abstractmethod
    def perimeter(self):
        ...

class Circle(Shape):
    def __init__(self, r):
        self.r = r
    def area(self):
        return 3.14 * self.r ** 2

try:
    c = Circle(2)
except TypeError as e:
    print("TypeError:", e)
ANo error, and calling c.perimeter() later raises NotImplementedError
Bc is created successfully — perimeter simply isn't callable on it
CTypeError: Can't instantiate abstract class Circle without an implementation for abstract method 'perimeter'
DTypeError: Can't instantiate abstract class Shape with abstract methods area, perimeter

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.