ProgrammingHardCapgemini · Infosys pattern
How many times is fun called in total when fun(6) is evaluated?
How many times is fun called in total when fun(6) is evaluated?
Function fun(Integer n)
if (n <= 2)
return 1
end if
return fun(n - 1) + fun(n - 2) + fun(n - 3)
End Function- A. 27
- B. 13
- C. 25
- D. 17
Correct answer: C. 25
Let C(n) be the number of calls. C(n) = 1 + C(n-1) + C(n-2) + C(n-3), with C(0) = C(1) = C(2) = 1. Then C(3) = 4, C(4) = 7, C(5) = 13, C(6) = 1 + 13 + 7 + 4 = 25. The return value, fun(6) = 17, is the trap.
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, j for (each i from 1 to 10) j = i while (j <= 10) c = c + 1 j = j + i…
- What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR, ^ is bitwise XOR, << and >> are left and right…
- What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR, ^ is bitwise XOR, << and >> are left and right…
- 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…
- 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