ProgrammingModerateInfosys · Capgemini pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer Function fun(Integer a, Integer b)
if (b == 0) return 1
if (b mod 2 == 0)
Integer t = fun(a, b / 2)
return t * t
end if
return a * fun(a, b - 1)
End Function
Print fun(3, 5)- A. 125
- B. 15
- C. 243
- D. 81
Correct answer: C. 243
fun computes aᵇ by fast exponentiation: 3⁵ = 3 · (3²)² = 3 · 81 = 243. Odd b peels off one factor, even b squares a half-power, so the call count is O(log b).
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 n = 45, c = 0 while (n != 0) c = c + (n & 1) n = n >> 1 end while Print c
- What will be the output of the following pseudocode? Integer x = 10, s = 0 while (x > 0) s = s + x x = x - 3 end while Print s, x
- What will be the output of the following pseudocode? Integer a = 10, b = 20, c = 30, r if (a > b AND b > c) r = 1 else if (a < b OR c < b)…
- What will be the output of the following pseudocode? Integer Function f(Integer n) if (n == 1) return 1 return f(n / 2) + n // integer…
- What will be the output of the following pseudocode? Integer count = 0 for (i = 2 to 30) Boolean prime = true for (d = 2 while d d <= i)…
- What will be the output of the following pseudocode? Integer a[6] = {1, 2, 3, 4, 5, 6}, n = 6 for (i = 0 to n/2 - 1) swap(a[i], a[n - 1…