ProgrammingModerateAccenture · Cognizant pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer n = 6, f[0..6], s = 0
f[0] = 0, f[1] = 1
for (i = 2 to n)
    f[i] = f[i - 1] + f[i - 2]
end for
for (i = 0 to n)
    s = s + f[i]
end for
Print s
  1. A. 12
  2. B. 21
  3. C. 20
  4. D. 33

Correct answer: C. 20

f = 0, 1, 1, 2, 3, 5, 8 and the sum is 20 – which equals f[8] − 1 = 21 − 1 (sum of the first Fibonacci numbers).

More Pseudocode and Code Tracing questions

Exams that ask this topic