AP CSP Day 28: Heuristic Algorithms
Share
Practice Question
What is displayed after the following code runs?
PROCEDURE mystery(a, b)
{
RETURN a + b * 2
}
DISPLAY(mystery(3, 4))Standard order of operations applies: multiplication before addition. Inside the procedure with a=3 and b=4: b * 2 = 4 * 2 = 8, then a + 8 = 3 + 8 = 11.
A) 14 would result from (3+4)*2, which incorrectly adds before multiplying. C) 10 does not correspond to any valid evaluation. B) 7 would be a + b without the multiplication.
Students add a + b first (getting 7) and then multiply by 2 (getting 14), ignoring the standard mathematical order of operations where multiplication precedes addition.
Standard math order of operations applies in AP CSP pseudocode: multiplication and division before addition and subtraction. Use parentheses mentally to clarify ambiguous expressions.
Keep Practicing!
Consistent daily practice is the key to AP CSP success.
AP CSP Resources Get 1-on-1 Help