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- A. 12
- B. 21
- C. 20
- 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).
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 x = 182 // binary 1011 0110, 8-bit register Print x >> 2, (x << 1) & 255, x &…
- What will be the output of the following pseudocode? Integer Function f(Integer a, Integer b) if (a == 0) return b return f(b mod a, a)…
- What will be the output of the following pseudocode? Integer a[] = {4, 7, 1, 9, 3} for (i = 0 to 3) if (a[i] > a[i + 1]) swap(a[i], a[i +…
- What will be the output of the following pseudocode? Integer n = 5, s = 0 for (i = 1 to n) for (j = 1 to i) if ((i + j) mod 2 == 0) s = s…
- What will be the output of the following pseudocode? Integer v = 1 for (k = 0 to 4) v = v 2 + k end for Print v
- What will be the output of the following pseudocode? Integer calls = 0 Integer Function f(Integer n) calls = calls + 1 if (n <= 2) return…