ProgrammingHardTCS NQT · Accenture pattern
How many numbers are printed when fun(5) is called?
How many numbers are printed when fun(5) is called?
Function fun(Integer n)
if (n <= 0)
return
end if
fun(n - 1)
Print n
fun(n - 2)
End Function- A. 15
- B. 7
- C. 12
- D. 8
Correct answer: C. 12
Let P(n) be the count. P(n) = P(n-1) + 1 + P(n-2), with P(0) = P(-1) = 0. So P(1) = 1, P(2) = 2, P(3) = 4, P(4) = 7, P(5) = 12 (Fibonacci numbers minus 1).
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? (A static variable keeps its value between calls and is initialised only once. The two calls in Print are…
- What will be the output of the following pseudocode? Integer a = 1, b = 2, c = 3 a = b + c b = a - b c c = a b - c Print a + b + c
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Integer s = 0 for (each i…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Integer n = 100, c = 0, d = 5…
- What will be the output? (calls counts every call, including the first.) Integer calls = 0 Function fun(Integer a, Integer b) calls =…
- What will be the output of the following pseudocode? Integer c = 0 for (i = 1; i < 64; i = i 2) for (j = i; j < 64; j = j + i) c = c + 1…