Learn AP Comp Sci

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?

  1. Return the smallest item in array
  2. Return the largest item in the array
  3. Return each successive item in the list, in order
  4. Return each successive item in the list, in reverse order

Show solution:

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.