Dataclasses & ABCs

Preview — 3 of 10 questions

What is the output of the following code?

javascript
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __eq__(self, other):
        return self.x == other.x and self.y == other.y

p1 = Point(1, 2)
points = {p1}
AAttributeError
BTypeError: cannot use 'Point' as a set element (unhashable type: 'Point')
Cpoints becomes an empty set
Dpoints = {p1} succeeds normally

What is the result of running the following code?

javascript
from abc import ABC, abstractmethod

class Shape(ABC):
    @abstractmethod
    def area(self):
        pass

class Circle(Shape):
    def __init__(self, radius):
        self.radius = radius

    def area(self):
        return 3.14159 * self.radius ** 2

s = Shape()
As = Shape() succeeds, but calling s.area() later raises NotImplementedError
Bs = Shape() succeeds, creating an instance with no area implementation
CNameError: name 'ABC' is not defined
DTypeError: Can't instantiate abstract class Shape without an implementation for abstract method 'area'

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)
print(p1 == p2)
ATypeError
BPoint(x=1, y=2) then False
CPoint(x=1, y=2) then True
D<__main__.Point object at 0x...> then False

Sign up free to play

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