ProgrammingModerateTCS NQT · Accenture pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer n = 5, t = 0
for (i = 1 to n)
    if (i mod 2 == 1)
        t = t + i * i
    else
        t = t - i
    end if
end for
Print t
  1. A. 29
  2. B. 35
  3. C. 41
  4. D. 23

Correct answer: A. 29

Odd squares 1 + 9 + 25 = 35, minus the even numbers 2 + 4 = 6 → 29. Forgetting the subtraction gives 35.

More Pseudocode and Code Tracing questions

Exams that ask this topic