ProgrammingModerateCapgemini · Infosys pattern
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 calls = 0
Function power(Integer a, Integer b)
calls = calls + 1
if (b == 0)
return 1
end if
Integer t = power(a, b / 2)
if (b mod 2 == 0)
return t * t
end if
return a * t * t
End Function
Print power(3, 5), calls- A. 243 3
- B. 125 4
- C. 243 4
- D. 243 5
Correct answer: C. 243 4
This is fast exponentiation: b goes 5 → 2 → 1 → 0, so there are 4 calls. Going back up: b = 0 gives 1, b = 1 gives 3, b = 2 gives 9, b = 5 gives 3 × 81 = 243.
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? (Arrays are indexed from 0.) Integer arr[5] = {3, 1, 4, 1, 5} for (each i from 1 to…
- What will be the output of the following pseudocode? Integer x = 0 for (each i from 1 to 100) if (i mod 4 == 0) continue end if if (i >…
- What will be the output of the following pseudocode? Integer a = 10, b = 3, c = 0 while (a >= b) a = a - b c = c + 1 end while Print c, a
- What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR, ^ is bitwise XOR, << and >> are left and right…
- What will be the output of the following pseudocode? Integer c = 0 for (each i from 1 to 5) for (each j from 1 to 5) if (j > i) break end…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Integer n = 2048, c = 0, d…