ProgrammingHardTCS NQT · Accenture pattern

What will be the output of the following pseudocode (finding the kth smallest element by sorting)? (Arrays are indexed from 0.)

What will be the output of the following pseudocode (finding the kth smallest element by sorting)? (Arrays are indexed from 0.)
Integer arr[6] = {7, 10, 4, 3, 20, 15}
Integer k = 3
sort arr ascending
Print arr[k - 1]
  1. A. 4
  2. B. 7
  3. C. 10
  4. D. 3

Correct answer: B. 7

Sorted ascending, arr becomes {3, 4, 7, 10, 15, 20}; the 3rd smallest (index k-1=2) is 7.

More Pseudocode and Code Tracing questions

Exams that ask this topic