ProgrammingVery hardAccenture · Cognizant 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[7] = {22, 96, 64, 59, 14, 18, 43}
for (each p from 0 to 0)
for (each i from 0 to 5 - 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 6)
Print arr[i]
end for- A. 43, 18, 14, 59, 64, 96, 22
- B. 14, 18, 22, 43, 59, 64, 96
- C. 22, 96, 64, 59, 14, 18, 43
- D. 22, 64, 59, 14, 18, 43, 96
Correct answer: D. 22, 64, 59, 14, 18, 43, 96
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 [22, 96, 64, 59, 14, 18, 43], the array is [22, 64, 59, 14, 18, 43, 96] — not fully sorted like [14, 18, 22, 43, 59, 64, 96], 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 is the final content of the stack (bottom to top, left to right) after this sequence of operations on an initially empty stack? push…
- 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 4 as a fixed constant, not…
- Consider this naive recursive Fibonacci. How many TOTAL calls to fib (including the initial call) happen when computing fib(6)? Function…
- 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 = 90, y = 76 x = x ^ y y = x ^ y x = x ^ y Print x, y