Nested if and Compound Booleans
Free AP Computer Science A multiple-choice practice for topics 2.4–2.6, Unit 2 (Selection and Iteration). Answer five random questions, check your work, and review the essential knowledge behind each one.
Unit 2: Selection and Iteration. Topics 2.4–2.6. This set has 55 questions. Each time the page loads you get five of them, chosen at random. Pick an answer, press Check answer, and read the explanation for every choice. Reload the page or use New questions for a fresh five.
Topics assessed
- 2.4 Nested if Statements
- 2.5 Compound Boolean Expressions
- 2.6 Comparing Boolean Expressions
Practice questions
Loading questions…
Essential knowledge for this set
These are the essential knowledge (EKS) statements from the AP Computer Science A Course and Exam Description that the 55 questions in this set assess, grouped by topic and learning objective. The table under each question links here.
Topic 2.4: Nested if Statements
2.4.A — Develop code to represent nested branching logical processes and determine the result of these processes.
- 2.4.A.1 Nested if statements consist of if, if-else, or if-else-if statements within if, if-else, or if-else-if statements.
- 2.4.A.2 The Boolean expression of the inner nested if statement is evaluated only if the Boolean expression of the outer if statement evaluates to true.
- 2.4.A.3 A multiway selection (if-else-if) is used when there are a series of expressions with different segments of code for each condition. Multiway selection is performed such that no more than one segment of code is executed based on the first expression that evaluates to true. If no expression evaluates to true and there is a trailing else statement, then the body of the else is executed.
Topic 2.5: Compound Boolean Expressions
2.5.A — Develop code to represent compound Boolean expressions and determine the result of these expressions.
- 2.5.A.1 Logical operators ! (not), && (and), and || (or) are used with Boolean expressions. The expression !a evaluates to true if a is false and evaluates to false otherwise. The expression a && b evaluates to true if both a and b are true and evaluates to false otherwise. The expression a || b evaluates to true if a is true, b is true, or both, and evaluates to false otherwise. The order of precedence for evaluating logical operators is ! (not), && (and), then || (or). An expression involving logical operators evaluates to a Boolean value.
- 2.5.A.2 Short-circuit evaluation occurs when the result of a logical operation using && or || can be determined by evaluating only the first Boolean expression. In this case, the second Boolean expression is not evaluated.
Topic 2.6: Comparing Boolean Expressions
2.6.A — Compare equivalent Boolean expressions.
- 2.6.A.1 Two Boolean expressions are equivalent if they evaluate to the same value in all cases. Truth tables can be used to prove Boolean expressions are equivalent.
- 2.6.A.2 De Morgan’s law can be applied to Boolean expressions to create equivalent Boolean expressions. Under De Morgan’s law, the Boolean expression !(a && b) is equivalent to !a || !b and the Boolean expression !(a || b) is equivalent to !a && !b.
2.6.B — Develop code to compare object references using Boolean expressions and determine the result of these expressions.
- 2.6.B.1 Two different variables can hold references to the same object. Object references can be compared using == and !=.
- 2.6.B.2 An object reference can be compared with null, using == or !=, to determine if the reference actually references an object.
- 2.6.B.3 Classes often define their own equals method, which can be used to specify the criteria for equivalency for two objects of the class. The equivalency of two objects is most often determined using attributes from the two objects.