ProgrammingModerateInfosys · Accenture · Capgemini pattern
What is printed?
What is printed?
x = 10
def f(a=x):
return a
x = 20
print(f())- A. None
- B. 10
- C. NameError
- D. 20
Correct answer: B. 10
Default values are evaluated once, when def runs. At that moment x is 10, so a defaults to 10. Rebinding x to 20 later does not change it.
More Python Programming questions
- What is printed? python print(sum(range(1, 10, 2)), list(range(5))[-1:-4:-1])
- What is printed? python print("abc" < "abd" < "ab", "10" < "9", 10 < 9)
- What is printed? python print({x % 3: x for x in range(7)})
- What is printed? python print([(i, j) for i in range(3) for j in range(i)])
- What is printed? python def g(): x = yield 1 yield x 2 it = g() print(next(it), it.send(5))
- What is printed? python calls = 0 def f(n): global calls calls += 1 return 1 if n <= 1 else f(n - 1) + f(n - 2) print(f(5), calls)