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 rec(Integer n, Integer depth)
Print depth, n
if (n > 1)
rec(n / 2, depth + 1)
end if
End Function
rec(10, 0)- A. 0 10 1 5 2 2 3 1
- B. 0 10 1 5 2 2
- C. 0 10 1 5 2 2 3 1 4 0
- D. 0 5 1 2 2 1
Correct answer: A. 0 10 1 5 2 2 3 1
Each call halves n (integer division) and increases depth: (0,10) -> (1,5) -> (2,2) -> (3,1). Since n=1 is not > 1, the recursion stops after printing (3, 1).
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.) Function power(Integer a…
- 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…