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
- 3.1 Boolean expressions and relational operators
- 3.2 if, if-else, and else-if statements
- 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
What is the value of 5 > 3 && 2 > 4, which uses logical and?
- false
- true
- 1
- An error
Answer: A. false && requires both sides to be true; 2 > 4 is false.
What is printed? int x = 7; if (x > 5) System.out.print("A"); else System.out.print("B");
- A
- B
- AB
- Nothing
Answer: A. A 7 > 5 is true, so only the if branch runs.
For two String variables s1 and s2, s1 == s2 is true when
- they contain the same characters in any case
- they have the same length
- either one is null
- both refer to the same String object
Answer: D. both refer to the same String object == compares references (memory locations) for objects.
What is the value of 5 > 3 || 2 > 4, which uses logical or?
- false
- true
- 0
- An error
Answer: B. true || is true if at least one side is true.
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";
- A
- B
- BC
- C
Answer: B. B In an else-if chain, only the first true branch runs: 85 >= 80.
Given String a = new String("hi"); String b = new String("hi");, what are a == b and a.equals(b)?
- true and true
- false and true
- false and false
- true and false
Answer: B. false and true new creates two different objects, so == is false, but their contents are equal.
What is the value of !(4 == 4)?
- true
- 4
- An error
- false
Answer: D. false 4 == 4 is true, and ! negates it.
What is printed? int score = 85; if (score >= 70) print "C"; if (score >= 80) print "B"; (two separate if statements)
- CB
- C
- B
- BC
Answer: A. CB Separate if statements are each checked, so both conditions print.
Which is the correct way to check that a reference obj is not null before calling a method?
- if (obj.isReady() && obj != null)
- if (obj.equals(null))
- if (obj != null && obj.isReady())
- if (obj == "null")
Answer: C. if (obj != null && obj.isReady()) Short-circuiting stops before calling isReady() when obj is null.
Which expression is true exactly when int x is between 1 and 10 inclusive?
- x >= 1 || x <= 10
- 1 <= x <= 10
- x >= 1 && x <= 10
- 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.
More AP Computer Science A
- Unit 1: Primitive Types, Variables, and Expressions
- Unit 2: Using Objects: Strings, Math, and Wrappers
- Unit 4: Iteration
- Unit 5: Writing Classes
- Unit 6: Arrays
- Unit 7: ArrayList
- Unit 8: 2D Arrays
- Unit 9: Recursion, Searching, and Sorting
Studying for a test in your own class? Make a practice test from your class notes.