ProgrammingHardProduct company OA · Infosys pattern
What will be the output of the following pseudocode? (Arrays are indexed from 0.)
What will be the output of the following pseudocode? (Arrays are indexed from 0.)
Integer a[5] = {1, 2, 3, 4, 5}, t
for (i = 0 to 4)
t = a[i]
a[i] = a[4 - i]
a[4 - i] = t
end for
Print a[0], a[1], a[2], a[3], a[4]- A. 5 4 3 2 1
- B. 1 2 3 4 5
- C. 5 2 3 4 1
- D. 1 4 3 2 5
Correct answer: B. 1 2 3 4 5
The loop runs over the WHOLE array instead of stopping at the middle. i = 0, 1 reverse the array (5 4 3 2 1), i = 2 swaps the middle with itself, and i = 3, 4 swap the same pairs back. The array ends unchanged. The correct reversal loop runs i from 0 to n/2 − 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? (Arrays are indexed from 0.) Integer a[5] = {5, 1, 8, 2, 3}, c = 0 for (i = 0 to 3)…
- What will be the output of the following pseudocode? (A static variable is initialised once and keeps its value between calls; operands…
- What will be the output of the following pseudocode? (^ is bitwise XOR, & is AND, >> is a right shift.) Integer x = 29, y = 30 Integer gx…
- What will be the output of the following pseudocode? (Arrays are indexed from 0; the matrix has 3 rows and 4 columns.) Integer m[3][4] =…
- What will be the output of the following pseudocode? (calls counts every call of f, including the first.) Integer calls = 0 Function…
- What will be the output of the following pseudocode? (/ is integer division; the division in the loop is always exact.) Integer n = 10, c…