ProgrammingHard

int x = 0; for (int i = 0; i < 4; i++) x += i % 2 ? i : -i; printf("%d", x); What is printed?

  1. A. 4
  2. B. 0
  3. C. 6
  4. D. 2

Correct answer: D. 2

The terms are -0 (i = 0), 1 (i = 1), -2 (i = 2) and 3 (i = 3). Their sum is 2.

More C Programming questions

Exams that ask this topic