ProgrammingHardTCS NQT · Accenture pattern
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] = {61, 93, 89, 15, 55, 48, 56, 60}
for (each p from 0 to 0)
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. 61, 89, 15, 55, 48, 56, 60, 93
- B. 60, 56, 48, 55, 15, 89, 93, 61
- C. 61, 93, 89, 15, 55, 48, 56, 60
- D. 15, 48, 55, 56, 60, 61, 89, 93
Correct answer: A. 61, 89, 15, 55, 48, 56, 60, 93
This is 1 pass of bubble sort (each pass bubbles the next-largest unsorted element toward the end, with the inner bound shrinking by 1 per pass). After 1 pass starting from [61, 93, 89, 15, 55, 48, 56, 60], the array is [61, 89, 15, 55, 48, 56, 60, 93] — not fully sorted like [15, 48, 55, 56, 60, 61, 89, 93], since only 1 sweep ran.
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? (All variables are integers, so / is integer division. Array is sorted and indexed…
- 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 is the tightest time complexity of the following pseudocode, in terms of n? (Treat any constant like 3 as a fixed constant, not…
- 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 = 81, y = 10 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…