PrepGo

AP Computer Science Principles Practice Quiz: Nested Conditionals

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

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

Question 1 of 7

Which of the following best defines a nested conditional statement?

All Questions (7)

Which of the following best defines a nested conditional statement?

A) A series of conditional statements executed one after another.

B) A conditional statement that is placed inside the block of another conditional statement.

C) A loop that contains a conditional statement.

D) A conditional statement that can only evaluate to true.

Correct Answer: B

Based on the provided content, 'Nested conditional statements consist of conditional statements within conditional statements.' This means one conditional is placed inside another.

Consider the following code segment. What is printed to the console? int x = 10; int y = 5; if (x > 5) { if (y < 3) { System.out.println("Apple"); } else { System.out.println("Banana"); } } else { System.out.println("Cherry"); }

A) Apple

B) Banana

C) Cherry

D) Nothing is printed.

Correct Answer: B

The outer condition `x > 5` (10 > 5) is true. The program then evaluates the inner conditional. The inner condition `y < 3` (5 < 3) is false. Therefore, the `else` block of the inner conditional is executed, printing 'Banana'.

Consider the following code segment. What is the final value of the variable `status`? int score = 88; boolean hasExtraCredit = false; String status = "Pending"; if (score >= 90) { status = "A"; } else { if (hasExtraCredit) { status = "B+"; } else { status = "B"; } }

A) Pending

B) A

C) B+

D) B

Correct Answer: D

The first condition `score >= 90` (88 >= 90) is false, so the outer `else` block is executed. Inside this block, the nested condition `hasExtraCredit` (which is false) is checked. Since it is false, the inner `else` block is executed, setting `status` to 'B'.

Which set of values for `temperature` and `isRaining` will result in the output 'Wear a jacket' from the following code segment? if (temperature < 60) { if (isRaining) { System.out.println("Wear a raincoat"); } else { System.out.println("Wear a jacket"); } } else { System.out.println("Wear a t-shirt"); }

A) temperature = 50, isRaining = true

B) temperature = 70, isRaining = false

C) temperature = 50, isRaining = false

D) temperature = 70, isRaining = true

Correct Answer: C

To print 'Wear a jacket', the outer condition `temperature < 60` must be true, and the inner condition `isRaining` must be false. Option C (temperature = 50, isRaining = false) is the only one that satisfies both conditions.

What is printed by the following code segment? int a = 5; int b = 10; int c = 15; if (a < b) { if (c < b) { System.out.println("Red"); } else { if (a < c) { System.out.println("Green"); } } } else { System.out.println("Blue"); }

A) Red

B) Green

C) Blue

D) Nothing is printed.

Correct Answer: B

The first condition `a < b` (5 < 10) is true, so the outer `if` block is executed. The next condition `c < b` (15 < 10) is false, so its `else` block is executed. Inside that `else` block, the condition `a < c` (5 < 15) is true, which causes 'Green' to be printed.

Which of the following code snippets correctly demonstrates the structure of a nested conditional statement?

A) if (condition1) { ... } if (condition2) { ... }

B) if (condition1) { if (condition2) { ... } }

C) if (condition1) { ... } else if (condition2) { ... }

D) while (condition1) { if (condition2) { ... } }

Correct Answer: B

A nested conditional statement is defined as a conditional statement within another conditional statement. Option B shows an `if` statement for `condition2` placed directly inside the code block of the `if` statement for `condition1`, which is the correct structure.

Consider the following code segment. What is the final value of `z`? int x = 10; int y = 10; int z = 0; if (x >= 10) { if (y > 10) { z = 1; } else { z = 2; } } else { if (y == 10) { z = 3; } else { z = 4; } }

A) 1

B) 2

C) 3

D) 4

Correct Answer: B

The outer condition `x >= 10` (10 >= 10) is true, so the program enters the first main block. The inner condition `y > 10` (10 > 10) is false. Therefore, the `else` part of this inner block is executed, setting `z` to 2. The entire outer `else` block (which would set z to 3 or 4) is skipped.