AP CSP Day 33: Internet Protocols
Share
Practice Question
What is displayed after the following code runs?
x ← 15
y ← 10
IF x > y
{
IF x - y > 10
{
DISPLAY("far apart")
}
ELSE
{
DISPLAY("close")
}
}
ELSE
{
DISPLAY("y is bigger")
}First condition: x > y evaluates to 15 > 10 = true. Inside the nested IF: x - y > 10 evaluates to 15 - 10 = 5 > 10 = false. The ELSE branch executes, displaying "close".
A) The difference (5) is not greater than 10. C) The outer condition x > y is true, so the outer ELSE does not execute. B) One of the branches must execute because the outer IF/ELSE covers all cases.
Students evaluate the outer condition correctly but then rush through the nested condition without computing the actual difference, or they confuse which ELSE pairs with which IF.
In nested IF/ELSE structures, work from the outside in. Determine which outer branch executes first, then evaluate the inner conditions within that branch only.
Keep Practicing!
Consistent daily practice is the key to AP CSP success.
AP CSP Resources Get 1-on-1 Help