ProgrammingModerateInfosys · Accenture · Capgemini pattern

What is printed?

What is printed?
x = 10
def f(a=x):
    return a
x = 20
print(f())
  1. A. None
  2. B. 10
  3. C. NameError
  4. 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

Exams that ask this topic