ProgrammingHardGoldman Sachs OA · Amazon OA
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer M[2][2] = {{1, 1}, {1, 0}}, R[2][2] = {{1, 0}, {0, 1}}, T[2][2]
for (each k from 1 to 5)
for (each i from 0 to 1)
for (each j from 0 to 1)
T[i][j] = R[i][0] * M[0][j] + R[i][1] * M[1][j]
end for
end for
R = T // copies all four entries
end for
Print R[0][0], R[0][1]- A. 5 3
- B. 8 5
- C. 13 8
- D. 5 5
Correct answer: B. 8 5
R becomes M⁵, and powers of [[1, 1], [1, 0]] hold Fibonacci numbers: Mⁿ = [[F(n+1), F(n)], [F(n), F(n−1)]]. With n = 5: 8 5.
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? (All variables are integers, so / is integer division) Integer s = 0 for (each i from…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer n = 360, d = 2, s = 0…
- What will be the output of the following pseudocode? Integer count = 0, s for (each n from 2 to 500) s = 1 for (each d from 2 to n - 1) if…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer count = 0, n, s, d for…
- What will be the output of the following pseudocode? Integer c = 0 for (each y from 1890 to 2010) if ((y mod 4 == 0 AND y mod 100 != 0) OR…
- What will be the output of the following pseudocode? (AND has higher precedence than OR) Integer a = 5, b = 2, c = 3 if (a > b OR b > c…