ProgrammingHardCapgemini · 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.)
Function power(Integer a, Integer b)
if (b == 0)
return 1
end if
Integer half = power(a, b / 2)
if (b mod 2 == 0)
return half * half
end if
return a * half * half
End Function
Print power(2, 10)- A. 512
- B. 100
- C. 1024
- D. 2048
Correct answer: C. 1024
This is fast exponentiation: b halves at each level (10->5->2->1->0), and the results are squared back up (and multiplied by a once when b is odd), correctly computing 2^10 = 1024.
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? (All variables are integers, so / is integer division.) Integer n = 17, i = 2 Boolean…
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer arr[8] = {3, 1, 4, 1, 5, 9, 2, 6} Integer n = 8…
- What will be the output of the following pseudocode? Function countdownSum(Integer n) if (n == 0) return 0 end if return n +…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Integer n = 16, c = 0, i = n…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Integer n = 1024, c = 0 while…
- What is the time complexity of the following pseudocode, in terms of n, where the array has n elements? for (each i from 0 to n - 1) for…