QUESTION IMAGE
Question
- (02.04 mc)
the code below uses ap exam pseudocode and is intended to swap the values of the variables firstflavor and secondflavor.
line 1: firstflavor ← secondflavor
line 2: temp ← firstflavor
line 3: secondflavor ← temp
which change, if any, is needed for the program to work as expected? (5 points)
○ swap line 2 with line 3.
○ swap line 1 with line 2.
○ swap line 1 with line 3.
○ no change is needed; the code works as expected.
Step1: Analyze current code flaw
Current Line 1 overwrites firstFlavor before its value is saved.
Step2: Identify correct swap order
A valid 3-step swap first saves the original value of firstFlavor to temp, then assigns firstFlavor to secondFlavor, then assigns secondFlavor to temp. Swapping Line 1 and Line 2 fixes this order:
temp ← firstFlavor(save original first value)firstFlavor ← secondFlavor(overwrite first with second)secondFlavor ← temp(overwrite second with saved first)
Step3: Verify fix
This corrected order successfully swaps the two variable values as intended.
Snap & solve any problem in the app
Get step-by-step solutions on Sovi AI
Photo-based solutions with guided steps
Explore more problems and detailed explanations
Swap Line 1 with Line 2.