ProgrammingHardAccenture · Cognizant pattern
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 arr[8] = {3, 1, 4, 1, 5, 9, 2, 6}
Integer n = 8
for (each i from 0 to n - 2)
Integer minIdx = i
for (each j from i + 1 to n - 1)
if (arr[j] < arr[minIdx])
minIdx = j
end if
end for
swap(arr[i], arr[minIdx])
end for
Print arr- A. [1, 1, 3, 4, 5, 2, 6, 9]
- B. [1, 2, 3, 4, 5, 6, 9, 1]
- C. [1, 1, 2, 3, 4, 5, 6, 9]
- D. [3, 1, 4, 1, 5, 9, 2, 6]
Correct answer: C. [1, 1, 2, 3, 4, 5, 6, 9]
This is selection sort: on each outer pass, it finds the minimum of the unsorted remainder and swaps it into place, eventually producing the fully sorted array [1, 1, 2, 3, 4, 5, 6, 9].
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? Function countdownSum(Integer n) if (n == 0) return 0 end if return n +…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Integer n = 16, c = 0, i = n…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Integer n = 1024, c = 0 while…
- What is the time complexity of the following pseudocode, in terms of n, where the array has n elements? for (each i from 0 to n - 1) for…
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer arr[6] = {5, 2, 9, 1, 5, 6} for (each i from 1…
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer arr[5] = {2, 4, 6, 8, 10} Integer prefix[6]…