ProgrammingModerateTCS NQT · Accenture

Let x = 0. For i from 1 to 10: if i mod 3 == 0 then x = x + i, otherwise x = x - 1. What is printed?

Let x = 0. For i from 1 to 10: if i mod 3 == 0 then x = x + i, otherwise x = x - 1. What is printed?
x = 0
for i = 1 to 10
    if i mod 3 == 0 then x = x + i else x = x - 1
end for
Print x
  1. A. -25
  2. B. -4
  3. C. 11
  4. D. 18

Correct answer: C. 11

The running value goes -1, -2, 1, 0, -1, 5, 4, 3, 12, 11, so the answer is 11. Subtracting the multiples of 3 flips their sign, adding 1 instead of i loses the weight, and skipping the else branch ignores the decrements.

More Pseudocode and Code Tracing questions

Exams that ask this topic