ProgrammingModerateInfosys · Cognizant pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer x = 1
for (k = 1 to 10)
    x = (x * 3) mod 7
end for
Print x
  1. A. 1
  2. B. 3
  3. C. 6
  4. D. 4

Correct answer: D. 4

Powers of 3 mod 7 cycle with period 6: 3, 2, 6, 4, 5, 1, 3, 2, 6, 4. After 10 steps x = 3¹⁰ mod 7 = 3⁴ mod 7 = 4. '1' assumes the cycle has just completed (that happens at k = 6 and 12).

More Pseudocode and Code Tracing questions

Exams that ask this topic