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
All Questions (9)
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.
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.
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".
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) 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.
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".
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`.
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.
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".