ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

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

Correct answer: D. -2

The conditional expression is i if i is odd and -i if it is even. The contributions are 0, 1, -2, 3, -4, which sum to -2.

More Python Programming questions

Exams that ask this topic