Functions & Scope — Series 3

Preview — 3 of 10 questions

What is the output of the following code?

javascript
def connect(host, *, port, timeout=30):
    return host, port, timeout

print(connect("localhost", port=8080))

try:
    print(connect("localhost", 8080))
except TypeError as e:
    print("TypeError:", e)
A('localhost', 8080, 30), then TypeError: connect() takes 1 positional argument but 2 were given
B('localhost', 8080, 30), then the second call also succeeds, treating 8080 as port
C('localhost', 8080, 30), then ('localhost', 8080, 30) — both calls succeed identically
DTypeError on the first call — port was never supplied positionally

What is the output of the following code?

javascript
first, *middle, last = [10, 20, 30, 40, 50]
print(first, middle, last)

a, *rest = [1]
print(a, rest)
A10 20 50, then 1 []
B10 [20, 30, 40] 50, then 1 []
C10 [20, 30, 40, 50] 50, then 1 []
D10 [20, 30, 40] 50, then TypeError: not enough values to unpack

What is the output of the following code?

javascript
keys = ["a", "b", "c"]
values = [1, 2, 3]
d = dict(zip(keys, values))
print(d)

pairs = [("x", 10), ("y", 20)]
print(dict(pairs))
A{'a': 1, 'b': 2, 'c': 3}, then [('x', 10), ('y', 20)]
BTypeError: cannot convert dict_keys to dict
C{'a': 1, 'b': 2, 'c': 3}, then {'x': 10, 'y': 20}
D[('a', 1), ('b', 2), ('c', 3)], then {'x': 10, 'y': 20}

Sign up free to play

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