ProgrammingHardCapgemini · Infosys pattern
What will be the output of the following pseudocode (reversing an array in groups of size k)? (Arrays are indexed from 0.)
What will be the output of the following pseudocode (reversing an array in groups of size k)? (Arrays are indexed from 0.)
Integer arr[8] = {1, 2, 3, 4, 5, 6, 7, 8}
Integer k = 3
Integer n = 8
for (each i from 0 to n - 1 step k)
Integer l = i, r = min(i + k, n) - 1
while (l < r)
swap(arr[l], arr[r])
l = l + 1
r = r - 1
end while
end for
Print arr- A. [3, 2, 1, 6, 5, 4, 8, 7]
- B. [8, 7, 6, 5, 4, 3, 2, 1]
- C. [1, 2, 3, 4, 5, 6, 7, 8]
- D. [3, 2, 1, 6, 5, 4, 7, 8]
Correct answer: A. [3, 2, 1, 6, 5, 4, 8, 7]
Each group of 3 consecutive elements is reversed independently: {1,2,3}->{3,2,1}, {4,5,6}->{6,5,4}, and the final partial group {7,8} (only 2 elements) reverses to {8,7}, giving [3,2,1,6,5,4,8,7].
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? (A square matrix's main diagonal is mat[i][i]; its anti-diagonal is mat[i][n-1-i]. If…
- This function uses a counter that PERSISTS across calls (a static/global variable), starting at 3. What does each of the 7 calls print, in…
- What will be the output of the following pseudocode? Function isEven(Integer n) if (n == 0) return true end if return isOdd(n - 1) end…
- What will be the output of the following pseudocode? Integer c = 0 for (each i from 1 to 12) for (each j from i + 1 to 12) if ((i + j) mod…
- What will be the output of the following pseudocode? (& is bitwise AND, >> is right shift.) Integer n = 1958, c = 0 while (n > 0) c = c +…
- What will be the output of the following pseudocode? (One pass = one full sweep of the outer loop; indexed from 0.) Integer arr[6] = {89…