ProgrammingHardAccenture · TCS · Infosys
What will be the output of the following pseudocode? (One pass = one full sweep of the outer loop; indexed from 0.)
What will be the output of the following pseudocode? (One pass = one full sweep of the outer loop; indexed from 0.)
Integer arr[8] = {88, 56, 43, 74, 27, 78, 23, 66}
for (each p from 0 to 2)
for (each i from 0 to 6 - p)
if (arr[i] > arr[i + 1])
Integer t = arr[i]
arr[i] = arr[i + 1]
arr[i + 1] = t
end if
end for
end for
for (each i from 0 to 7)
Print arr[i]
end for- A. 88, 56, 43, 74, 27, 78, 23, 66
- B. 23, 27, 43, 56, 66, 74, 78, 88
- C. 66, 23, 78, 27, 74, 43, 56, 88
- D. 43, 27, 56, 23, 66, 74, 78, 88
Correct answer: D. 43, 27, 56, 23, 66, 74, 78, 88
This is 3 passes of bubble sort (each pass bubbles the next-largest unsorted element toward the end, with the inner bound shrinking by 1 per pass). After 3 passes starting from [88, 56, 43, 74, 27, 78, 23, 66], the array is [43, 27, 56, 23, 66, 74, 78, 88] — not fully sorted like [23, 27, 43, 56, 66, 74, 78, 88], since only 3 sweeps ran.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What is the final content of the queue (front to back, left to right) after this sequence of operations on an initially empty queue?…
- What will be the output of the following pseudocode? (Arrays are passed and assigned BY REFERENCE: b = a makes b point to the same array…
- What will be the output of the following pseudocode? (^ is bitwise XOR.) Integer x = 25, y = 20 x = x ^ y y = x ^ y x = x ^ y Print x, y
- 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 2. What does each of the 5 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…