ProgrammingHardTCS NQT · Infosys · Capgemini
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer memo[10] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
Integer calls = 0
Function F(n)
calls = calls + 1
If n < 2 Then Return n
If memo[n] != -1 Then Return memo[n]
memo[n] = F(n - 1) + F(n - 2)
Return memo[n]
End Function
Integer r = F(8)
Print r, calls- A. 21 15
- B. 21 67
- C. 21 8
- D. 21 9
Correct answer: A. 21 15
F(8) = 21. With memoisation each n from 2 to 8 expands once (7 expansions); each expansion makes two calls, so calls = 1 + 2 × 7 = 15. Without the memo the same function would make 67 calls.
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 exponentiation; / is integer division truncating toward zero. An operator pops…
- What will be the output of the following pseudocode? Integer val[6] = {5, 8, 2, 9, 4, 7} Integer nxt[6] = {3, 5, 0, 1, -1, 4} Integer p =…
- What will be the output of the following pseudocode? Integer a[51] = all zeros (indices 0 to 50) For i = 1 to 50 For j = i to 50 step i…
- What will be the output of the following pseudocode? (ord('A') = 65; the letters are uppercase and the mod keeps every shift inside the…
- What will be the output of the following pseudocode? (Arrays are 0-indexed.) Integer a[7] = {5, 1, 4, 2, 8, 0, 3} Integer swaps = 0…
- What will be the output of the following pseudocode? (Static scoping: a name used inside a procedure refers to the variable visible where…