ProgrammingHardGoldman Sachs OA · TCS Digital

What will be the output of the following pseudocode? (All variables are integers, so / is integer division)

What will be the output of the following pseudocode? (All variables are integers, so / is integer division)
Integer r = 1, b = 3, e = 13, m = 11
while (e > 0)
    if (e mod 2 == 1)
        r = (r * b) mod m
    end if
    b = (b * b) mod m
    e = e / 2
end while
Print r
  1. A. 3
  2. B. 4
  3. C. 5
  4. D. 9

Correct answer: C. 5

Fast exponentiation computes 3^13 mod 11. Since 3^5 = 243 = 22 × 11 + 1, 3^13 = 3^10 × 3^3 ≡ 27 ≡ 5.

More Pseudocode and Code Tracing questions

Exams that ask this topic