Learn AP Comp Sci

Problem of the Day

Monday, December 8, 2025


Problem:

The method check given here analyzes sequences of integers stored in an array.

public static boolean check(int[] arr)
{
if (arr.length % 2 == 1)
return false;
for (int i = 0; i < arr.length - 1; i += 2)
if ( !(arr[i] % 2 == 1 && arr[i + 1] % 2 == 0))
return false;
return true;
}

What will be the boolean results of running these four sequences through the check method?

{1, 2, 3, 4, 5}
{1, 2, 3, 4, 5, 6}
{3, 1, 2, 3, 4, 5}
{3, 2, 3, 4, 7, 2}
  1. false, true, false, true
  2. false, true, false, false
  3. false, false, false, true
  4. false, true, true, true
  5. Show solution:

    The correct answer is a. By analyzing the boolean statement in the function we can see that only sequences with an even-number of integer values might be true. Also, the for-loop considers pairs of values, and checks to see if the first one is odd ( arr[i] % 2 == 1 ) and the second one is even. If this is ever not the case, the sequence is false. If all pairs of values in the array satisfy this condition, the sequence earns a boolean result of true.