AP CSA Unit 3: Boolean Expressions and if Statements

287 multiple-choice questions on AP Computer Science A Unit 3, Boolean Expressions and if Statements, organized by topic. They are not tied to any one class or teacher: they follow the College Board course framework. Learn the 37 key terms with the flashcards first, then test yourself. Every answer comes with a short explanation.

Take the practice test · Flashcards · All study tools

What it covers

  1. 3.1 Boolean expressions and relational operators
  2. 3.2 if, if-else, and else-if statements
  3. 3.3 Comparing objects

Key terms

3.1 Boolean expressions and relational operators

Relational operators
The six operators ==, !=, <, >, <=, and >=, which compare two values and produce a boolean result
Equality operator (==)
For primitives, true when the two values are the same; for references, true only when both refer to the same object
Inequality operator (!=)
The opposite of ==: true when two primitives differ, or when two references do not point to the same object
Logical AND (&&)
A binary operator that is true only when both of its operands are true
Logical OR
A binary operator, written as two vertical bars, that is true when at least one of its operands is true
Logical NOT (!)
A unary operator that evaluates to false when its operand is true, and to true when its operand is false
Compound boolean expression
A boolean expression that joins two or more conditions with AND or OR
Exclusive or
A compound condition that is true when exactly one of two boolean values is true
Truth table
A listing of a boolean expression's value for every combination of its operands
Boolean literals
The values true and false, which are the only literals of type boolean
Boolean expression
An expression that evaluates to true or false
Logical operators
&& (and), || (or), ! (not)
Short-circuit evaluation
Skipping the right operand when the left one already decides the result
De Morgan's laws
!(a && b) == !a || !b and !(a || b) == !a && !b

3.2 if, if-else, and else-if statements

if statement
A control structure that runs its body only when a boolean condition is true
if-else statement
A two-way choice that runs one body when the condition is true and a different body when it is false
else branch
The body of an if-else statement that runs when the condition is false
Selection statement
A statement such as if or if-else that chooses which code to run based on a condition
Statement block
One or more statements grouped inside a pair of curly braces so they form a single body
Dangling else
The rule that an else is paired with the nearest unmatched if, which can surprise you when an inner if has no braces
Empty statement
A lone semicolon, which is a statement that does nothing
Independent if statements
Consecutive if statements that are each evaluated on their own, so more than one body can run
Mutually exclusive conditions
Conditions of which at most one can be true at the same time
Boolean condition
The expression in parentheses after if, which Java requires to have type boolean
else-if chain
A sequence of conditions where only the first true branch runs
Nested conditional
An if statement inside another if or else

3.3 Comparing objects

Aliasing
Two references to the same object
Reference equality
A comparison of two references with ==, which is true only when both refer to the same object
Content equality
A comparison of two objects with equals, which for String is true when they hold the same characters
equals method
The instance method used to decide whether two objects should be treated as equal
Null test
A comparison of a reference with null, written == null or != null
Case-sensitive equality
The rule that String.equals treats an uppercase letter as different from the same letter in lowercase
Null-safe equals
Calling equals on a string that is known not to be null, such as "quit".equals(answer)
Shared string objects
String literals with the same characters refer to one shared String object
Object identity
Whether two references point to one object in memory; == tests this for reference types
Guarded condition
A compound condition that uses short-circuit AND or OR so a method is not called on a null reference
Reference comparison
Using == or != on two object references to test whether they refer to the same object

Sample practice questions

  1. What is the value of 5 > 3 && 2 > 4, which uses logical and?

    1. false
    2. true
    3. 1
    4. An error

    Answer: A. false && requires both sides to be true; 2 > 4 is false.

  2. What is printed? int x = 7; if (x > 5) System.out.print("A"); else System.out.print("B");

    1. A
    2. B
    3. AB
    4. Nothing

    Answer: A. A 7 > 5 is true, so only the if branch runs.

  3. For two String variables s1 and s2, s1 == s2 is true when

    1. they contain the same characters in any case
    2. they have the same length
    3. either one is null
    4. both refer to the same String object

    Answer: D. both refer to the same String object == compares references (memory locations) for objects.

  4. What is the value of 5 > 3 || 2 > 4, which uses logical or?

    1. false
    2. true
    3. 0
    4. An error

    Answer: B. true || is true if at least one side is true.

  5. What is printed? int score = 85; if (score >= 90) print "A"; else if (score >= 80) print "B"; else if (score >= 70) print "C"; else print "F";

    1. A
    2. B
    3. BC
    4. C

    Answer: B. B In an else-if chain, only the first true branch runs: 85 >= 80.

  6. Given String a = new String("hi"); String b = new String("hi");, what are a == b and a.equals(b)?

    1. true and true
    2. false and true
    3. false and false
    4. true and false

    Answer: B. false and true new creates two different objects, so == is false, but their contents are equal.

  7. What is the value of !(4 == 4)?

    1. true
    2. 4
    3. An error
    4. false

    Answer: D. false 4 == 4 is true, and ! negates it.

  8. What is printed? int score = 85; if (score >= 70) print "C"; if (score >= 80) print "B"; (two separate if statements)

    1. CB
    2. C
    3. B
    4. BC

    Answer: A. CB Separate if statements are each checked, so both conditions print.

  9. Which is the correct way to check that a reference obj is not null before calling a method?

    1. if (obj.isReady() && obj != null)
    2. if (obj.equals(null))
    3. if (obj != null && obj.isReady())
    4. if (obj == "null")

    Answer: C. if (obj != null && obj.isReady()) Short-circuiting stops before calling isReady() when obj is null.

  10. Which expression is true exactly when int x is between 1 and 10 inclusive?

    1. x >= 1 || x <= 10
    2. 1 <= x <= 10
    3. x >= 1 && x <= 10
    4. x > 1 && x < 10

    Answer: C. x >= 1 && x <= 10 Both conditions must hold, joined with &&. Java does not allow chained comparisons like 1 <= x <= 10.

Practice all 287 questions

More AP Computer Science A

Studying for a test in your own class? Make a practice test from your class notes.