PrepGo

AP Computer Science A Practice Quiz: if Statements

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

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

Question 1 of 10

According to the provided text, what is the primary purpose of a selection statement?

All Questions (10)

According to the provided text, what is the primary purpose of a selection statement?

A) To repeat a segment of code a specific number of times.

B) To store data in a variable for later use.

C) To change the sequential execution of statements based on a condition.

D) To group related code into a reusable block called a method.

Correct Answer: C

The content states, 'Selection statements change the sequential execution of statements.' This means they alter the normal top-to-bottom flow of a program.

When is the body of a one-way selection (if statement) executed?

A) Only when the associated Boolean expression evaluates to true.

B) Only when the associated Boolean expression evaluates to false.

C) It is always executed, regardless of the Boolean expression.

D) It is executed once if the expression is true and twice if it is false.

Correct Answer: A

The provided content specifies that for a one-way selection (if statement), 'The body is executed only when the Boolean expression is true.'

A programmer needs to display 'Access Granted' if a password is correct and 'Access Denied' if it is incorrect. Which logical structure is most appropriate for this situation?

A) A one-way selection (if statement)

B) A two-way selection (if-else statement)

C) Sequential execution

D) A variable declaration

Correct Answer: B

This scenario requires two different actions based on the condition (password is correct or not). The content states that a two-way selection (if-else) 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.'

Consider the following code segment. What is printed to the console? ```java int temperature = 95; if (temperature > 90) { System.out.print("Hot"); } System.out.print(" Day"); ```

A) Hot

B) Day

C) Hot Day

D) Nothing is printed.

Correct Answer: C

The Boolean expression `temperature > 90` (95 > 90) is true. Therefore, the body of the if statement is executed, printing 'Hot'. The program then continues its sequential execution and prints the next statement, ' Day'.

Consider the following code segment. What is printed to the console? ```java int score = 50; if (score > 60) { System.out.print("Pass"); } System.out.print("Complete"); ```

A) Pass

B) Complete

C) PassComplete

D) Nothing is printed.

Correct Answer: B

The Boolean expression `score > 60` (50 > 60) is false. Therefore, the body of the one-way if statement is skipped. The program continues its sequential execution and prints 'Complete'.

Consider the following code segment. What is printed to the console? ```java int x = 20; if (x % 2 == 0) { System.out.print("Even"); } else { System.out.print("Odd"); } ```

A) Even

B) Odd

C) EvenOdd

D) The code produces an error.

Correct Answer: A

The Boolean expression `x % 2 == 0` checks if x is even. Since 20 divided by 2 has a remainder of 0, the expression is true. The code inside the `if` block is executed, printing 'Even', and the `else` block is skipped.

Consider the following code segment. What is printed to the console? ```java boolean isAsleep = true; if (isAsleep) { System.out.print("Quiet"); } else { System.out.print("Loud"); } ```

A) Quiet

B) Loud

C) QuietLoud

D) Nothing is printed.

Correct Answer: A

The variable `isAsleep` is true, so the Boolean expression in the if statement is true. Consequently, the first block of code is executed, printing 'Quiet', and the else block is ignored.

Which of the following best describes the difference between one-way and two-way selection?

A) One-way selection uses an `if`, while two-way selection uses a `while` loop.

B) One-way selection executes code for a true condition, while two-way selection executes code for both true and false conditions.

C) One-way selection can only check integer values, while two-way selection can check Boolean values.

D) One-way selection is for branching processes, while two-way selection is for sequential processes.

Correct Answer: B

Based on the content, a one-way selection (`if`) executes a code segment only when a condition is true. A two-way selection (`if-else`) provides two segments of code: one for when the condition is true and another for when it is false.

The condition in a selection statement, which determines which branch of code is executed, is known as what type of expression?

A) An arithmetic expression

B) A string expression

C) A method expression

D) A Boolean expression

Correct Answer: D

The provided text explicitly mentions that selection statements execute code based on a 'Boolean expression' being true or false.

What is the result of executing the following code segment? ```java int balance = 100; int withdrawal = 50; balance = balance - withdrawal; if (balance < 50) { System.out.print("Low"); } else { System.out.print("OK"); } ```

A) Low

B) OK

C) LowOK

D) The code will not compile.

Correct Answer: B

First, `balance` is updated: `balance = 100 - 50`, so `balance` becomes 50. The if statement then checks the condition `balance < 50`. Since 50 is not less than 50, the condition is false. Therefore, the code in the `else` block is executed, printing 'OK'.