AP CSP 3.7 Exercise 2: Nested Conditionals Applied Challenge

Big Idea 3: Algorithms and Programming · Topic 3.7 · Exercise 2

Nested Conditionals: Applied Challenge

Six traces where the ORDER of the tests, not the tests themselves, decides the answer.

How this one is different

A nested conditional whose branches are all individually correct can still be wrong overall. Order is the whole subject here.

Applied Practice

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

Question 1 of 6Debug
A grading program always reports C for every passing score.
What is wrong with this segment?
IF (score >= 70) { DISPLAY "C" } ELSE IF (score >= 80) { DISPLAY "B" } ELSE IF (score >= 90) { DISPLAY "A" }
Correct. ELSE IF only runs when every earlier test failed, so a broad first test makes the stricter later tests unreachable. Ordering from highest threshold down fixes it.
Incorrect. ELSE IF chains are entirely valid.
Incorrect. Each label matches its own threshold. The problem is which threshold is tested first.
Incorrect. The comparison operators are correct. The sequence they appear in is not.
Question 2 of 6Trace
What does this segment display when age is 20 and hasTicket is false?
IF (age >= 18) { IF (hasTicket) { DISPLAY "enter" } ELSE { DISPLAY "buy ticket" } } ELSE { DISPLAY "too young" }
Incorrect. This branch runs only when the outer test fails, which it does not.
Incorrect. Every path here reaches a DISPLAY.
Incorrect. Entering requires the inner condition to be true, and hasTicket is false.
Correct. The outer test passes because 20 is at least 18, so control moves inside, where the false inner test selects the ELSE.
Question 3 of 6Analyze
In an ELSE IF chain, when does the third branch execute?
Incorrect. A chain never requires multiple conditions to hold at once.
Incorrect. This describes separate IF statements, not a chain.
Correct. A chain is exclusive. Reaching a later branch means every earlier test already failed, and then its own test must still pass.
Incorrect. Once a branch runs, the rest of the chain is skipped entirely.
Question 4 of 6Apply
Which ordering correctly assigns A for 90 and above, B for 80 to 89, and C for 70 to 79?
Incorrect. The broadest test first swallows every score, which is the classic bug.
Correct. Starting from the strictest threshold means each later branch is only reached by scores the earlier ones already excluded.
Incorrect. Equality matches three individual scores and nothing else.
Incorrect. The 80 test runs before the 90 test, so a 95 is labeled B.
Question 5 of 6Trace
What does this segment display when n is 12?
IF (n MOD 3 = 0) { IF (n MOD 4 = 0) { DISPLAY "both" } ELSE { DISPLAY "three only" } } ELSE { DISPLAY "neither" }
Correct. 12 divides evenly by 3 and by 4, so the outer test passes and the inner test passes too.
Incorrect. This runs only when the inner test fails, and 12 MOD 4 is 0.
Incorrect. This requires the outer test to fail, and 12 MOD 3 is 0.
Incorrect. The structure never reaches a four only case, since the outer test gates everything.
Question 6 of 6Evaluate
A student replaces a nested conditional with a single condition using AND.
When is that rewrite safe?
Incorrect. Which variables are tested does not affect whether the rewrite preserves behavior.
Incorrect. The rewrite is safe in the common no ELSE case, so a blanket never is wrong.
Incorrect. An inner ELSE has no equivalent in a flattened AND, so the rewrite loses a case.
Correct. With no inner ELSE, the only path that produces an action is both tests passing, which is exactly what AND expresses.

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]