ProgrammingHardTCS CodeVita · Infosys HackWithInfy · Product Company SDE

What does the following Python 3 code print? def mk(): c = 0 def inc(): nonlocal c c += 4 return c return inc a, b = mk(), mk() for _ in range(3): a() print(a(), b())

  1. A. 16 12
  2. B. 4 4
  3. C. 16 20
  4. D. 16 4

Correct answer: D. 16 4

Each call to mk() creates a separate closure cell, so a and b keep independent counters. Output: 16 4.

More Python Programming questions

Exams that ask this topic