ProgrammingHardAccenture · Cognizant pattern

What will be the output of the following pseudocode (maximum product of any two elements)? (Arrays are indexed from 0.)

What will be the output of the following pseudocode (maximum product of any two elements)? (Arrays are indexed from 0.)
Integer arr[8] = {3, -1, 4, 1, 5, 9, 2, 6}
sort arr ascending
Integer n = length(arr)
Print arr[n - 1] * arr[n - 2]
  1. A. 45
  2. B. 54
  3. C. 56
  4. D. 63

Correct answer: B. 54

After sorting ascending, the two largest elements are 9 and 6 (the last two positions), and their product is 9*6=54.

More Pseudocode and Code Tracing questions

Exams that ask this topic