Functions & Scope

Preview — 3 of 10 questions

What is the output of the following code?

javascript
x = "global"

def outer():
    x = "enclosing"
    def inner():
        nonlocal x
        x = "inner"
        return x
    inner()
    return x

print(outer())
print(x)
Aglobal, global
Binner, global
Cinner, inner
Denclosing, global

What is the output of the following code?

javascript
funcs = []
for i in range(3):
    funcs.append(lambda: i)

print([f() for f in funcs])
ATypeError
B[0, 0, 0]
C[0, 1, 2]
D[2, 2, 2]

What is the output of the following code?

javascript
def describe(a, b, *args, c=10, **kwargs):
    return a, b, args, c, kwargs

print(describe(1, 2, 3, 4, c=99, d=5))
A(1, 2, (3, 4, 99), 10, {'d': 5})
BTypeError
C(1, 2, (3, 4), 99, {'d': 5})
D(1, 2, (3, 4), 10, {'c': 99, 'd': 5})

Sign up free to play

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