Sovi.AI - AI Math Tutor

Scan to solve math questions

QUESTION IMAGE

7. (02.04 mc) the code below uses ap exam pseudocode and is intended to…

Question

  1. (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.

Explanation:

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:

  1. temp ← firstFlavor (save original first value)
  2. firstFlavor ← secondFlavor (overwrite first with second)
  3. secondFlavor ← temp (overwrite second with saved first)

Step3: Verify fix

This corrected order successfully swaps the two variable values as intended.

Answer:

Swap Line 1 with Line 2.