ProgrammingHardAccenture · Cognizant pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer Function f(Integer n, Integer acc)
if (n == 0) return acc
return f(n - 1, (acc * n) mod 7)
End Function
Print f(6, 1)- A. 720
- B. 6
- C. 1
- D. 0
Correct answer: B. 6
f computes 6! mod 7 = 720 mod 7 = 6, reducing at every step so numbers stay small. By Wilson's theorem (p − 1)! ≡ −1 ≡ p − 1 (mod p) for prime p. The recursion is tail-recursive: the result is passed down in acc.
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 x = 100, c = 0 while (x > 1) if (x mod 2 == 0) x = x / 2 else x = 3 x + 1 c =…
- What will be the output of the following pseudocode? Integer a[] = {1, 3, 5, 7, 9, 11, 13} Integer lo = 0, hi = 6, t = 11, steps = 0, mid…
- What will be the output of the following pseudocode? Integer x = 15, y = 4, r = 0 while (x >= y) x = x - y r = r + 1 end while Print r, x
- What will be the output of the following pseudocode? Integer a = 1, b = 2, c = 3 a = (a < b) ? b : c b = (b < c AND a == 2) ? c : a c = (a…
- What will be the output of the following pseudocode? Integer Function h(Integer n) if (n <= 0) return 1 if (n mod 2 == 1) return h(n - 1)…
- What will be the output of the following pseudocode? Integer v = 0 for (i = 1 to 5) v = (v << 1) | (i & 1) end for Print v