ProgrammingVery hardGoldman Sachs OA · Infosys SP
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer stars = 0
Procedure p(Integer n)
stars = stars + 1
for (each i from 1 to n)
p(n - i)
end for
End Procedure
p(5)
Print stars- A. 31
- B. 16
- C. 120
- D. 32
Correct answer: D. 32
Let C(n) be the calls made by p(n). C(0) = 1 and C(n) = 1 + C(n − 1) + … + C(0), which doubles each time: 1, 2, 4, 8, 16, 32. So stars = 32.
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 bitwise AND on two's-complement integers) Integer n = 360 while (n > 0) Print n…
- What will be the output of the following pseudocode? (substring(s, i, k) is the k characters starting at index i; indices start at 0)…
- What will be the output of the following pseudocode? Function F(Integer n) if (n == 0) return 1 end if return n - M(F(n - 1)) End Function…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[6] = {1, 2, 3, -2, 2, 1}, c = 0, s for (each i…
- What will be the output of the following pseudocode? Integer m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} for (each i from 0 to 2) for…
- What will be the output of the following pseudocode? (C-style for loop: the update i = i + 1 runs after each pass of the body) Integer c =…