AP CSA 2.4 Exercise 2: Nested if Statements Applied Practice

Unit 2: Selection and Iteration · Lesson 2.4 · Exercise 2

Nested if Statements: Applied Practice

Six scenarios on which if an else belongs to, and when nesting is the same as an and.

How this one is different

The exercise-1 editor asks you to nest. These six ask what the nesting means, since a dangling else attaches by rule rather than by indentation.

Applied Practice

6 questions · scenario driven · every answer is recorded for your teacher

Question 1 of 6Analyze
Code reads: if (a) if (b) doX(); else doY(); with no braces.
Which if owns the else?
Correct. Correct. This is the dangling else, and braces are the only reliable fix.
Incorrect. It compiles cleanly, which is what makes it a trap.
Incorrect. An else belongs to exactly one if.
Incorrect. Indentation carries no meaning to the compiler.
Question 2 of 6Apply
A program must act only when a user is both logged in AND an administrator, with no other branches.
What is true of nesting versus a compound condition?
Incorrect. Nesting expresses it too; it is just wordier.
Incorrect. With no else branches, the outcomes are identical.
Incorrect. A single && expresses the same requirement.
Correct. Correct. Nesting earns its place when the two levels need DIFFERENT else branches.
Question 3 of 6Analyze
An outer if tests age >= 18 and an inner if tests hasLicense, with an else on the inner if printing "needs a license".
What does a 15 year old with a license see?
Incorrect. Neither inner branch is reached.
Incorrect. That else lives inside the outer if and is unreachable when the outer test fails.
Correct. Correct, and it is the usual cause of "why does my program print nothing".
Incorrect. No error occurs; the block is simply skipped.
Question 4 of 6Apply
A decision has three levels of nesting and the deepest branch is the common case.
What restructuring usually helps?
Incorrect. Depth is the problem, not the solution.
Correct. Correct. Guard clauses trade nesting depth for a flat main path.
Incorrect. Flattening is right in spirit but loses behavior when the levels have distinct elses.
Incorrect. Repetition is unrelated to a decision.
Question 5 of 6Evaluate
A student adds braces to a nested if/else without changing any condition, and the program's output changes.
What does that prove?
Correct. Correct, and a behavior change on adding braces is the definitive symptom of a dangling else.
Incorrect. It compiled and ran both times.
Incorrect. The braces revealed the bug rather than causing it.
Incorrect. Braces group statements; they do not change evaluation.
Question 6 of 6Apply
A nested structure must classify a number as positive-even, positive-odd, or non-positive.
What is the cleanest arrangement?
Incorrect. A single expression cannot produce three distinct outcomes.
Incorrect. There is nothing to repeat.
Incorrect. Independent tests repeat the positivity check and can disagree after an edit.
Correct. Correct. The nesting mirrors the structure of the classification itself.

Where to go next

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]