Learn AP Comp Sci

☰

Problem of the Day

Monday, October 26, 2026


Problem:

Two int variables have been initialized as shown here.

int x = 3;
int y = 7;

Which of the following code segments will produce the output 2,8 ?


  1. if (!(x - 3 <= 0 && y + 2 <= 9))
    {
    x = x - 1;
    y = y + 1;
    }
    System.out.println(x + "," + y);

  2. if (!(!(x - 3 > 0) && y + 2 < 9))
    {
    x = x - 1;
    y = y + 1;
    }
    System.out.println(x + "," + y);

  3. if (!(x - 3 >= 0) || y + 2 > 9)
    {
    x = x - 1;
    y = y + 1;
    }
    System.out.println(x + "," + y);

  4. if (!(x - 3 >= 0 && !(y + 2 < 9))

    {
    x = x - 1;
    y = y + 1;
    }
    System.out.println(x + "," + y);

Show solution:

The correct answer is b. The boolean expression !(!(x - 3 > 0) && y + 2 < 9) can be simplified by

  1. distributing the initial not operation over the two terms in the expression to get (x - 3 > 0) || !(y + 2 < 9)
  2. Simplifying the second term by converting 'not <' to '>='
  3. Substituting in values to the resulting expression and evaluating it: (3 - 3 > 0) || 7 + 2 >= 9

The first expression is not true, but the second one, 7 + 2 >= 9, is true, and the 'or' operation || results in a boolean expression that evaluates as true.

As a result, x and y are modified accordingly to produce the output 2,8.