ProgrammingHardCapgemini · Cognizant GenC Next
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
for (each i from 0 to 2)
for (each j from 0 to 2)
swap(m[i][j], m[j][i])
end for
end for
Print m[0][1], m[1][0], m[2][0]- A. 2 4 7
- B. 4 2 3
- C. 4 2 7
- D. 2 4 3
Correct answer: A. 2 4 7
Because j runs over the whole row, every off-diagonal pair is swapped twice (once at (i, j) and again at (j, i)), so the matrix ends up unchanged: 2 4 7. A real transpose needs j to start from i + 1.
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 n = 0, c = 0 do c = c + 1 n = n + 3 while (n mod 5 != 0) Print c, n
- What will be the output of the following pseudocode? Integer cat[0..6] cat[0] = 1 for (each i from 1 to 6) cat[i] = 0 for (each j from 0…
- What will be the output of the following pseudocode? Integer a[6] = {3, 1, 2, 6, 4, 5}, swaps = 0, m, n = 6 for (each i from 0 to n - 2) m…
- What will be the output of the following pseudocode? Integer a[6] = {5, 2, 4, 6, 1, 3}, shifts = 0, key, j for (each i from 1 to 5) key =…
- What will be the output of the following pseudocode? (code(c) gives a letter's character code and char(k) the reverse; 'A'..'Z' have…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer n = 987654321, c = 0…