ProgrammingModerateTCS NQT · Accenture pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Function fun(Integer n)
if (n < 3)
return n
end if
return fun(n - 1) + 2 * fun(n - 3)
End Function
Print fun(6)- A. 8
- B. 16
- C. 10
- D. 12
Correct answer: D. 12
Build it up from the bottom: fun(0) = 0, fun(1) = 1, fun(2) = 2, fun(3) = 2 + 0 = 2, fun(4) = 2 + 2 = 4, fun(5) = 4 + 4 = 8, fun(6) = 8 + 2 × 2 = 12.
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 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…
- What will be the output of the following pseudocode? Integer x = 0 for (each i from 1 to 5) for (each j from 1 to i) if ((i + j) mod 2 ==…
- What will be the output of the following pseudocode? Function fun(Integer n) if (n > 0) fun(n - 1) Print n fun(n - 1) end if End Function…
- What will be the output of the following pseudocode? Integer a = 1, b = 1, c for (each i from 1 to 6) c = a + b a = b b = c end for Print b
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Integer n = 100, c = 0 while…