The eight-page Preliminary Examination in Computer Science, Paper 1, Long Vacation 2003. Original source: Released examination archive, member 4213-1.pdf. Printed pp. 2–3 cover functional programming, p. 4 covers algorithms, pp. 5–6 cover imperative programming, and pp. 7–8 cover digital electronics. The timing diagram on printed p. 8 permits a new clock cycle pulse immediately after a cycle that rearms the circuit.
Imperative programming Created 2026-10-13 Updated 2026-11-05
Imperative programming describes computation through commands changing program state. Assignment updates a variable, sequencing orders commands and loops repeat updates until a condition changes. To sum a list, an imperative program can maintain an accumulator and add each next element into it. Correctness can be expressed through a loop invariant: after processing a prefix, the accumulator equals that prefix's sum. State changes can be local or observable as a side effect. The paradigm concerns the description of computation, rather than requiring a particular processor architecture.
The 12 June 2013 preliminary examination paper, released as member 4216-1.pdf of Fakour Zip Final 1.zip. Questions 1–4 concern imperative programming and data structures; Questions 5–8 concern Scala object-oriented programming.
An ML reference type 'a ref describes a mutable cell holding a value of type 'a. The operations are ref : 'a -> 'a ref for allocation, ! : 'a ref -> 'a for dereferencing and := : 'a ref * 'a -> unit for assignment. An immutable variable binding can name a mutable cell; assigning to the cell does not change what the binding denotes. Two names may alias the same cell, so an update through either is visible through the other.
val counter = ref 0;
val alias = counter;
counter := !counter + 1;
Both dereferences now give one. A reference's stored type remains fixed; the value restriction prevents allocating one unrestricted polymorphic cell and then using incompatible instantiations.
Imperative programming uses sequencing (e1; e2; ...; en) to perform effects left to right, with the final expression supplying the result. Conditional expressions choose one branch. while condition do body repeatedly checks a Boolean condition and performs a unit-valued body, returning () on termination. Standard ML has no required primitive for loop, but recursion or a reference with while supplies one. For example:
fun sumTo n =
    let val i = ref 1
        val total = ref 0
    in
        while !i <= n do
          (total := !total + !i; i := !i + 1);
        !total
    end;
References provide mutable state; sequencing, conditionals and loops control its updates.