Computer Science I / State, Mutation, and Side Effects
Practice question · Put in order

Order these events so that every stated outcome is true at the moment it is written.

Hints
  1. Each line states what the names hold at that instant, so only one order makes every line true.
  2. The copy must be taken at the exact moment the list holds [5, 6], neither before nor after.
Show the answer
  1. Set data to the list [5]
  2. Set alias to data, so both names now see [5]
  3. Append 6 through alias, so data now reads [5, 6]
  4. Set copy to an explicit copy of data, freezing it as [5, 6]
  5. Append 7 to data, so alias now reads [5, 6, 7] while copy still reads [5, 6]
Why

Order decides everything here: the copy freezes the list as it was at that instant, so 6 must be appended before the copy and 7 after it. The alias, by contrast, has no such timing, it shares the one list forever, so the final append is visible through both data and alias but not through copy.

Read the lesson: State, Mutation, and Side Effects →

Practise State, Mutation, and Side Effects

The app has 7 more questions on this lesson, and keeps your place in the course. Computer Science I is free to start.

More questions on State, Mutation, and Side Effects