ProgrammingModerateWipro · Accenture pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Function f(Integer n)
if (n <= 1)
return 1
end if
return f(n - 1) + 2 * f(n - 2)
End Function
Print f(5)- A. 11
- B. 21
- C. 43
- D. 15
Correct answer: B. 21
Build up from the base cases f(0) = f(1) = 1: f(2) = 1 + 2 = 3, f(3) = 3 + 2 = 5, f(4) = 5 + 6 = 11, f(5) = 11 + 10 = 21. 11 is f(4); 43 is f(6).
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? (Strings are indexed from 0.) String s = "PSEUDOCODE" Integer v = 0 for (i = 0 to…
- What will be the output of the following pseudocode? Integer x = 1 for (k = 1 to 10) x = (x 3) mod 7 end for Print x
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer arr[5] = {3, 1, 4, 1, 5} for (i = 1 to 4) arr[i]…
- What will be the output of the following pseudocode? (calls counts every call, including the first; mod is the remainder.) Integer calls =…
- What will be the output of the following pseudocode? (& is bitwise AND.) Integer total = 0 for (i = 1 to 15) Integer n = i while (n != 0)…
- What will be the output of the following pseudocode? Integer c = 0, i for (i = 0; i < 10; i = i + 1) if (i mod 3 == 0) i = i + 2 end if c…