CompSci.rocks

Boolean Expressions and if Statements

Free AP Computer Science A multiple-choice practice for topics 2.1–2.3, 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.1–2.3. This set has 57 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.1 Algorithms with Selection and Repetition
  • 2.2 Boolean Expressions
  • 2.3 if Statements

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 57 questions in this set assess, grouped by topic and learning objective. The table under each question links here.

Topic 2.1: Algorithms with Selection and Repetition

2.1.A — Represent patterns and algorithms that involve selection and repetition found in everyday life using written language or diagrams.

  • 2.1.A.1 The building blocks of algorithms include sequencing, selection, and repetition.
  • 2.1.A.2 Algorithms can contain selection, through decision making, and repetition, via looping.
  • 2.1.A.3 Selection occurs when a choice of how the execution of an algorithm will proceed is based on a true or false decision.
  • 2.1.A.4 Repetition is when a process repeats itself until a desired outcome is reached.
  • 2.1.A.5 The order in which sequencing, selection, and repetition are used contributes to the outcome of the algorithm.

Topic 2.2: Boolean Expressions

2.2.A — Develop code to create Boolean expressions with relational operators and determine the result of these expressions.

  • 2.2.A.1 Values can be compared using the relational operators == and != to determine whether the values are the same. With primitive types, this compares the actual primitive values. With reference types, this compares the object references.
  • 2.2.A.2 Numeric values can be compared using the relational operators <, >, <=, and >= to determine the relationship between the values.
  • 2.2.A.3 An expression involving relational operators evaluates to a Boolean value.

Topic 2.3: if Statements

2.3.A — Develop code to represent branching logical processes by using selection statements and determine the result of these processes.

  • 2.3.A.1 Selection statements change the sequential execution of statements.
  • 2.3.A.2 An if statement is a type of selection statement that affects the flow of control by executing different segments of code based on the value of a Boolean expression.
  • 2.3.A.3 A one-way selection (if statement) is used when there is a segment of code to execute under a certain condition. In this case, the body is executed only when the Boolean expression is true.
  • 2.3.A.4 A two-way selection (if-else statement) is used when there are two segments of code—one to be executed when the Boolean expression is true and another segment for when the Boolean expression is false. In this case, the body of the if is executed when the Boolean expression is true, and the body of the else is executed when the Boolean expression is false.