ProgrammingVery hardInfosys SP · Capgemini
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer total = 0
Procedure p(Integer n)
if (n <= 0)
return
end if
total = total + n
p(n - 2)
p(n - 3)
End Procedure
p(7)
Print total- A. 21
- B. 25
- C. 28
- D. 41
Correct answer: B. 25
Let T(n) be the total added by p(n): T(n) = n + T(n − 2) + T(n − 3), T(n ≤ 0) = 0. Going up: T(1) to T(7) = 1, 2, 4, 7, 11, 17, 25, e.g. T(7) = 7 + T(5) + T(4) = 7 + 11 + 7 = 25.
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? Integer c = 0 for (each i from 1 to 8) for (each j from i + 1 to 9) for (each k from…
- What will be the output of the following pseudocode? Function f(Integer a, Integer b) return a b - (a + b) End Function Print f(f(2, 3)…
- What will be the output of the following pseudocode? (Strings are indexed from 0) Integer calls = 0 Function pal(String s, Integer i…
- What will be the output of the following pseudocode? Integer s = 0, i = 0 while (i < 10) i = i + 1 if (i mod 3 == 0) continue end if s = s…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[5] = {10, 20, 30, 40, 50}, b[5], k = 2, n = 5…
- What will be the output of the following pseudocode? (Strings are indexed from 0) String s = "ABBCCCDDDD" Integer best = 0, run = 1 for…