PrepGo

AP Computer Science A Practice Quiz: Nested 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 9 questions to check your progress.

Question 1 of 9

Consider the following code segment. int x = 15; int y = 20; if (x < 20) { if (y > 15) { x = x + 5; } } What is the value of x after this code segment has executed?

All Questions (9)

Consider the following code segment. int x = 15; int y = 20; if (x < 20) { if (y > 15) { x = x + 5; } } What is the value of x after this code segment has executed?

A) 15

B) 20

C) 25

D) The code will not compile.

Correct Answer: B

The outer condition `x < 20` (15 < 20) is true, so the program enters the outer if-block. The inner condition `y > 15` (20 > 15) is also true. Therefore, the statement `x = x + 5;` is executed, changing the value of x from 15 to 20.

Which statement best describes a nested if statement based on its logical structure?

A) A series of independent if statements that are checked one after another.

B) An if statement that is placed inside the code block of another if, if-else, or if-else-if statement.

C) A structure that guarantees at least one block of code will always be executed.

D) A multiway selection where multiple code segments can be executed if their conditions are true.

Correct Answer: B

This is the definition of a nested if statement. An inner conditional statement's execution is dependent on the outer conditional statement evaluating to true. Option D is incorrect because in a multiway selection (if-else-if), only the first true condition's code segment is executed.

Analyze the following code snippet. int score = 75; String result = "Pending"; if (score >= 50) { if (score >= 90) { result = "Excellent"; } else { result = "Pass"; } } else { result = "Fail"; } What is the value of result after the code executes?

A) Pending

B) Excellent

C) Pass

D) Fail

Correct Answer: C

The outer condition `score >= 50` (75 >= 50) is true. The program then evaluates the nested if-else statement. The inner condition `score >= 90` (75 >= 90) is false. Therefore, the inner else block is executed, and the value of `result` is set to "Pass".

Consider the following code segment, which is intended to determine a shipping cost. int weight = 25; double cost = 10.0; if (weight > 0) { if (weight > 50) { cost = 25.0; } else if (weight > 20) { cost = 15.0; } else if (weight > 10) { cost = 12.0; } } What is the final value of cost?

A) 10.0

B) 12.0

C) 15.0

D) 25.0

Correct Answer: C

The outer condition `weight > 0` (25 > 0) is true. The program enters the nested multiway selection. The first inner condition `weight > 50` (25 > 50) is false. The second inner condition `weight > 20` (25 > 20) is true. Its code block `cost = 15.0;` is executed. Because this is an if-else-if structure, no more segments are checked, even though `weight > 10` is also true. The final value of cost is 15.0.

A program needs to check if a number `n` is both a positive number and an even number. Consider the nested structure below: if (n > 0) { if (n % 2 == 0) { // n is positive and even } } Which of the following single if statements represents an equivalent logical process?

A) if (n > 0 || n % 2 == 0)

B) if (n > 0 && n % 2 == 0)

C) if (n > 0)

D) if (n % 2 != 1)

Correct Answer: B

The nested structure requires the outer condition (`n > 0`) AND the inner condition (`n % 2 == 0`) to both be true to reach the target code block. The logical AND operator (`&&`) checks if both conditions are true in a single expression, making it logically equivalent.

What is printed by the following code segment? int a = 10; int b = 5; if (a == 10) { System.out.print("A"); if (b > 10) { System.out.print("B"); } else { System.out.print("C"); } } else { System.out.print("D"); if (a > b) { System.out.print("E"); } }

A) AC

B) AB

C) DE

D) A

Correct Answer: A

The first condition `a == 10` is true, so "A" is printed and the outer else block is ignored. Inside the first if-block, the nested condition `b > 10` (5 > 10) is false. Therefore, the nested else block is executed, and "C" is printed. The final output is "AC".

Examine this code segment carefully. int val = 1; int num = 10; if (num > 5) { val = 2; if (val == 1) { num = 5; } else { num = 0; } } else { val = 3; } What are the final values of val and num?

A) val is 1, num is 10

B) val is 2, num is 5

C) val is 2, num is 0

D) val is 3, num is 10

Correct Answer: C

The outer condition `num > 5` (10 > 5) is true. Inside this block, `val` is immediately updated to 2. The next line is the nested condition `if (val == 1)`. At this point in the execution, `val` is 2, so this condition is false. The nested else block is executed, setting `num` to 0. The final values are `val = 2` and `num = 0`.

In a multiway selection statement like `if (c1) { ... } else if (c2) { ... } else if (c3) { ... }`, when is the condition `c3` evaluated?

A) It is always evaluated.

B) Only if c1 is true.

C) Only if both c1 and c2 are false.

D) Only if c1 is false and c2 is true.

Correct Answer: C

A key feature of a multiway selection (if-else-if) is that conditions are evaluated in order, and the process stops as soon as a true condition is found. Therefore, `c3` will only be checked if all preceding conditions (`c1` and `c2`) have evaluated to false.

Consider the code below. int temp = 30; boolean isHumid = false; String activity = "Wait"; if (temp > 25) { if (isHumid) { activity = "Swim"; } else { activity = "Hike"; } } What is the value of `activity` after execution?

A) Wait

B) Swim

C) Hike

D) The code produces an error.

Correct Answer: C

The outer condition `temp > 25` (30 > 25) is true. The program proceeds to the nested if-else statement. The inner condition `isHumid` evaluates to false. Consequently, the inner else block is executed, and the value of `activity` is updated to "Hike".