ProgrammingVery hardProduct company OA · code-tracing level
What will be the output of the following pseudocode? (Arrays are indexed from 0; / is integer division; 'from … to …' includes both ends.)
What will be the output of the following pseudocode? (Arrays are indexed from 0; / is integer division; 'from … to …' includes both ends.)
Integer a[6] = {1, 2, 3, 4, 5, 6}, n = 6, t
for (each i from 0 to n / 2)
t = a[i]
a[i] = a[n - 1 - i]
a[n - 1 - i] = t
end for
Print a[0], a[2], a[3], a[5]- A. 6 3 4 1
- B. 6 4 3 1
- C. 1 3 4 6
- D. 6 3 4 2
Correct answer: A. 6 3 4 1
The loop runs for i = 0, 1, 2, 3 — one iteration too many for a reversal. After i = 0, 1, 2 the array is reversed: 6 5 4 3 2 1. The extra iteration i = 3 swaps a[3] with a[n − 1 − 3] = a[2], undoing the middle swap: 6 5 3 4 2 1. So the output is 6 3 4 1. '6 4 3 1' is what a correct reversal (i up to n/2 − 1) would print.
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 c = 0, d, k for (each x from 1 to 30) k = 0 for (each d from 1 to x) if (x…
- What will be the output of the following pseudocode? (Array positions run from 1 to 10.) Integer alive[1..10] = all 1, left = 10, pos = 0…
- What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR, << and >> are shifts.) Integer x = 22, r = 0 for…
- What will be the output of the following pseudocode? (Arrays are indexed from 0; / is integer division.) Integer a[7] = {1, 3, 5, 5, 5, 7…
- What will be the output of the following pseudocode? Integer s = 0 Procedure p(Integer n) if (n == 0) return end if p(n - 1) s = s + n p(n…
- What will be the output of the following pseudocode? (/ is integer division.) Function A(Integer n) if (n <= 0) return 1 end if return B(n…