AP Computer Science A • Unit 2.2

Boolean Expressions

Detailed answer explanations for the Unit 2.2 multiple-choice practice questions.

Q1B
Q2B
Q3D
Q4A
Q5C
Q6B
Q7A
Q8B
Q9C
Q10B
Q11A
Q12A
Q13C
Q14A
How to use this page: First try each question without looking at the explanation. Then compare your answer with the key and read why the other choices are not correct.

Answer Explanations

Q1

Consider the following code segment. What is printed as a result of executing the code segment?

int a = 12;
int b = 12;
System.out.println(a == b);
System.out.println(a != b);
Atrue followed by true
Btrue followed by false
Cfalse followed by true
Dfalse followed by false
Correct answer: B. Because a and b hold the same number, == produces true and != produces false.
Explanation

Primitive values are compared by value. Two int variables are equal when they hold the same number. The equality operator asks whether the values match, and the inequality operator asks the opposite question, so the two results are always opposites.

  • A is not correct: the second line uses !=, which asks whether the values differ; equal values make that comparison produce false.
  • C is not correct: a and b hold the same number, so == produces true rather than false on the first line.
  • D is not correct: both comparisons cannot be false at once, because == and != ask opposite questions about the same two values.
Q2

A room may contain no more than its stated capacity. Which expression tests whether the number admitted is at most the capacity? Which expression correctly represents the rule?

Aadmitted < capacity
Badmitted <= capacity
Cadmitted >= capacity
Dadmitted == capacity
Correct answer: B. A rule that allows no more than the capacity includes equality, so the correct operator is <= rather than a strict comparison.
Explanation

The wording of a rule often tells which operator to use. The words at most and no more than include equality and match <=, while at least and no less than match >=.

  • A is not correct: admitted < capacity excludes equality, so it reports false when the room is exactly full, which the rule still allows.
  • C is not correct: admitted >= capacity asks whether the room is full or over, which is the opposite of the stated limit.
  • D is not correct: admitted == capacity is true only at exactly one value, so it rejects every smaller number the rule permits.
Q3

Consider the following code segment. What is printed as a result of executing the code segment?

int cap = 40;
int inside = 37;
int line = 4;

System.out.println(inside < cap);
System.out.println(inside + line <= cap);
Atrue followed by true
Bfalse followed by true
Cfalse followed by false
Dtrue followed by false
Correct answer: D. inside is smaller than cap, but adding line pushes the sum past cap, so <= gives false.
Explanation

Java completes the arithmetic before applying the relational operator. The second line first adds inside and line, then compares that sum with cap. The sum is larger than 40, so the comparison produces false.

  • A is not correct: the second comparison is not true, because the sum of inside and line is larger than cap.
  • B is not correct: the first comparison is true, since 37 is smaller than 40 and < asks exactly that question.
  • C is not correct: the first line prints true, so both printed values cannot be false; only the second comparison fails.
Q4

Consider the following code segment. What is printed as a result of executing the code segment?

int n = 45;
System.out.println(n % 2 == 0);
System.out.println(n % 5 == 0);
Afalse followed by true
Btrue followed by false
Ctrue followed by true
Dfalse followed by false
Correct answer: A. Dividing 45 by 2 leaves a remainder, while dividing it by 5 leaves 0.
Explanation

To test whether one integer is evenly divisible by another, compare the remainder with zero. A result of true means nothing is left over, which is why n % 5 == 0 holds here but n % 2 == 0 does not.

  • B is not correct: the two results are reversed; 45 is not even, so the first comparison produces false rather than true.
  • C is not correct: the first comparison fails, because dividing 45 by 2 does not leave a remainder of 0.
  • D is not correct: the second comparison succeeds, because 45 is evenly divisible by 5 and leaves nothing over.
Q5

If int x = 7;, what is the value of x + 3 * 2 >= 13?

A13
B6
Ctrue
Dfalse
Correct answer: C. Multiplication happens before addition and before the comparison, so the expression compares 13 with 13 and produces true.
Explanation

Java evaluates multiplication before addition, and applies the relational operator after all of the arithmetic. In x + 3 * 2 >= 13 the product is found first, then the sum, and the comparison finally produces a boolean.

  • A is not correct: 13 is the value of the arithmetic on the left, not the value of the whole comparison, which is a boolean.
  • B is not correct: 6 is only a partial arithmetic result; a relational expression produces true or false, never a number.
  • D is not correct: >= includes equality, and the two sides are equal here, so the comparison produces true instead.
Q6

Which expression evaluates to true?

A(4 + 2) * 3 == 15
B4 + 2 * 3 == 10
C4 * 2 + 3 >= 12
D4 - 1 * 2 == 6
Correct answer: B. Multiplication is applied before addition, so the left side equals 10 and the equality comparison produces true.
Explanation

Java evaluates parentheses first, then multiplication, division, and remainder, then addition and subtraction, and only then the relational and equality operators. Checking each choice means finishing the arithmetic before comparing.

  • A is not correct: the parentheses are evaluated first, so the left side is 18 rather than 15, and the equality fails.
  • C is not correct: the product is added before the comparison, giving a value smaller than 12, so >= produces false.
  • D is not correct: multiplication happens before subtraction, so the left side is 2, which does not equal 6.
Q7

If int n = 18;, what is the value of n % 3 == 0?

Atrue
Bfalse
C3
D6
Correct answer: A. 18 is evenly divisible by 3, so the remainder is 0 and the comparison produces true.
Explanation

