ProgrammingVery hardCodeforces-style · placement coding round

What is printed?

What is printed?
calls = 0
def expensive():
    global calls
    calls += 1
    return 0
d = {'x': 5}
for k in ['x', 'y', 'x']:
    v = d.get(k, expensive())
print(v, calls)
  1. A. 5 3
  2. B. 0 3
  3. C. 5 2
  4. D. 5 0

Correct answer: A. 5 3

The default argument of get is evaluated before the call, so expensive() runs on all three iterations even when the key exists. The last value found is 5.

More Python Programming questions

Exams that ask this topic