Advanced Patterns — Series 3

Preview — 3 of 10 questions

What is the output of the following code?

javascript
x = 1_000_000
print(x)
print(int("1_000"))
try:
    print(int("1__000"))
except ValueError as e:
    print("ValueError:", e)
A1000000, 1000, ValueError: invalid literal for int() with base 10: '1__000'
B1_000_000, 1000, ValueError: invalid literal for int() with base 10: '1__000'
C1000000, TypeError: int() can't parse strings with underscores
D1000000, 1000, 1000 — int() silently collapses repeated underscores

What is the output of the following code?

javascript
class Vec:
    def __init__(self, val):
        self.val = val
    def __add__(self, other):
        return Vec(self.val + other)
    def __repr__(self):
        return f"Vec({self.val})"

v = Vec(1)
before_id = id(v)
v += 5
print(v)
print(id(v) == before_id)
AVec(6), True
BVec(6), False
CTypeError: unsupported operand type(s) for +=: 'Vec' and 'int'
DVec(1), True

What is the output of the following code?

javascript
data = [1, 2, 3, 4, 5, 6, 7, 8]
results = [y for x in data if (y := x * x) > 20]
print(results)
print(y)
A[25, 36, 49, 64], 1
B[25, 36, 49, 64], NameError: name 'y' is not defined
C[25, 36, 49, 64], 64
D[5, 6, 7, 8], 64

Sign up free to play

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