If Else Control Flow Error
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.
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | hitrishabhatia | 1 | 1 | 0m 00s | 100 |
| #2 | y.seong2027 | 1 | 3 | 1m 56s | -36 |
| #3 | psak12 | 1 | 1 | 9m 21s | -461 |
Items per page:
10
1 – 3 of 3
APFIVE