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 ?
if (!(x - 3 <= 0 && y + 2 <= 9))
{
x = x - 1;
y = y + 1;
}
System.out.println(x + "," + y);if (!(!(x - 3 > 0) && y + 2 < 9))
{
x = x - 1;
y = y + 1;
}
System.out.println(x + "," + y);if (!(x - 3 >= 0) || y + 2 > 9)
{
x = x - 1;
y = y + 1;
}
System.out.println(x + "," + y);if (!(x - 3 >= 0 && !(y + 2 < 9))
{
x = x - 1;
y = y + 1;
}
System.out.println(x + "," + y);
The correct answer is b. The boolean expression !(!(x - 3 > 0) && y + 2 < 9) can be simplified by
- distributing the initial not operation over the two terms in the expression to get
(x - 3 > 0) || !(y + 2 < 9) - Simplifying the second term by converting 'not
<' to '>=' - 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.