Problem of the Day
Friday, November 28, 2025
Problem:
Consider the static method foo shown here.
/**
* Precondition: arr has a length > 0
*/
public static int foo(int[] arr)
{
int s = arr[0];
for (int i = 1; i < arr.length; i++)
arr[i - 1] = arr[i];
arr[arr.length - 1] = s;
return s;
}
What do calls to the method foo do?
- Return the smallest item in array
- Return the largest item in the array
- Return each successive item in the list, in order
- Return each successive item in the list, in reverse order
The correct answer is c. Each time the method is called, the item at position 0 is saves as s, and all other elements of the array advanced by one position. Finally the item that was formerly at index 0, now stored in s, is placed at the rear of the array, and also returned as the result of the call. Successive calls will work their way through the array, and continue to cycle through its elements in order.