ProgrammingModerateTCS NQT · Accenture pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer p = 1
for (each i from 1 to 10)
    p = (p * 3) mod 7
end for
Print p
  1. A. 4
  2. B. 5
  3. C. 3
  4. D. 1

Correct answer: A. 4

p = 3¹⁰ mod 7. Powers of 3 mod 7 repeat every 6 (3, 2, 6, 4, 5, 1), so 3¹⁰ ≡ 3⁴ ≡ 81 mod 7 = 4.

More Pseudocode and Code Tracing questions

Exams that ask this topic