AP Computer Science A · Topic 3.3
if-else Statements Practice
Part of Boolean Expressions and if Statements.
Practice questions
6
Sample questions
5 of 6 — sign in to practice the rest with adaptive difficulty and mastery tracking.
Sample 1difficulty 2/5
if (g >= 90) grade = 'A'; if (g >= 80) grade = 'B'; if (g >= 70) grade = 'C';Why does a 95 produce grade 'C'?
- A
char cannot hold a grade
- B
g must be a String
- Ccheck_circle
All ifs run; later assignments overwrite earlier ones. Use else if.
- D
Nothing is wrong
Why
Independent ifs all execute; with g=95 every condition is true, so the last assignment ('C') wins. Chain with else if for mutual exclusion.
- A
Sample 2difficulty 2/5
What does this print when x = 5?
<code>if (x > 10) System.out.println("big"); else System.out.println("small"); </code></pre>
- A
Nothing
- B
big small
- Ccheck_circle
small
- D
big
Why
5 is not > 10, so else branch runs.
- A
Sample 3difficulty 3/5
int x = 10; String label = "none"; if (x > 0) { label = "positive"; } if (x > 5) { label = "big"; } System.out.println(label);What is printed?
- A
none
- B
positive big
- Ccheck_circle
big
- D
positive
Why
These are two SEPARATE if statements (no else). With x = 10, both run. label is set to "positive" then overwritten to "big". Final value printed is "big".
- A
Sample 4difficulty 3/5
int n = 5; if (n > 0) if (n > 10) System.out.println("big"); else System.out.println("small");What is printed?
- A
big
- Bcheck_circle
small
- C
big small
- D
nothing
Why
The else binds to the nearest unmatched if (n > 10). Since n = 5, n > 0 is true (enter), n > 10 is false (else), so "small" prints.
- A
Sample 5difficulty 4/5
For score = 85:
<code>String grade; if (score >= 90) grade = "A"; else if (score >= 80) grade = "B"; else if (score >= 70) grade = "C"; else grade = "F"; </code></pre> grade is
- A
C
- Bcheck_circle
B
- C
A
- D
F
Why
85 >= 80 (after failing 90 check) → "B".
- A