AP Computer Science A · Topic 3.4
else if Statements Practice
Part of Boolean Expressions and if Statements.
Practice questions
4
Sample questions
4 of 4 — sign in to practice the rest with adaptive difficulty and mastery tracking.
Sample 1difficulty 2/5
int score = 75; if (score >= 90) { System.out.println("A"); } else if (score >= 80) { System.out.println("B"); } else if (score >= 70) { System.out.println("C"); } else { System.out.println("F"); }What is printed?
- Acheck_circle
C
- B
B
- C
F
- D
A
Why
score is 75. The first two conditions (>=90, >=80) fail. The third (>=70) is true, so "C" is printed and the rest of the chain is skipped.
- A
Sample 2difficulty 3/5
int score = 95; if (score >= 70) { System.out.println("C"); } if (score >= 80) { System.out.println("B"); } if (score >= 90) { System.out.println("A"); }What is printed (each on its own line)?
- Acheck_circle
C, B, A on three lines
- B
A only
- C
C only
- D
A, B, C on three lines
Why
These are three separate if statements (not else if). All three conditions are true for score 95, so "C", "B", and "A" all print in that order.
- A
Sample 3difficulty 3/5
For grade computation, after <code>if (g >= 90)</code> returns A, the next <code>else if (g >= 80)</code> should have what condition?
- A
g == 80
- B
g >= 80 && g < 90 (redundant)
- Ccheck_circle
g >= 80 (suffices because earlier branch caught >= 90)
- D
g >= 80 || g < 90
Why
Cascading else-if eliminates the need for explicit upper bound.
- A
Sample 4difficulty 3/5
For <code>int x = 5;</code>:
<code>if (x > 10) System.out.println("A"); else if (x > 0) System.out.println("B"); else System.out.println("C"); </code></pre> What is printed?
- Acheck_circle
B
- B
A B
- C
A
- D
C
Why
5 > 10 false; 5 > 0 true → "B" prints; else doesn't run.
- A