Learn AP Comp Sci

Problem of the Day

Monday, March 2, 2026


Problem:

The int array vals stores a series of values. Which of the following code segments will reverse the order of that array of values?


  1. for (int i = 0; i < vals.length / 2; i++)
    {
    int j = vals.length - i - 1;
    int tmp = vals[i];
    vals[i] = vals[j];
    vals[j] = tmp;
    }

  2. int[] vals2 = new int[vals.length];
    int j = 0;
    for (int i = vals.length - 1; i >= 0; i--)
    {
    vals2[j] = vals[i];
    j++;
    }
    vals = vals2;

  3. int j = vals.length - 1;
    for (int i = 0; i < vals.length / 2; i++)
    {
    int tmp = vals[i];
    vals[i] = vals[j];
    vals[j] = tmp;
    j--;
    }
  1. I only
  2. II only
  3. I and II only
  4. I and III only
  5. I, II, and III

Show solution:

The correct answer is e. All three solutions will result in a reversal of the values in the array vals.