Classes & Instances — Series 3

Preview — 3 of 10 questions

What is the output of the following code?

javascript
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self._compute_area()

    def _compute_area(self):
        return self.width * self.height

r = Rectangle(3, 4)
print(r.area)
A12
BNone
CTypeError: _compute_area() missing 1 required positional argument
DAttributeError: 'Rectangle' object has no attribute '_compute_area'

What is the output of the following code?

javascript
class Builder:
    def __init__(self):
        self.parts = []

    def add(self, part):
        self.parts.append(part)
        return self

b = Builder().add("wheels").add("engine").add("seats")
print(b.parts)
A['wheels']
B['wheels', 'engine', 'seats']
CNone
DAttributeError: 'NoneType' object has no attribute 'add'

What is the output of the following code?

javascript
class Widget:
    count = 0

    def __init__(self):
        Widget.count += 1
        self.id = Widget.count

w1 = Widget()
w2 = Widget()
w3 = Widget()
print(w1.id, w2.id, w3.id)
print(Widget.count)
A1 1 1, 1
B0 1 2, 3
C1 2 3, 3
D1 2 3, 1

Sign up free to play

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