Compound Boolean Expressions

AP Computer Science A· difficulty 3/5

int n = 85;
String grade;
if (n >= 60) grade = "D";
else if (n >= 70) grade = "C";
else if (n >= 80) grade = "B";
else if (n >= 90) grade = "A";
else grade = "F";
System.out.println(grade);

What is printed?

  • A

    D

    check_circle
  • B

    B

  • C

    A

  • D

    F

Explanation

The chain is in WRONG ORDER. The first matching branch is taken. n = 85 satisfies n >= 60, so grade = "D" and the rest are skipped. A common bug — the lowest threshold should be checked LAST.

Want 10 more like this — adaptive to your weak spots?

Related questions