| preferred AP College board partner for AP classes
medium Solved by 4 students
If Else Control Flow Error
< Prev
Next >

Consider the following code segment. When executed, the program prints both “Age discount” and “No discount”. Which of the following statements best explains the logical error in the code?

public class DiscountEligibility {
    public static void main(String[] args) {
        int age = 23;
        boolean member = false;
        if(age < 25)
            System.out.println("Age discount");
        if(member)
            System.out.println("Membership discount");
        else
            System.out.println("No discount");
    }
}
A

Missing braces cause the else to be associated only with the second if, leading to combined outputs.

B

The conditions should be combined using a logical AND (&&).

C

The member condition should be checked before the age condition.

D

The logical NOT operator is missing in the member check.

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

Question Leaderboard

Rank
User
Correct Count
Attempt Count
Time
Score
#1hitrishabhatia11 0m 00s 100
#2y.seong202713 1m 56s -36
#3psak1211 9m 21s -461
Items per page:
10
1 – 3 of 3

AI Tutor

How can I help?

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