| preferred AP College board partner for AP classes
AP Computer Science A/Unit 1: Primitive Types
Start Practice TestPractice Test
About Exam
easy Solved by 3 students
Parity Check Logic Error
< 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

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.

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

Question Leaderboard

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

AI Tutor

How can I help?

APFIVE © 2020.
Email: [email protected]|Privacy Policy