ProgrammingVery hardTCS NQT · Infosys · Capgemini
What will be the output of the following pseudocode? (Arrays are 0-indexed.)
What will be the output of the following pseudocode? (Arrays are 0-indexed.)
Integer a[6] = {4, 7, 1, 9, 3, 6}
Integer i = 0
For k = 1 to 5
Integer j = (i + a[i]) mod 6
Integer t = a[i]
a[i] = a[j]
a[j] = t
i = j
End For
Print a[0], a[1], a[2], a[3], a[4], a[5]- A. 3 1 9 3 7 6
- B. 3 7 4 9 1 6
- C. 4 7 1 9 3 6
- D. 1 7 4 9 3 6
Correct answer: D. 1 7 4 9 3 6
Each pass jumps from i to j = (i + a[i]) mod 6 using the value held at i before the swap, swaps the two cells, and continues from j. The walk visits indices 0, 4, 2, 0, 4 in turn, and the five swaps give the array shown.
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? String s = "ABCDEF", r = "" For i = 0 to 5 If i mod 2 = 0 Then r = s[i] + r Else r =…
- What will be the output of the following pseudocode? Integer a = 91, b = 35, steps = 0 While a != b If a > b Then a = a - b Else b = b - a…
- What will be the output of the following pseudocode? Integer memo[10] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1} Integer calls = 0…
- What will be the output of the following pseudocode? (^ is exponentiation; / is integer division truncating toward zero. An operator pops…
- What will be the output of the following pseudocode? Integer val[6] = {5, 8, 2, 9, 4, 7} Integer nxt[6] = {3, 5, 0, 1, -1, 4} Integer p =…
- What will be the output of the following pseudocode? Integer a[51] = all zeros (indices 0 to 50) For i = 1 to 50 For j = i to 50 step i…