Learn AP Comp Sci

Problem of the Day

Wednesday, May 13, 2026


Problem:

Consider the following method.

public static int doesWhat(int[] arr)
{
int i = arr.length - 1;
int result = 0;
while (i > 0)
{
if (arr[i] > arr[i - 1])
{
result = -1;
break;
}
i--;
}
return result;
}

What does this method do?

  1. Return the value of the smallest element in arr
  2. Return the value of the largest element in arr
  3. Return 0 if the elements are in increasing order, -1 otherwise
  4. Return 0 if the elements are in decreasing order, -1 otherwise
  5. Return an ArrayIndexOutOfBoundsException

Show solution:

The correct answer is d. The variable result is initially set to 0, and retains that value while the loop moves backwards through the array, as long as the current value is less than or equal to the value below it in the array. If we ever do find a value that is greater than the value below, result is set to -1 and we break out of the loop to return that value.