AP® Computer Science A Selection and Iteration Practice Questions
Work through AP Computer Science A practice questions on Selection and Iteration, then check each answer against a worked solution.
Free · no sign-up Open questions need no account. Sign up or log in to open the rest.
Subtopic: Boolean Expressions Clear filter
Question 162
Question 163
Question 164
What is printed?
int a = 4, b = 7, c = 10;
boolean p = !(a < b && b < c);
boolean q = !(a < b) || !(b < c);
boolean r = a >= b || b >= c;
System.out.println(p + " " + q + " " + r + " " + (p == q && q == r));Main Solution
Let's evaluate each boolean variable step by step with a = 4, b = 7, c = 10.
Evaluate p:
p = !(a < b && b < c)
a < b→4 < 7→trueb < c→7 < 10→truetrue && true→true!(true)→ false
Evaluate q:
q = !(a < b) || !(b < c)
!(a < b)→!(4 < 7)→!(true)→false!(b < c)→!(7 < 10)→!(true)→falsefalse || false→ false
Evaluate r:
r = a >= b || b >= c
a >= b→4 >= 7→falseb >= c→7 >= 10→falsefalse || false→ false
Evaluate (p == q && q == r):
p == q→false == false→trueq == r→false == false→truetrue && true→ true
So the output is: false false false true, which matches option D.
Teacher Explanation
This question tests De Morgan's Law and boolean expression evaluation. Note that p = !(a < b && b < c) is logically equivalent to !(a < b) || !(b < c) by De Morgan's Law, which is exactly q. Therefore p and q will always be equal. Since all three booleans evaluate to false, the final expression (p == q && q == r) is true.
Education Coach Advice
When evaluating boolean expressions on the AP exam, work inside-out: evaluate the innermost sub-expressions first, then apply negations and logical operators. For De Morgan's Law, remember: !(A && B) equals !A || !B, and !(A || B) equals !A && !B. Also, when you see a final expression comparing booleans with ==, substitute the computed values and evaluate carefully — don't assume the result matches the individual values.
Traps
A common trap is option A (true true true true): students might incorrectly negate the entire compound expression or misapply De Morgan's Law, thinking the negation of a true AND expression yields true. Another trap is option C (false false false false): students might forget that p == q && q == r compares the boolean values — since all three are false, the equality checks both return true, making the last value true, not false.
Answer images
Question 165
What is printed?
int x = 6;
boolean a = x % 2 == 0 && x % 4 == 0;
boolean b = x % 3 == 0 || x % 4 == 0;
boolean c = !(x % 8 == 0) || (x % 2 == 0);
System.out.println(a + " " + b + " " + c);Main Solution
Let's evaluate each boolean expression with x = 6:
Evaluating a:
a = x % 2 == 0 && x % 4 == 0
x % 2 == 0→6 % 2 = 0→truex % 4 == 0→6 % 4 = 2→2 == 0→falsetrue && false→false
Evaluating b:
b = x % 3 == 0 || x % 4 == 0
x % 3 == 0→6 % 3 = 0→true- Since the left side is
true, short-circuit evaluation makesb = trueimmediately. true
Evaluating c:
c = !(x % 8 == 0) || (x % 2 == 0)
x % 8 == 0→6 % 8 = 6→6 == 0→false!(false)→true- Since the left side is
true, short-circuit evaluation makesc = trueimmediately. true
Therefore, System.out.println(a + " " + b + " " + c) prints: false true true
The correct answer is B.
Teacher Explanation
This question tests students' ability to evaluate compound boolean expressions involving the modulo operator (%), logical AND (&&), logical OR (||), and logical NOT (!). The key insight for a is that while 6 is divisible by 2, it is NOT divisible by 4, so the AND expression is false. For b, 6 is divisible by 3, making the OR true. For c, 6 is not divisible by 8, so !(false) is true, making the whole OR expression true.
Education Coach Advice
AP Exam Strategy: When evaluating compound boolean expressions, work through each sub-expression one at a time and write down intermediate results. Pay special attention to the && vs || distinction — AND requires BOTH sides to be true, while OR only requires ONE side. Also remember that ! (NOT) flips the boolean value. For modulo, recall that a % b == 0 means a is evenly divisible by b.
Traps
Common traps:
- Option A (true true true): Students may incorrectly evaluate
aastruebecause 6 is even (divisible by 2), forgetting that the AND also requires divisibility by 4, which 6 does not satisfy. - Option C (false false true): Students may incorrectly evaluate
basfalseby only checkingx % 4 == 0(which is false) and forgetting thatx % 3 == 0is true, making the OR true. - Option D (true false true): A combination of both errors above — misreading
aas true andbas false.
Answer images
Question 166
What is printed?
int a = 7, b = 3, c = 5;
int result = 0;
if (a > b + c) {
result = 1;
} else if (a > b && a > c) {
if (b + c > a + 1) {
result = 2;
} else {
result = 3;
}
} else {
result = 4;
}
System.out.println(result);Main Solution
Step-by-step trace with a = 7, b = 3, c = 5:
- First condition:
a > b + c→7 > 3 + 5→7 > 8→ false. Skip the first block. - else-if condition:
a > b && a > c→7 > 3 && 7 > 5→true && true→ true. Enter this block. - Inner if condition:
b + c > a + 1→3 + 5 > 7 + 1→8 > 8→ false. Skip inner if, go to inner else. - Inner else:
result = 3. System.out.println(result)prints 3.
The correct answer is C) 3.
Teacher Explanation
This question tests students' ability to trace nested if/else-if/else logic with compound boolean conditions. The key insight is that the first condition (7 > 8) is false, the else-if condition (7 > 3 && 7 > 5) is true, but the inner condition (8 > 8) is false (strict inequality), so the inner else executes, setting result to 3.
Education Coach Advice
AP Exam Strategy: When tracing nested conditionals, evaluate each condition carefully one at a time. Write down the values of all variables first (a=7, b=3, c=5), then substitute into each boolean expression. Pay special attention to strict inequality (>) vs. greater-than-or-equal (≥) — a single character difference can change the entire outcome. Work top-to-bottom and stop as soon as you find the first true branch.
Traps
Common traps:
- Option B (2): Students may incorrectly evaluate
b + c > a + 1as8 > 8being true (confusing > with ≥). Since it is strict greater-than, 8 is NOT greater than 8, so result = 2 is never reached. - Option A (1): Students may miscalculate
b + cas something other than 8, making the first condition seem true. - Option D (4): Students may fail to recognize that the else-if condition is true, falling through to the final else.
Answer images
Question 167
What is printed?
int a = 0, b = 5, c = 3;
boolean p = (a != 0) && (b / a > 1);
boolean q = (a == 0) || (b / a > 1);
boolean r = (c > b) || (a != 0) && (b > c);
System.out.println(p + " " + q + " " + r);Main Solution
Let's trace through the code with a = 0, b = 5, c = 3.
Evaluating p:
boolean p = (a != 0) && (b / a > 1);
The left operand of && is (a != 0) → (0 != 0) → false.
Because Java uses short-circuit evaluation with &&, if the left side is false, the right side is never evaluated. So b / a (which would be division by zero) is never executed.
Therefore, p = false.
Evaluating q:
boolean q = (a == 0) || (b / a > 1);
The left operand of || is (a == 0) → (0 == 0) → true.
Because Java uses short-circuit evaluation with ||, if the left side is true, the right side is never evaluated. So again, b / a is never executed.
Therefore, q = true.
Evaluating r:
boolean r = (c > b) || (a != 0) && (b > c);
Operator precedence: && has higher precedence than ||, so this is parsed as:
(c > b) || ((a != 0) && (b > c))
(c > b)→(3 > 5)→ false- Since left of
||is false, evaluate right side:(a != 0) && (b > c) (a != 0)→(0 != 0)→ false- Short-circuit: right side of
&¬ evaluated. - So
(a != 0) && (b > c)→ false false || false→ false
Therefore, r = false.
Output:
System.out.println(p + " " + q + " " + r); prints: false true false
The correct answer is C.
Teacher Explanation
This question tests two key Java concepts: short-circuit evaluation and operator precedence. Students must recognize that && short-circuits on a false left operand and || short-circuits on a true left operand, preventing the division-by-zero runtime error. Additionally, students must know that && has higher precedence than ||, so the expression for r groups as (c > b) || ((a != 0) && (b > c)).
Education Coach Advice
On the AP CSA exam, always check for short-circuit evaluation when you see && or || — especially when one operand could cause a runtime error (like division by zero). Also remember: && binds more tightly than ||, just like multiplication before addition. When in doubt, add parentheses mentally to clarify grouping before tracing the logic.
Traps
Option D (Runtime error) is the most tempting wrong answer. Students who do not know about short-circuit evaluation will assume b / a (i.e., 5 / 0) is always evaluated, causing an ArithmeticException. However, Java's && and || operators short-circuit, so the division is never reached in either p or q.
Option A (false true true) is a trap for students who misapply operator precedence in r, treating it as ((c > b) || (a != 0)) && (b > c) or making an error in the boolean logic.
Answer images
Question 168
What is printed?
int x = 12;
String label = "";
if (x % 2 == 0 && x % 3 == 0) {
label = "A";
}
if (x % 4 == 0 && x % 3 == 0) {
label = label + "B";
}
if (x % 6 == 0) {
label = label + "C";
} else {
label = label + "D";
}
System.out.println(label);Main Solution
Let's trace through the code step by step with x = 12:
- Initialization:
x = 12,label = "" - First if:
x % 2 == 0→12 % 2 = 0✓ ANDx % 3 == 0→12 % 3 = 0✓ → condition istrue, solabel = "A" - Second if:
x % 4 == 0→12 % 4 = 0✓ ANDx % 3 == 0→12 % 3 = 0✓ → condition istrue, solabel = "A" + "B" = "AB" - Third if/else:
x % 6 == 0→12 % 6 = 0✓ → condition istrue, so theifbranch executes:label = "AB" + "C" = "ABC". Theelsebranch is skipped. - Output:
System.out.println(label)prints ABC
The correct answer is C: ABC.
Teacher Explanation
This question tests students' ability to trace through sequential (non-mutually-exclusive) if statements and an if/else block. Key insight: the first two if statements are independent — both can execute. The third is an if/else, so only one branch runs. Since 12 is divisible by 2, 3, 4, and 6, all three if conditions are true, and the else is skipped, yielding "ABC".
Education Coach Advice
AP Exam Strategy: When tracing code with multiple independent if statements, remember that each one is evaluated separately — unlike if/else if chains. Always compute each modulo operation carefully. Write out the value of label after each block to avoid losing track of string concatenation.
Traps
Common traps:
- Option A (AB): Students may think the third
if/elseblock doesn't execute at all, or forget to append "C". In fact,12 % 6 == 0is true, so "C" is appended. - Option B (ABD): Students may incorrectly evaluate
12 % 6 == 0as false and take theelsebranch, appending "D" instead of "C". - Option D (AC): Students may think the second
ifcondition (x % 4 == 0 && x % 3 == 0) is false. Since12 % 4 = 0and12 % 3 = 0, both are true and "B" is appended.
Answer images
Question 169
What is printed?
int score = 78;
String grade = "";
if (score >= 90) {
grade = "A";
} else if (score >= 80) {
grade = "B";
} else if (score >= 70) {
grade = "C";
} else {
grade = "F";
}
int bonus = score % 10;
if (bonus >= 5) {
grade = grade + "+";
}
System.out.println(grade + " " + bonus);Main Solution
Step-by-step trace:
score = 78,grade = ""- Check
score >= 90: 78 >= 90 → false - Check
score >= 80: 78 >= 80 → false - Check
score >= 70: 78 >= 70 → true →grade = "C" bonus = score % 10 = 78 % 10 = 8- Check
bonus >= 5: 8 >= 5 → true →grade = grade + "+" = "C+" System.out.println(grade + " " + bonus)prints: C+ 8
The correct answer is D: C+ 8.
Teacher Explanation
This question tests students' ability to trace through a multi-branch if/else-if/else structure and a subsequent if statement. The key insight is that 78 falls in the 70–79 range (grade = "C"), and 78 % 10 = 8, which is ≥ 5, so a "+" is appended. The final println concatenates the modified grade string with a space and the integer bonus value.
Education Coach Advice
When tracing if/else-if chains, evaluate each condition in order and stop at the first true branch. Then continue tracing any subsequent (separate) if statements — they are not part of the else-if chain. Also remember that % gives the remainder, not a rounded value. Write out each variable's value after each step to avoid errors.
Traps
Common traps:
- Option A (B 8): Students may think 78 rounds up to the 80s range, triggering the
score >= 80branch. But 78 is strictly less than 80, so this branch is skipped. - Option B (C 8): Students may correctly identify grade as "C" but forget to check the bonus condition. Since 8 >= 5, the "+" is appended.
- Option C (B+ 8): Combines both errors — wrong grade letter AND correct bonus logic.
Answer images
Question 170
int x = 5, y = 10;
boolean p = !(x > 3 && y < 15);
boolean q = x <= 3 || y >= 15;
boolean r = !(x > 3) || !(y < 15);
System.out.println(p + " " + (p == q) + " " + (q == r));Answer images
AP® and Advanced Placement® are registered trademarks of the College Board, which is not affiliated with, and does not endorse, Verityprep. Names are used for identification purposes only.

