ProgrammingModerateTCS NQT · Accenture pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer a[6] = {1, 2, 3, 4, 5, 6}, n = 6
for (i = 0 to n/2 - 1)
swap(a[i], a[n - 1 - i])
end for
Print a[1], a[2], a[3]- A. 2 3 4
- B. 5 4 3
- C. 4 3 2
- D. 5 3 4
Correct answer: B. 5 4 3
Swapping a[i] with its mirror for the first half reverses the array: 6 5 4 3 2 1, so indices 1..3 hold 5, 4, 3. Looping all the way to n − 1 would swap every pair twice and restore the original order.
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? // C-style integer arithmetic: / truncates toward zero, mod keeps the dividend's sign…
- What will be the output of the following pseudocode? Integer Function g(Integer n) if (n <= 1) return n return g(n - 1) + 2 g(n - 2) End…
- What will be the output of the following pseudocode? String s = "" Integer n = 13 while (n > 0) s = (n mod 2) + s // prepend the digit n =…
- What will be the output of the following pseudocode? Integer i = 1, j, c = 0 while (i <= 16) j = i while (j <= 16) c = c + 1 j = j 2 end…
- What will be the output of the following pseudocode? Integer a = 0 for (i = 1 to 10) if (i mod 2 == 0) continue if (i > 7) break a = a + i…
- 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] +…