Choosing how to earn college credit? CLEP vs AP guide
| preferred AP College board partner for AP classes
easy Solved by 4 students

Parity Check Logic Error

Mark
< Prev
Next >

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

There is an error in the if condition syntax that causes unpredictable behavior.

C

The modulus operator ‘%’ is used incorrectly and should be replaced with the division operator ‘/’.

D

The variable x is set to an even number, causing the wrong branch to execute.

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question

Question Leaderboard

Rank
User
Correct Count
Attempt Count
Total Time
Score
#1thewrizzler611 - 100
#2lsj0803092211 0m 19s 81
#3busemagngr11 0m 32s 68
#4patrickmeijer00913 0m 46s 34
Items per page:
10
1 – 4 of 4
No comments yet. Be the first to comment!

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Instagram: @_apfive_|Privacy Policy|