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(6): a() print(a(), b())

  1. A. 4 4
  2. B. 28 4
  3. C. 28 32
  4. D. 28 24

Correct answer: B. 28 4

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

More Python Programming questions

Exams that ask this topic