Parity Check Logic Error
The following code segment is intended to print whether the integer x is odd or even. The code compiles and runs but contains a logical error that produces incorrect output.
public class ParityBug {
public static void main(String[] args) {
int x = 10;
if (x % 2 == 1) {
System.out.println("x is even");
} else {
System.out.println("x is odd");
}
}
}
Which of the following statements best describes the error?
A
The output messages in the if-else block are reversed relative to the condition tested.
B
The variable x is set to an even number, causing the wrong branch to execute.
C
The modulus operator ‘%’ is used incorrectly and should be replaced with the division operator ‘/’.
D
There is an error in the if condition syntax that causes unpredictable behavior.
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | thewrizzler6 | 1 | 1 | 0m 00s | 100 |
| #2 | lsj08030922 | 1 | 1 | 0m 19s | 81 |
| #3 | patrickmeijer009 | 1 | 3 | 0m 46s | 34 |
Items per page:
10
1 – 3 of 3
APFIVE