ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
adders = []
for i in range(3):
    def adder(x, i=i):
        return x + i
    adders.append(adder)
print(adders[0](10), adders[2](10))
  1. A. 13 13
  2. B. 10 12
  3. C. 10 10
  4. D. 13 11

Correct answer: B. 10 12

Default argument i=i captures the current loop value of i at the time each function is DEFINED. adders[0] captured i=0 (10+0=10) and adders[2] captured i=2 (10+2=12).

More Python Programming questions

Exams that ask this topic