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?
- Return the value of the smallest element in
arr - Return the value of the largest element in
arr - Return
0if the elements are in increasing order,-1otherwise - Return
0if the elements are in decreasing order,-1otherwise - Return an
ArrayIndexOutOfBoundsException
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.