ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
x = 10
def f(x, y=x):
    x += 1
    return x, y
x = 20
print(f(5), f(y=1, x=2), x)
  1. A. (6, 20) (3, 1) 20
  2. B. (6, 10) (3, 1) 20
  3. C. (6, 10) (3, 1) 21
  4. D. (6, 5) (3, 1) 20

Correct answer: B. (6, 10) (3, 1) 20

The default y=x is evaluated at def time using the global x = 10. Inside f, x is the parameter, so x += 1 affects only the local name; the global stays 20.

More Python Programming questions

Exams that ask this topic