AP CSP Day 40: Side Effects & Mutation | Cycle 2

Key Concepts

Unintended mutation occurs when a procedure modifies a shared list as a side effect, changing data that other parts of the program expected to remain unchanged. This is especially problematic when the same list is passed to multiple procedures in sequence. AP CSP Cycle 2 side-effect questions present code where a procedure both returns a value and modifies an external list, and ask students to trace the state of the list after multiple procedure calls. Distinguishing intended from unintended mutations requires reading each procedure carefully for any operation that writes to a non-local variable.

📚 Study the Concept First (Optional) Click to expand ▼

Unintended Mutation: Tracing Shared State

Shared Mutable State

When a list is passed to a procedure and the procedure modifies that list, the original list in the calling code changes too. This is because lists are passed by reference in most languages, meaning both the parameter and the original variable point to the same data.

Tracing Multiple Mutations

When multiple procedures modify the same list in sequence, the list's state after each call becomes the input to the next. Students who only trace the final call without accounting for earlier mutations will get an incorrect result.

Common Trap: Assuming the original list is unchanged after a procedure call. Unless the procedure explicitly creates a new list or the problem states it works on a copy, assume the original is modified.
Exam Tip: Maintain a list state tracker alongside your variable trace table. Update the list contents after every procedure call that could modify it, even if the mutation is a side effect rather than the main purpose.
Big Idea 3: Algorithms & Programming
Cycle 2 • Day 40 Practice • Hard Difficulty
Focus: Side Effects & Mutation

Practice Question

What is displayed after the following code runs?

data ← [10, 20, 30, 40]
i ← 1
REPEAT UNTIL i > LENGTH(data)
{
   IF data[i] < 25
   {
      REMOVE(data, i)
   }
   ELSE
   {
      i ← i + 1
   }
}
DISPLAY(LENGTH(data))
Why This Answer?

Trace: data=[10,20,30,40], i=1. data[1]=10<25: REMOVE index 1 → data=[20,30,40], i stays 1. data[1]=20<25: REMOVE index 1 → data=[30,40], i stays 1. data[1]=30, not <25: i=2. data[2]=40, not <25: i=3. 3>LENGTH(data)=2: stop. LENGTH = 2.

Why Not the Others?

B) Both 10 and 20 are removed, leaving 2 elements, not 3. C) Two elements are removed, so the list does not keep all 4. D) 30 and 40 remain because they are not less than 25.

Common Mistake
Watch Out!

Students assume i always increments, which would skip elements after a removal. The key insight is that i only increments in the ELSE branch — when no removal occurs. This prevents skipping.

AP Exam Tip

Removing elements during list traversal requires careful index management. If i increments after every removal, the element that shifts into the removed position gets skipped.

Keep Practicing!

Consistent daily practice is the key to AP CSP success.

AP CSP Resources Get 1-on-1 Help
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.