AP CSP Day 55: Algorithm Complexity
Share
Practice Question
What is displayed after the following code runs?
x ← 7
y ← 3
z ← 5
IF x > y AND y > z
{
DISPLAY("A")
}
ELSE
{
IF x > z OR y > z
{
DISPLAY("B")
}
ELSE
{
DISPLAY("C")
}
}First condition: x > y AND y > z = (7 > 3) AND (3 > 5) = true AND false = false. The ELSE block executes. Inside ELSE: x > z OR y > z = (7 > 5) OR (3 > 5) = true OR false = true. "B" is displayed.
A) The AND condition fails because y (3) is not greater than z (5). C) The OR condition is true because x (7) is greater than z (5). B) Only one branch of each IF/ELSE pair executes.
Students evaluate the AND condition by checking only the first part (x > y = true) and assume the entire condition is true without checking the second part (y > z = false).
For compound Boolean conditions with AND, both parts must be true. For OR, only one part needs to be true. Check each part separately before combining.
Keep Practicing!
Consistent daily practice is the key to AP CSP success.
AP CSP Resources Get 1-on-1 Help