Learn AP Comp Sci

Problem of the Day

Tuesday, January 6, 2026


Problem:

The static boolean method bothEvenOrOdd takes two integers a and b as parameters and returns true if both integers are even or both integers are odd—otherwise, it returns false. Which of the following methods will not perform correctly?


  1. public static boolean bothEvenOrOdd(int a, int b)
    {
    return ( a + b ) % 2 == 0;
    }

  2. public static boolean bothEvenOrOdd(int a, int b)
    {
    return ((a % 2) + (b % 2)) % 2 == 0;
    }

  3. public static boolean bothEvenOrOdd(int a, int b)
    {
    return a % 2 == b % 2;
    }

  4. public static boolean bothEvenOrOdd(int a, int b)
    {
    if (a % 2 == 0 && b % 2 == 0)
    return true;
    else
    return false;
    }

Show solution:

The correct answer is d—that method will not return true for the case where both integers are odd.