44.2k views
5 votes
Identify the logic error in the code below. The code is supposed to display a single message “child”, “adult” or “senior” for any value of age:

if (age < 18) {
System.out.println("child");
} else if (age >= 18) {
System.out.println("adult");
} else if (age > 65) {
System.out.println("senior");
}
Question 1 options:

a)

no message for age = 65

b)

too many messages for age = 18

c)

no message for age < 65

d)

message “senior” will never be displayed

User Gdlmx
by
3.4k points

1 Answer

2 votes

If we take a look at the first else if block, the code runs whenever the age is greater than or equal to 18. This means that senior will never be displayed. Answer choice D is correct.

User Haael
by
3.2k points