Skip to content

CP2 practice paper

This paper practises CP2, the week 11 checkpoint. It has the real paper's format, length, question types and marks. The course wrote the code and the observation record for this practice paper. They come from no real project, and no real paper uses them. The practice papers page explains how to sit and mark the paper. The marking points and worked answers are on a separate page. Open it only when you have finished.

Conditions. Work alone for 30 minutes with no device, model, agent or reference material. The only help allowed is the glossary below the code. Keep your reasons short. Cite line numbers and record lines, so that a marker can find what you mean.

Marks. Ten points: six for Q1, two for Q2 and two for Q3. On the real paper, the recorded mark is the raw mark × 0.75, so 7.5 at most.

Suggested pace. The first half ends after Q1. If you sit the paper in two halves, stop there at minute 15 and take Q2 and Q3 in one more 15-minute sitting.

Minutes What to do
0–4 Read the contract, the observation record and the listing
4–15 Q1 (end of the first half)
15–21 Q2
21–27 Q3
27–30 Check your answers

Contract and observation record

A small team keeps its service settings in a shared store. update_settings applies an engineer's list of (name, value) changes to the store, in the order given. Every change must name a setting that already exists; if any change does not, nothing is changed and the result is blocked. If a change cannot be applied, every setting must be set back to its value from before the call, and only then may the result be reverted. The result is changed only when every change has been applied. The value of the setting api_token is a secret and must never be written to the journal, which every member of the team can read. store.set(name, value) sets a setting, creating it if it does not exist, and raises OSError if the setting is locked; a locked setting keeps its value.

Before the run, the store held region=north-1, retries=3, timeout=30 and an api_token. The setting region was locked for maintenance. The on-call engineer ran update_settings(store, [("retries", 5), ("timeout", 90), ("region", "north-2")], journal) and got back reverted. The engineer then read timeout from the store, and it was 30. Nobody read anything else after the run. This is the journal after the run:

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

Code under review

01 HIDDEN = "api_token"
02
03 def masked(name, value):
04     return "<hidden>" if name == HIDDEN else value
05
06 def describe(store):
07     return ", ".join(f"{n}={store.get(n)}" for n in sorted(store.names()))
08
09 def all_known(store, changes):
10     for name, value in changes:
11         if name not in store.names():
12             return False
13         return True
14     return True
15
16 def update_settings(store, changes, journal):
17     if not all_known(store, changes):
18         return "blocked"
19     undo = None
20     for name, value in changes:
21         old = store.get(name)
22         try:
23             store.set(name, value)
24         except OSError as err:
25             journal.append(f"failed on {name}: {err}; now {describe(store)}")
26             if undo:
27                 store.set(*undo)
28             return "reverted"
29         undo = (name, old)
30         journal.append(f"set {name} to {masked(name, value)}")
31     return "changed"

Glossary

  • Store: the shared place that holds the settings. store.names() lists the names of the settings that exist, store.get(name) returns a setting's current value, and store.set(name, value) is described in the contract.
  • Journal: the list of lines that update_settings appends to. Every member of the team can read it.
  • Secret: a value that gives access to something, such as a token, and must stay out of anything others can read.
  • Revert: set settings back to the values they had before a change began.
  • f"...": text in which each {...} is replaced by the value inside it. f"{n}={store.get(n)}" gives retries=3 when n is "retries" and its value is 3.
  • ", ".join(...): joins texts with a comma and a space between them. sorted(...) puts names in alphabetical order.
  • store.set(*undo): calls store.set with the two values saved in undo, a name and a value.
  • except OSError as err: runs when store.set raises OSError. err is the error. {err} in a journal line gives its message, such as locked for maintenance.

Questions

Q1: review findings (6 marks)

Find three different defects. For each one, give its location (the function and the line numbers), an input or condition that triggers it, and what anyone could then observe. Two findings with the same symptom count as one.







End of the first half.

Q2: recovery argument (2 marks)

Say what in the observation record you can rely on and which claim you reject. Then write the bounded check you would run next, with the condition under which you would stop or set the settings back, before calling the settings recovered.



Q3: verification plan (2 marks)

Give two regression checks, each with its input, the result you expect, and the contract sentence or invariant that the expectation comes from.



Stop when the time is up. Then mark your paper with the marking points and worked answers.