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]
  1. A. 5 3
  2. B. 8 5
  3. C. 13 8
  4. 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.

More Pseudocode and Code Tracing questions

Exams that ask this topic