ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
x = 0
total = 0
for i in range(1, 6):
    if i % 2:
        continue
    x += i
    if x > 4:
        break
else:
    total = -1
print(x, total)
  1. A. 2 0
  2. B. 6 0
  3. C. 2 -1
  4. D. 6 -1

Correct answer: B. 6 0

The loop adds 2 and then 4, giving x = 6 after the second pass, which is greater than 4, so break runs. Since break was taken, the else block is skipped and total stays 0.

More Python Programming questions

Exams that ask this topic