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- A. 3
- B. 4
- C. 5
- 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.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? Integer c[0..5] = {1, 0, 0, 0, 0, 0} for (each i from 1 to 5) for (each j from 1 to…
- What will be the output of the following pseudocode? (C-style switch: a case without break falls through to the next one) Integer x = 2, s…
- What will be the output of the following pseudocode? Integer i = 1, c = 0 while (i <= 100) i = i 2 + c c = c + 1 end while Print c, i
- What will be the output of the following pseudocode? // list: 1 -> 2 -> 3 -> 4 -> 5 -> NULL, head points to node 1 Node prev = NULL, cur =…
- What will be the output of the following pseudocode? (a loop whose start is greater than its end runs zero times) Integer count = 0…
- What will be the output of the following pseudocode? Integer stars = 0 Procedure p(Integer n) stars = stars + 1 for (each i from 1 to n)…