The following method, which is intended to find the maximum element of the parameter array, is incorrect. When does it fail? public int max (int[]…

The following method, which is intended to find the maximum element of the parameter array, is incorrect. When does it fail?

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

From the Combined Bank Officer (IT) October 2024 – Preliminary (BIBM) exam, question 90 of 95.

More from this paper