ProgrammingHardGoldman Sachs OA · TCS Digital
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer calls = 0
Procedure f(Integer n)
calls = calls + 1
for (each i from 1 to n - 1)
f(i)
end for
End Procedure
f(5)
Print calls- A. 15
- B. 16
- C. 10
- D. 32
Correct answer: B. 16
Let C(n) be the calls made by f(n): C(1) = 1 and C(n) = 1 + C(1) + … + C(n − 1), which doubles: 1, 2, 4, 8, 16. calls = 16.
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 t[0..10] t[0] = 0 t[1] = 1 t[2] = 1 for (each i from 3 to 10) t[i] = t[i - 1]…
- What will be the output of the following pseudocode? (strings are indexed from 0) String s = "{[()]}([)]" Stack st = empty Integer bad =…
- What will be the output of the following pseudocode? Queue q = {1, 2, 3, 4, 5} Integer k = 3 Stack st = empty for (each i from 1 to k)…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) // numbers stored as digit…
- What will be the output of the following pseudocode? // circular list of 6 people: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> back to 1 Node p = node 1…
- What is the time complexity of the following pseudocode, as a function of n? Function f(Integer n) if (n <= 1) return 1 end if return f(n…