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[8] = {52, 38, 76, 1, 13, 66, 34, 24}
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. 24, 34, 66, 13, 1, 76, 38, 52
- B. 52, 38, 76, 1, 13, 66, 34, 24
- C. 1, 13, 24, 34, 38, 52, 66, 76
- D. 38, 52, 1, 13, 66, 34, 24, 76
Correct answer: D. 38, 52, 1, 13, 66, 34, 24, 76
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 [52, 38, 76, 1, 13, 66, 34, 24], the array is [38, 52, 1, 13, 66, 34, 24, 76] — not fully sorted like [1, 13, 24, 34, 38, 52, 66, 76], 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 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 = 68, y = 64 x = x ^ y y = x ^ y x = x ^ y Print x, y
- 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? (All variables are integers, so / is integer division.) Function f(Integer n, Integer…
- What will be the output of the following pseudocode? Integer c = 0 for (each i from 1 to 14) for (each j from i + 1 to 14) if ((i + j) mod…