ProgrammingHardTCS CodeVita · Infosys HackWithInfy · Product Company SDE

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

  1. A. 12 2
  2. B. 12 10
  3. C. 12 14
  4. D. 2 2

Correct answer: A. 12 2

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

More Python Programming questions

Exams that ask this topic