AP CSA Unit-Level MCQ
Compound Conditions
Practice mode with 15 Java-focused questions, immediate answer checks, and explanations.
What compound conditions covers
Compound conditions join several tests into one decision. Range checks need && because a value must satisfy both bounds, while membership checks across separate values need ||. Reading a compound condition well means substituting a concrete value and evaluating each piece, rather than pattern-matching on how the code looks.
Where students lose points
Writing 0 < x < 10 as if Java supported chained comparisons is a compile error, and the correct form is x > 0 && x < 10. Students also produce impossible conditions such as x < 5 && x > 10, which is never true, or overly permissive ones such as x < 5 || x > 10 when they meant to exclude a range.
How it shows up on the AP exam
Unit 2 content that pairs directly with Boolean operators. Exam questions often present four similar conditions and ask which one correctly describes a stated rule, so precision matters more than speed.
For practice use only.
Compound Conditions MCQ Practice
Practice AP CSA compound conditions: combining range tests with && and ||, ordering checks safely, and avoiding conditions that can never be true.
Answered 0 of 15
Choose one answer.
Code Tracing 01 - Compound Conditions: track the final printed value.
int a = 18;
int b = 3;
boolean ok = a > 5 && b < 2;
System.out.print(ok);All 15 compound conditions questions
Work through the interactive quiz above first. This is the full compound conditions question set with worked solutions, so students can review any question after attempting it.
1.Code Tracing 01 - Compound Conditions: track the final printed value.
int a = 18; int b = 3; boolean ok = a > 5 && b < 2; System.out.print(ok);- false
- true
- The code does not compile.
- A runtime exception is thrown.
Show worked solution
Correct answer: false
An && expression is true only when both sides are true, so one false half decides the whole result.
2.Code Tracing 12 - Compound Conditions: follow the variable updates.
int a = 26; int b = 5; boolean ok = a > 7 || b > 4; System.out.print(ok);- A runtime exception is thrown.
- true
- false
- The code does not compile.
Show worked solution
Correct answer: true
An || expression needs only one true side, so it succeeds even when the other half fails.
3.Code Tracing 28 - Compound Conditions: trace the branch and loop path.
int a = 38; int b = 3; boolean ok = !(a < 10) && (b + 3 >= 10); System.out.print(ok);- A runtime exception is thrown.
- false
- true
- The code does not compile.
Show worked solution
Correct answer: false
The negation applies to the whole parenthesised comparison before && combines the two halves.
4.Code Tracing 39 - Compound Conditions: watch the index changes.
int a = 46; int b = 5; boolean ok = (a % 2 == 0 || b % 2 == 0) && 5 > 5; System.out.print(ok);- The code does not compile.
- A runtime exception is thrown.
- false
- true
Show worked solution
Correct answer: false
The parentheses force the || to resolve first, and its single boolean result is then combined with &&.
5.Code Tracing 50 - Compound Conditions: evaluate the state change step by step.
boolean left = 54 > 7; boolean right = 7 < 3; System.out.print(left != right);- false
- The code does not compile.
- A runtime exception is thrown.
- true
Show worked solution
Correct answer: true
Comparing two booleans with != is true exactly when they differ, which is an exclusive-or test.
6.What is printed by the following code segment?
int n = 25; boolean r = n > 10 && n < 30 && n % 5 == 0; System.out.print(r);- true
- false
- The code does not compile.
- 25
Show worked solution
Correct answer: true
All three conditions hold, and chained && requires every one of them.
7.What is printed by the following code segment?
int a = 5; int b = 15; boolean r = (a < 10 || b < 10) && (a > 3 && b > 3); System.out.print(r);- false
- true
- The code does not compile.
- 20
Show worked solution
Correct answer: true
The first group is true because a is small, and the second group is true because both exceed 3.
8.What is printed by the following code segment?
int count = 0; for (int i = 1; i <= 30; i++) { if (i % 2 == 0 && i % 3 == 0 && i % 5 != 0) { count++; } } System.out.print(count);- 5
- 3
- 4
- 6
Show worked solution
Correct answer: 4
The multiples of 6 up to 30 are 6, 12, 18, 24, and 30, and 30 is excluded because it is also a multiple of 5.
9.Which expression is true exactly when n lies in 1 through 9 or in 20 through 29?
- n >= 1 && n <= 9 || n >= 20 && n <= 29 && n != 25
- (n >= 1 || n <= 9) && (n >= 20 || n <= 29)
- n >= 1 && n <= 29
- (n >= 1 && n <= 9) || (n >= 20 && n <= 29)
Show worked solution
Correct answer: (n >= 1 && n <= 9) || (n >= 20 && n <= 29)
Two ranges joined by || each need their own pair of bounds, and the parentheses keep the groups separate.
10.What is printed by the following code segment?
String s = "code"; boolean r = s.length() >= 4 && s.charAt(0) == 'c' && s.substring(1, 2).equals("o"); System.out.print(r);- true
- false
- The code does not compile.
- code
Show worked solution
Correct answer: true
Each condition holds, and the length guard runs first so the later character accesses are safe.
11.What is printed by the following code segment?
int x = 8; boolean r = !(x < 5 || x > 10); System.out.print(r);- false
- true
- The code does not compile.
- 8
Show worked solution
Correct answer: true
Negating the outside-a-range test gives the inside-a-range test, and 8 lies between 5 and 10.
12.What is printed by the following code segment?
int a = 4; int b = 6; boolean r = a % 2 == 0 == (b % 2 == 0); System.out.print(r);- false
- The code does not compile.
- true
- 10
Show worked solution
Correct answer: true
Both parity tests are true, and comparing two boolean values with == asks whether they agree.
13.What is printed by the following code segment?
int n = 0; boolean r = n != 0 && 10 / n > 1 || n == 0; System.out.print(r);- false
- An exception is thrown.
- The code does not compile.
- true
Show worked solution
Correct answer: true
The && short-circuits to false without dividing, and the || then finds its second operand true.
14.What is printed by the following code segment?
int hours = 45; boolean full = hours >= 40 && hours <= 50; boolean over = hours > 40; System.out.print(full + " " + (full && over));- true true
- true false
- false true
- false false
Show worked solution
Correct answer: true true
Both conditions hold, so the conjunction of the two booleans is also true.
15.Which expression is true exactly when a triangle with sides a, b, and c is valid, meaning every pair of sides sums to more than the third?
- a + b > c || a + c > b || b + c > a
- a + b > c && a + c > b && b + c > a
- a + b + c > 0
- a > b && b > c
Show worked solution
Correct answer: a + b > c && a + c > b && b + c > a
All three inequalities must hold at once, so they are joined with && rather than ||.
