Cambridge IAL Computer Science study materials
Cambridge IAL Computer Science study resources

Cambridge IAL Computer Science: Programming paradigms

Cambridge IAL Computer Science · Work through your exam, one topic at a time.

All guide sections

Programming paradigms

Programming paradigms

A paradigm is a style of programming with its own model of computation. The exam asks you to recognise which one a given program uses and to justify choosing it. You need structured/imperative, object-oriented, and declarative (functional and logic) with their defining features, advantages and drawbacks.

Imperative and structured

Programs are a sequence of commands that change stored state. Structured programming restricts this to sequence, selection and iteration with single entry/exit blocks, no arbitrary jumps, and top-down design - making code easier to trace, test and maintain.

Object-oriented

  • Class vs object: a class is the template (attributes plus methods); an object is an instance with its own attribute values.
  • Encapsulation: attributes are private and reached only through public methods, so the class controls its own invariants.
  • Inheritance: a subclass reuses and extends a superclass; the is-a test must hold.
  • Polymorphism: the same method call behaves according to the object's actual class, resolved at run time.

Declarative

You state what is true, not how to compute it step by step. Functional code composes pure functions - same input, same output, no side effects - using recursion and higher-order functions instead of loops and mutable variables. Logic code is a set of facts and rules; the engine unifies queries and backtracks to find solutions.

Worked example

A library lends Book (loan 7 days) and DVD (loan 3 days). Both inherit loanPeriod() from LibraryItem and each overrides it.

items = [Book('Dune'), DVD('Alien')]
FOR EACH item IN items
    OUTPUT item.loanPeriod()
NEXT

Each element has static type LibraryItem but dynamic type Book or DVD. The call compiles because loanPeriod() exists on LibraryItem; at run time the object's real class selects the overriding method, so output is 7 then 3. Adding a Magazine override needs no change to the loop - that extensibility is what polymorphism buys.

Traps that cost marks

  • Confusing overriding (same signature, subclass) with overloading (same name, different parameters).
  • Saying encapsulation means hiding the class rather than restricting access to attributes.
  • Claiming functional programs cannot hold state at all, or that declarative code contains no algorithm.
  • Describing inheritance where composition (has-a) is correct.
  • Mixing up class and object in definitions.