ProgrammingModerateInfosys · Capgemini pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer x = 1
for (i = 1 to 4)
    if (i mod 2 == 1)
        x = x * i
    else
        x = x + i
    end if
end for
Print x
  1. A. 13
  2. B. 24
  3. C. 10
  4. D. 15

Correct answer: A. 13

i = 1: x = 1·1 = 1; i = 2: 1 + 2 = 3; i = 3: 3·3 = 9; i = 4: 9 + 4 = 13. Mixing up which branch handles odd i would give 1 + 1 = 2, 4, 7, 28.

More Pseudocode and Code Tracing questions

Exams that ask this topic