Skip to content

The following method, which is intended to find the maximum element of the parameter array, is incorrect. public int max (int[]

  1. A.{ int max = 0; for (int i=0; i<a.length; i++) { if(a[i]>max) { max = a[i]; } } return max; } (a) It fails whenever the array contains a 0.
  2. B.It fails whenever the array contains a negative number.
  3. C.It fails whenever the array contains only negative numbers.
  4. D.It fails whenever the first element of the array a is the largest.
Show answer

Answer: C. It fails whenever the array contains only negative numbers.

If all elements are negative, the condition a[i]>0 is never met, returning incorrect max 0.