PrepGo

AP Computer Science A Practice Quiz: Compound Boolean Expressions

Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026

Test your understanding with short quizzes. This quiz has 9 questions to check your progress.

Question 1 of 9

Consider the following code segment: boolean a = true; boolean b = false; What is the result of the expression `a && !b`?

All Questions (9)

Consider the following code segment: boolean a = true; boolean b = false; What is the result of the expression `a && !b`?

A) true

B) false

C) The code will produce a compile error.

D) The result is non-deterministic.

Correct Answer: A

The expression is `a && !b`. First, the `!` (not) operator is evaluated due to higher precedence. `!b` becomes `!false`, which is `true`. The expression then becomes `a && true`, which is `true && true`. The result of this `&&` (and) operation is `true`.

Given the following integer variables: int x = 10; int y = 20; Which of the following Boolean expressions evaluates to `false`?

A) x < 20 || y > 10

B) x == 10 && y == 20

C) !(x > y)

D) x > 10 || y < 20

Correct Answer: D

Let's evaluate each option: A: `(10 < 20)` is true. `true || (y > 10)` results in `true`. B: `(10 == 10)` is true and `(20 == 20)` is true. `true && true` results in `true`. C: `(10 > 20)` is false. `!(false)` results in `true`. D: `(10 > 10)` is false and `(20 < 20)` is false. `false || false` results in `false`.

What is the result of the following Boolean expression? `true || false && !true`

A) true

B) false

C) A syntax error will occur.

D) The expression cannot be evaluated.

Correct Answer: A

According to the order of precedence for logical operators, `!` (not) is evaluated first, then `&&` (and), and finally `||` (or). 1. `!true` evaluates to `false`. 2. The expression becomes `true || false && false`. 3. `false && false` evaluates to `false`. 4. The expression becomes `true || false`. 5. `true || false` evaluates to `true`.

Consider the following code segment: int x = 5; String s = null; boolean result = (x < 4) && (s.length() > 0); What happens when this code is executed?

A) The value of `result` becomes `true`.

B) The value of `result` becomes `false`.

C) A NullPointerException occurs.

D) A compile-time error occurs.

Correct Answer: B

This question tests short-circuit evaluation. The first part of the `&&` expression, `(x < 4)`, evaluates to `false` because 5 is not less than 4. Since the first operand of an `&&` operation is `false`, the entire expression must be `false`. Due to short-circuit evaluation, the second part, `(s.length() > 0)`, is never evaluated. This prevents a NullPointerException that would have occurred if the code tried to call the `.length()` method on a null reference.

Consider the following code segment: int k = 0; boolean check = (k > 0) || (++k == 1); What is the value of `k` after this code is executed?

A) 0

B) 1

C) 2

D) The code is invalid.

Correct Answer: B

This question tests short-circuit evaluation with the `||` operator. The first operand, `(k > 0)`, evaluates to `false` since `k` is initially 0. Because the first operand is `false`, the second operand must be evaluated to determine the result of the `||` expression. The second operand, `(++k == 1)`, first increments `k` to 1 and then compares it to 1. The comparison `(1 == 1)` is `true`. Therefore, the final value of `k` is 1.

Which statement best describes the order of precedence for logical operators?

A) Operators are evaluated from left to right, with `&&` and `||` having equal precedence.

B) The `||` (or) operator is evaluated first, then `&&` (and), then `!` (not).

C) The `!` (not) operator is evaluated first, then `&&` (and), then `||` (or).

D) The `&&` (and) operator is evaluated first, then `||` (or), then `!` (not).

Correct Answer: C

The order of precedence for logical operators dictates the order of evaluation in a compound expression. The `!` (not) operator has the highest precedence, followed by `&&` (and), and finally `||` (or) has the lowest precedence.

Consider the following code segment: int a = 10; int b = 5; boolean result = (a == 10 || b > 10) && (b == 5); What is the value of `result`?

A) true

B) false

C) A compile-time error occurs.

D) A runtime error occurs.

Correct Answer: A

Parentheses dictate that the `||` expression is evaluated first. `(a == 10 || b > 10)` becomes `(true || false)`, which evaluates to `true`. The expression then simplifies to `true && (b == 5)`. The second part, `(b == 5)`, is `true`. The final expression is `true && true`, which results in `true`.

Which Boolean expression correctly represents the condition that an integer `year` is a leap year? A year is a leap year if it is divisible by 4, unless it is divisible by 100 but not by 400.

A) year % 4 == 0 && year % 100 != 0 && year % 400 == 0

B) year % 4 == 0 || (year % 100 != 0 && year % 400 == 0)

C) (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)

D) year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)

Correct Answer: C

The condition for a leap year has two parts connected by an 'or'. The first part is that the year is divisible by 4 AND not divisible by 100. This is represented by `(year % 4 == 0 && year % 100 != 0)`. The second part is the exception for century years, which are leap years if they are divisible by 400. This is represented by `(year % 400 == 0)`. Combining these with an `||` (or) gives the correct complete expression.

Consider the following code segment: int count = 10; boolean check = (10 > 5) || (++count < 15); What is the value of `count` after this code is executed?

A) 10

B) 11

C) 12

D) 15

Correct Answer: A

This question demonstrates short-circuit evaluation with the `||` (or) operator. The first operand, `(10 > 5)`, evaluates to `true`. Since the first part of an `||` expression is `true`, the entire expression is guaranteed to be `true`, regardless of the second operand's value. Therefore, the second operand, `(++count < 15)`, is not evaluated. The `++count` operation is never performed, and the value of `count` remains 10.