Skip to content

CP2 practice paper: marking points and worked answers

Open this page only when you have finished the CP2 practice paper.

How to mark your paper

The paper has ten points of one mark each. A point is either earned or not. For each point below you will find when it is earned, a worked answer, and the usual ways to miss it. The worked answer is one way to earn the point. A shorter answer that gives the same evidence earns it too. Spelling, diagram style and choice of words earn and cost nothing.

  • Q1, six points. Mark your best three distinct findings. Each finding earns one point for a location that names the function and the line or lines, and one point for a concrete trigger together with an impact someone else could observe. A correct mechanism with no trigger is not a complete finding. The same symptom given twice is marked once.
  • Q2, two points. One for separating what the record proves from the claim you reject, one for a bounded next check with a stopping or rollback condition.
  • Q3, two points. One for each check that has an input, an expected result and the contract sentence or invariant behind it, and that fails on the printed code. A check whose expected result is copied from what the code does now earns nothing.

Add up your raw total out of 10. On the real paper, the recorded mark is raw × 0.75, so 7.5 at most.

Q1: review findings (LO5, LO7, LO8)

The listing has three planted defects. Any order is fine.

Finding: the secret is written to the journal (LO7)

Location point. describe, line 7, used in the failure line on line 25. It writes every setting with its value, api_token included, into the journal. masked hides the token only in the success line, line 30.

Trigger and impact point. Any run in which a store.set fails while the store holds api_token; the engineer's run is one. The token's value then sits in a journal that every member of the team can read, and the record shows it: api_token=sk-EXAMPLE in the third journal line. From that moment the token has to be treated as exposed.

Finding: the revert sets back only the last applied change (LO8)

Location point. update_settings, lines 19 and 26 to 29. undo holds a single change and is overwritten after every successful store.set on line 29, so line 27 sets back only the most recent one.

Trigger and impact point. A list in which at least two changes are applied before one fails, such as the engineer's run: retries and timeout were applied, then region failed. The result is reverted, but only timeout is set back to 30; retries stays 5. A failure on the second change does not expose the defect: the one applied change is then the one set back, and the revert is complete.

Finding: only the first name is checked (LO5)

Location point. all_known, line 13. return True is inside the loop, so the function returns after looking at the first change.

Trigger and impact point. An unknown name in the second or a later place, such as update_settings(store, [("timeout", 90), ("timout", 30)], journal). The result is changed: timeout becomes 90 and a new setting timout is created, where the contract requires blocked and no change at all. An unknown name in the first place does not expose the defect: it is blocked, as it should be.

Other findings you may have written

  • masked and line 30. They are correct. When the change is to api_token, the success line reads set api_token to <hidden>. No point.
  • Line 21 reads the old value before the change. This is correct. It is the value a revert needs. No point.
  • The store.set on line 27 could itself raise. The contract says nothing about a failed revert, so this is not a defect under the contract. No point in Q1. But it is a good reason in Q2 to read the settings back.
  • "reverted is wrong" and "retries stays 5" as two findings. They are one symptom of the same defect, so they are marked once.

Usual ways to miss a point. A location without a line number; a trigger such as "when something fails" with no input; an impact such as "the state is bad" that nobody could observe.

Q2: recovery argument (LO8)

First point: what the record proves, and the claim to reject.

The record can be trusted for what was observed. The first two journal lines show that retries and timeout were applied. The third shows that region failed because it was locked, with retries=5 and timeout=90 at that moment. After the run, timeout was read as 30. Reject reverted as evidence that every setting is back. Nobody read retries after the run, and line 27 sets back only the last applied change, so retries is most likely still 5. The result is a claim made by the code under review, not an observation.

Second point: a bounded next check, with a stopping or rollback condition.

Read retries, timeout and region now and compare each with its value from before the run: 3, 30 and north-1. If one differs, set it back by hand and read it again. If that fails or the value still differs, stop and hand the incident on instead of declaring recovery. Declare the settings recovered only when all three read their earlier values. Separately, the token was written to a journal the whole team can read. Replace the token and retire the old one, because nobody can unread a line that was written.

Usual ways to miss a point.

  • Trusting reverted because the function returned it.
  • "Check the settings", with no names, no values to compare against and no condition for stopping.
  • "Run update_settings again". It would repeat the faulty revert.

Q3: verification plan (LO5)

