ProgrammingVery hardProduct company OA · code-tracing level
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer s = 0
Procedure p(Integer n)
if (n == 0)
return
end if
p(n - 1)
s = s + n
p(n - 1)
End Procedure
p(5)
Print s- A. 31
- B. 57
- C. 15
- D. 80
Correct answer: B. 57
p(n) adds n once and calls p(n − 1) twice, so the value k is added 2^(5 − k) times: 5 once, 4 twice, 3 four times, 2 eight times, 1 sixteen times. s = 5 + 8 + 12 + 16 + 16 = 57. 31 = 2⁵ − 1 is the number of additions, not their sum; 15 = 1 + … + 5 treats each value as added once.
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? (/ is integer division.) Function A(Integer n) if (n <= 0) return 1 end if return B(n…
- What will be the output of the following pseudocode? (g is a global variable.) Integer g = 0 Function f(Integer n) if (n == 0) return 0…
- What will be the output of the following pseudocode? (Integers are passed by value. An array parameter receives a reference to the…
- What will be the output of the following pseudocode? (&& binds more tightly than ||, and both stop evaluating as soon as the result is…
- What will be the output of the following pseudocode? Integer c = 0, i for (i = 1; i <= 50; i = i + 1) i = i 2 c = c + 1 end for Print c, i
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer a[8] = {1, 2, 3, 4, 6, 7, 8, 9} Integer i = 0, j…