ProgrammingVery hardAmazon OA · Infosys SP
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[7] = {7, 2, 9, 4, 1, 8, 5}
Integer pivot = a[6], i = -1
for (each j from 0 to 5)
if (a[j] <= pivot)
i = i + 1
swap(a[i], a[j])
end if
end for
swap(a[i + 1], a[6])
Print i + 1, a[0], a[6]- A. 3 2 7
- B. 4 2 9
- C. 3 7 5
- D. 2 2 7
Correct answer: A. 3 2 7
Lomuto partition around the pivot 5: the elements ≤ 5 (2, 4, 1) are swapped to the front in order, giving {2, 4, 1, 7, 9, 8, 5}; then the pivot swaps with a[3]: {2, 4, 1, 5, 9, 8, 7}. Output 3 2 7.
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? Integer a[9] = {3, 3, 4, 2, 4, 4, 2, 4, 2} Integer cand = 0, cnt = 0 for (each i from…
- What will be the output of the following pseudocode? Integer a[7] = {16, 17, 4, 3, 5, 2, 1} Integer mx = a[6], c = 1 for (each i from 5…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer nxt[6] = {3, 5, 0, 1, 2, 4} Integer x = 0, steps…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[6] = {4, 5, 2, 25, 7, 8}, res[6] Stack st =…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer h[12] = {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}…
- What will be the output of the following pseudocode? (code(c) gives a letter's character code) String s = "PROGRAMMING", r = "" Boolean…