Worked answer, check 1. The input is the engineer's case: a store with region=north-1 (locked), retries=3, timeout=30 and an api_token, and the call update_settings(store, [("retries", 5), ("timeout", 90), ("region", "north-2")], journal). The expected result is reverted. After the call, retries reads 3, timeout 30 and region north-1. This follows from the contract sentence that every setting must be set back to its value from before the call before the result may be reverted. The printed code leaves retries at 5. So the check fails now, and it passes after a repair that sets back every applied change.

Worked answer, check 2. The input is the same store with nothing locked, and the call update_settings(store, [("timeout", 90), ("timout", 30)], journal). The expected result is blocked, with timeout still 30 and no setting named timout. This follows from the contract: every change must name a setting that already exists. Otherwise nothing is changed and the result is blocked. The printed code returns changed, sets timeout to 90 and creates timout.

Also earns a point. A check of the security boundary in both directions. Run the engineer's case again and read the journal. The denied side: no line contains the token's value, sk-EXAMPLE. The permitted side: one line still names region as the change that failed. The justification is the sentence that the value of api_token must never be written to the journal.

Usual ways to miss a point.

  • "It returns reverted" as the expected result. The printed code already does that.
  • An expected retries of 5, which is copied from what the code does now.
  • An input whose first name is the unknown one. The printed code already blocks it.
  • No contract sentence or invariant named.

Check your answers by running the listing

The paper is sat without running anything. Afterwards, running the listing shows whether your reasoning held. The block below is the listing without its line numbers, so line 27 of the file is line 27 of the paper. It is followed by a small store that behaves as the paper describes and by the engineer's run. Save it as cp2_practice.py in a scratch folder of your own, outside the course checkout. The token in it, sk-EXAMPLE, is the course's placeholder, not a real key.

HIDDEN = "api_token"

def masked(name, value):
    return "<hidden>" if name == HIDDEN else value

def describe(store):
    return ", ".join(f"{n}={store.get(n)}" for n in sorted(store.names()))

def all_known(store, changes):
    for name, value in changes:
        if name not in store.names():
            return False
        return True
    return True

def update_settings(store, changes, journal):
    if not all_known(store, changes):
        return "blocked"
    undo = None
    for name, value in changes:
        old = store.get(name)
        try:
            store.set(name, value)
        except OSError as err:
            journal.append(f"failed on {name}: {err}; now {describe(store)}")
            if undo:
                store.set(*undo)
            return "reverted"
        undo = (name, old)
        journal.append(f"set {name} to {masked(name, value)}")
    return "changed"


# Not on the paper: a small in-memory store that behaves as the paper describes.
class MemoryStore:
    def __init__(self, settings, locked=()):
        self.settings = dict(settings)
        self.locked = set(locked)

    def names(self):
        return list(self.settings)

    def get(self, name):
        return self.settings.get(name)

    def set(self, name, value):
        if name in self.locked:
            raise OSError("locked for maintenance")
        self.settings[name] = value


def before():
    return {"api_token": "sk-EXAMPLE", "region": "north-1", "retries": 3, "timeout": 30}


# The engineer's run.
store = MemoryStore(before(), locked={"region"})
journal = []
print(update_settings(store, [("retries", 5), ("timeout", 90), ("region", "north-2")], journal))
for line in journal:
    print(line)
print("after:", {name: store.get(name) for name in ("region", "retries", "timeout")})

Run it in that folder with the course's Python environment active:

python cp2_practice.py
reverted
set retries to 5
set timeout to 90
failed on region: locked for maintenance; now api_token=sk-EXAMPLE, region=north-1, retries=5, timeout=90
after: {'region': 'north-1', 'retries': 5, 'timeout': 30}

The three journal lines are the record printed on the paper. The last line shows the setting nobody read. To try one of your Q3 checks, add it at the end of the file as an assertion. For check 1:

store = MemoryStore(before(), locked={"region"})
update_settings(store, [("retries", 5), ("timeout", 90), ("region", "north-2")], [])
assert store.get("retries") == 3

On the printed listing the run ends with AssertionError. Then try one repair for each finding, and run again after each:

  • Line 13: delete it, so that the loop looks at every name before all_known returns True.
  • Line 7: write masked(n, store.get(n)) in place of store.get(n), so that the failure line hides the token as the success line does.
  • The revert: start with undo = [] on line 19. Record every applied change with undo.append((name, old)) on line 29. On a failure, set each one back, newest first, in place of lines 26 and 27.

With the revert repaired, check 1 passes. With line 13 deleted, the case in check 2 is blocked. With line 7 repaired, the failure line no longer contains sk-EXAMPLE and still names region. The course ran the listing and each of these repairs with Python 3.12 on 15 October 2026. The output above is what the printed listing printed.