ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
i = 0
while (i := i + 1) < 4:
    pass
print(i)
  1. A. 3
  2. B. 5
  3. C. 0
  4. D. 4

Correct answer: D. 4

The walrus assigns i each time the condition runs. The loop stops when i reaches 4, the first value that fails the test, so the printed value is 4.

More Python Programming questions

Exams that ask this topic