Remainder produces what is left over after division. Comparing that remainder with zero tests divisibility, and a result of true means nothing remains. The whole expression is a Boolean expression, so its value is true or false.

  • B is not correct: nothing is left over when 18 is divided by 3, so the comparison succeeds rather than failing.
  • C is not correct: 3 is the divisor written in the expression, not the value of the comparison, which is a boolean.
  • D is not correct: 6 would be a quotient from division, while this expression takes a remainder and then compares it with 0.
Q8

If int age = 15; and boolean ok = age + 1 >= 17;, what does ok store?

A15
Bfalse
Ctrue
D16
Correct answer: B. Adding 1 to 15 gives 16, which is not at least 17, so ok stores false.
Explanation

The arithmetic finishes before the relational operator is applied, and the resulting value is then stored. Storing works because the types match: the expression produces a boolean value, and a boolean variable holds exactly that type of value.

  • A is not correct: 15 is the value of age, not the result of the comparison, which is true or false.
  • C is not correct: 16 is smaller than 17, so the comparison fails even though >= includes equality.
  • D is not correct: 16 is the value of the arithmetic on the left; a boolean variable cannot store a number.
Q9

Suppose admitted = 38, waiting = 3, and capacity = 40. Which expression is true?

Aadmitted + waiting < capacity
Badmitted + waiting <= capacity
Cadmitted + waiting > capacity
Dadmitted == capacity
Correct answer: C. The sum of admitted and waiting is larger than capacity, so only the strict greater-than comparison produces true.
Explanation

Relational expressions may include arithmetic, and Java completes the addition before comparing. Once the sum is known, each choice asks a different question about it, and only one of those questions has the answer true.

  • A is not correct: admitted + waiting < capacity asks whether the sum is smaller, but the sum is larger than capacity.
  • B is not correct: admitted + waiting <= capacity allows equality, yet the sum is still larger than capacity, so it fails.
  • D is not correct: admitted == capacity compares only admitted, which is 38, with 40, and those two numbers differ.
Q10

Which expression evaluates to true?

A3 + 4 * 2 == 14
B(3 + 4) * 2 == 14
C18 / 3 + 1 != 7
D20 % 6 == 0
Correct answer: B. Parentheses are evaluated first, so the left side equals 14 and the equality comparison produces true.
Explanation

Java evaluates parentheses first, then multiplication, division, and remainder, then addition and subtraction, then the relational operators, and finally the equality operators. Working through each choice in that order shows which comparison holds.

  • A is not correct: without parentheses the multiplication happens first, so the left side is smaller than 14 and the equality fails.
  • C is not correct: the division and addition give exactly 7, so != asks whether the values differ and answers false.
  • D is not correct: 20 % 6 == 0 tests divisibility, and dividing 20 by 6 leaves 2 rather than nothing.
Q11

Which statement correctly stores the result of a comparison?

Aboolean full = admitted >= capacity;
Bboolean full == admitted >= capacity;
Cint full = admitted >= capacity;
Dboolean full != admitted >= capacity;
Correct answer: A. A comparison produces a boolean, so its result is placed with the assignment operator into a boolean variable.
Explanation

Storing a relational expression works because the types match. The expression produces a boolean value, and a boolean variable holds exactly that type of value. The assignment operator = is what stores the value in the variable.

  • B is not correct: == compares two values and produces true or false; it does not store anything in a variable.
  • C is not correct: an int variable holds a number, while the comparison on the right produces true or false.
  • D is not correct: != asks whether two values differ, so it also compares rather than storing the result of the comparison.
Q12

If int roomNo = 214;, what is the value of (roomNo + 6) % 10 == 0?

Atrue
Bfalse
C2
D0
Correct answer: A. Adding 6 to 214 gives a multiple of 10, so the remainder is 0 and the result is true.
Explanation

Java evaluates parentheses first, so the addition finishes before the remainder is taken and before the equality operator is applied. Comparing the remainder with zero tests whether the parenthesized sum is evenly divisible by 10.

  • B is not correct: nothing is left over after the parenthesized sum is divided by 10, so the comparison succeeds.
  • C is not correct: 2 is not the value of this expression; equality operators produce true or false, not numbers.
  • D is not correct: 0 is the remainder being compared, not the value of the whole expression, which is a boolean.
Q13

Suppose score = 84 and bonus = 6. Which expression tests whether the final score is at least 90?

Ascore + bonus > 90
Bscore = bonus + 90
Cscore + bonus >= 90
Dscore + bonus != 90
Correct answer: C. At least includes equality, so the final score is tested with >= after the bonus has been added.
Explanation

The wording of a rule points to the operator. At least and no less than include equality and use >=. Java completes the addition first, then applies the relational operator to the sum.

  • A is not correct: score + bonus > 90 excludes equality, so it fails when the final score is exactly 90.
  • B is not correct: score = bonus + 90 uses the assignment operator, which stores a value in a variable instead of comparing.
  • D is not correct: score + bonus != 90 asks whether the sum differs from 90, which is not the stated rule.
Q14

What is the value of 18 / 3 + 2 <= 7?

Afalse
Btrue
C8
D7
Correct answer: A. The division and addition finish first, giving 8, which is not less than or equal to 7.
Explanation

Java evaluates division before addition, and applies the relational operator last. The arithmetic gives 8 here, and <= asks whether that value is at most 7. It is not, so the answer is false.

  • B is not correct: the arithmetic result is larger than 7, so the comparison cannot produce true even though <= includes equality.
  • C is not correct: 8 is the value of the arithmetic on the left, not the value of the whole comparison.
  • D is not correct: 7 is the number being compared against, and a relational expression produces true or false.