ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
i = 'x'
_ = [i for i in range(3)]
print(i)
  1. A. NameError
  2. B. x
  3. C. 2
  4. D. 0

Correct answer: B. x

In Python 3 a list comprehension has its own scope, so its loop variable does not overwrite the outer i. The outer i keeps the value 'x'.

More Python Programming questions

Exams that ask this topic