Skip to content
Cambridge AS & A Level9618

Programming Paradigms

Computer Science 9618 Chapter Notes

Last updated November 2026

ShareWhatsAppPost

Programming Paradigms notes as a printable PDF.

Read online free. Download a watermarked copy with a free account.

Read the notes

The full Programming Paradigms notes as text: skim, search, and jump between subtopics.

~16 min read

1. The four paradigms

The syllabus names four programming paradigms: low-level, imperative (procedural), object-oriented and declarative. A mark scheme describes each paradigm by how its programs are written.

Low-level programs use the instruction set of a processor. Imperative programs give an explicit sequence of commands that update the program state, with or without procedure calls. Object-oriented programs use the concepts of class, inheritance, encapsulation and polymorphism. Declarative programs specify the desired result rather than how to get to it.

Examiner insight

“Some candidates gave answers related to declarative programming languages.” “Some candidates gave answers related to imperative (procedural) programming languages.”

Worked example 14 marks

Draw one line from each programming paradigm to its most appropriate description.

Programming paradigmDescription
Programs using the instruction set of a processor
DeclarativePrograms based on events such as user actions or sensor outputs
ImperativePrograms using the concepts of class, inheritance, encapsulation and polymorphism
Low-levelPrograms with an explicit sequence of commands that update the program state, with or without procedure calls
Object-orientedPrograms that specify the desired result rather than how to get to it
  1. 1

    Declarative: programs that specify the desired result rather than how to get to it [1]

  2. 2

    Imperative: programs with an explicit sequence of commands that update the program state, with or without procedure calls [1]

  3. 3

    Low-level: programs using the instruction set of a processor [1]

  4. 4

    Object-oriented: programs using the concepts of class, inheritance, encapsulation and polymorphism [1]

2. Low-level and imperative code

Low-level code uses the instruction set of the processor, as in assembly language. It can use five addressing modes: immediate, direct, indirect, indexed and relative. This sample is low-level / assembly.

``` Start: LDD Counter INC ACC STO Counter ```

Imperative (procedural) code is built from variables, constructs, procedures and functions. It uses variables that are changed using assignment statements, and it relies on a method of repetition or iteration. The statements run in the order written. This sample is procedural / imperative.

``` FOR Counter ← 1 TO 20 X ← X * Counter NEXT Counter ```

Examiner insight

“A few candidates incorrectly described what the code was doing rather than stating the programming paradigm it represented.”

Worked example 14 marks

Identify the programming paradigm for each of these program code examples.

Program code exampleProgramming paradigm
`male(john).` `female(ethel).` `parent(john, ethel).`
`FOR Counter = 1 TO 20` `X = X * Counter` `NEXT Counter`
`Start: LDD Counter` `INC ACC` `STO Counter`
`public class Vehicle { private speed; public Vehicle() { speed = 0; } }`
  1. 1

    `male(john).` `female(ethel).` `parent(john, ethel).`: declarative [1]

  2. 2

    `FOR Counter = 1 TO 20` `X = X * Counter` `NEXT Counter`: procedural / imperative [1]

  3. 3

    `Start: LDD Counter` `INC ACC` `STO Counter`: low-level / assembly [1]

  4. 4

    `public class Vehicle ...`: object oriented / (OOP) [1]

3. Declarative programming: facts

A declarative program instructs a program on what needs to be done instead of how to do it. It uses facts and rules, and queries to satisfy goals. It can be logical, stating a program as a set of logical relations, or functional, built by applying functions to arguments.

A fact is a clause with a lower-case name and lower-case values in brackets. It ends with a full stop.

``` type(leopard, wild). hair(lion, short). ```

To record new information, write one fact for each thing the statement says. A caracal that is a wild cat with short hair gives `type(caracal, wild).` and `hair(caracal, short).`. A fact can also name a kind of thing, as in `customer(betty).`

Examiner insight

“Candidates who wrote their clauses in the correct order, with no spelling errors, no use of capitals and a full stop at the end of each line achieved full marks.”

Worked example 14 marks

A declarative programming language is used to represent some facts about people and their hobbies. The clauses include `hobby(music).` (Music is a hobby), `person(toby).` (Toby is a person), `enjoys(natasha, travelling).` (Natasha enjoys travelling) and `dislikes(toby, caving).` (Toby dislikes caving).

Carlos is a person who enjoys the hobby of cycling but does not like music. Write additional clauses to represent this information.

  1. 1

    `person(carlos).` [1]

  2. 2

    `hobby(cycling).` [1]

  3. 3

    `enjoys(carlos, cycling).` [1]

  4. 4

    `dislikes(carlos, music).` [1]

4. Declarative programming: goals and rules

A goal asks a question of the facts. A variable stands where the answer goes. The goal `choice1(X, chemistry)` returns `X = teresa, elspeth`. The variable is written with a capital letter, and the values returned are not.

One goal can join two conditions with a comma. The goal `spots(WildSpotty, yes), type(WildSpotty, wild).` finds all the wild cats with spots.

A rule builds a new relationship from facts. It names the relationship, then IF, then the conditions. Conditions are joined by AND (a comma is accepted), and NOT is used for something that has not happened.

``` may_choose_subject(N, S) IF student(N) AND subject(S) AND NOT choice1(N, S). ```

The rule needs a condition for each kind of thing, `student(N)` and `subject(S)`, as well as the condition on the choice.

Examiner insight

“Most candidates gave the correct answers and did not add any capital letters and so achieved the mark.”

Worked example 14 marks

A declarative programming language is used to represent subjects that students can choose to study. Students must choose two subjects. The clauses include `student(tomaz).`, `subject(mathematics).` and `choice1(tomaz, mathematics).` (Tomaz has chosen mathematics as his first choice).

Students must choose two different subjects such that: N may choose S, if N is a student and S is a subject and N has not chosen S as the first choice. Write this as a rule: `may_choose_subject(N, S)` IF

  1. 1

    `student(N)` as a condition [1]

  2. 2

    `subject(S)` as a condition [1]

  3. 3

    `choice1(N, S)` with the same variables [1]

  4. 4

    all logical operators correct, `AND` (or a comma) between conditions and `NOT` before `choice1(N, S)`, with no additional code: `IF student(N) AND subject(S) AND NOT choice1(N, S)` [1]

5. Object-oriented programming: classes and objects

A class is a template / blueprint from which objects are created. It is defined once, but many objects can be created from it. An object is an instance of a class: an occurrence of an object, or an instantiation of a class.

A class holds attributes / properties with their data types. It also holds methods: programmed procedures or functions that act on the properties. A constructor is used to create instances of the class.

No memory is allocated when a class is defined. Objects are allocated memory when they are created. Objects can be manipulated, but a class cannot.

Examiner insight

“Candidates should use the terms property, method, class or object, as applicable, when defining OOP terms.”

Worked example 13 marks

Objects and classes form the basic structure of Object-Oriented Programming (OOP). Outline the structure of a class.

  1. 1

    Attributes / properties ... are variables bound to the class, with their data types [1]

  2. 2

    Methods ... are subroutines / functions / procedures that act upon the attributes [1]

  3. 3

    A constructor ... is used to create instances / objects of this class (also credited: getters / setters are methods that fetch / update the contents of attributes) [1]

6. Properties, getters, setters and encapsulation

Properties are the data items of a class, each with a data type. They are declared `PRIVATE`, so they are only accessible using the class's own methods. This is encapsulation: putting properties and methods inside a class, which ensures sensitive data is hidden from users.

A getter is a method used to return the value of a property. It takes no parameter. A setter is a method used to update the value of a property. It takes the new value as a parameter.

Examiner insight

“Typical errors noted were the setting of Telephone as an integer data type, not including two setters and/or two getters or incorrect setter parameters.” “A common error was sending a parameter to the get method or outputting the value instead of returning it.”

Worked example 13 marks

A school has a program written using OOP to maintain its staff and student records. The object SubstituteTeacher allows the details of the school's substitute teachers to be stored. This includes their full name, telephone number and whether or not they are in school today. For example: SubName Sarah Jones, Telephone 01223658721, InSchool TRUE.

Complete the diagram for the object SubstituteTeacher, including appropriate properties and their getters and setters.

``` SubstituteTeacher SubName : STRING InSchool : BOOLEAN SetTelephone(Tel : STRING) GetSubName() ```

  1. 1

    properties correct: add `Telephone : STRING` [1]

  2. 2

    setters correct: `SetSubName(StaffName : STRING)` and `SetInSchool(Present : BOOLEAN)` [1]

  3. 3

    getters correct: `GetTelephone()` and `GetInSchool()` [1]

7. Writing a class in pseudocode

A class runs from `CLASS` to ENDCLASS. Attributes are declared first, each `PRIVATE` with its data type. The constructor is a PUBLIC PROCEDURE NEW. Its parameters carry the values in, and it assigns them to the attributes.

This is part of the Car class from a mark scheme, with the getters and two attributes left out.

``` CLASS Car PRIVATE Make : STRING PRIVATE BodyType : STRING PRIVATE Fuel : STRING PUBLIC PROCEDURE NEW(CarMake : STRING, CarBodyType : STRING) Make ← CarMake BodyType ← CarBodyType Fuel ← "" ENDPROCEDURE ENDCLASS ```

Use the identifiers the question gives. Values passed in through the constructor are assigned to the attributes; an attribute with no parameter, such as Fuel, is given its starting value.

Examiner insight

“Typical errors included missing ENDCLASS as the final statement, not using correct variable names in the declarations and/or the constructor.” “Fewer candidates passed the required parameters to the constructor, with some candidates assigning set values to these instead.”

Worked example 15 marks

Object-Oriented Programming (OOP) is used to design a class called Node. A partial design of the Node class includes the STRING attribute Content and the INTEGER attribute Pointer, together with their getters and setters. The class also includes a constructor, with two parameters, to set the values of these attributes.

Write pseudocode statements to declare the Node class, its attributes and the constructor.

  1. 1

    correct `CLASS Node` and `ENDCLASS` statements [1]

  2. 2

    correct attribute declaration section before the constructor [1]

  3. 3

    correct statement to begin and end the constructor as PUBLIC: `PROCEDURE NEW ... ENDPROCEDURE` [1]

  4. 4

    two parameters identified in the constructor, with appropriate names and data types [1]

  5. 5

    values passed using the constructor are assigned correctly (setters also allowed) [1]

  6. 6

    ``` CLASS Node PRIVATE Content : STRING PRIVATE Pointer : INTEGER PUBLIC PROCEDURE NEW(NewContent : STRING, NewPointer : INTEGER) Content ← NewContent Pointer ← NewPointer ENDPROCEDURE ENDCLASS ```

8. Inheritance, polymorphism and aggregation

Inheritance is the capability of defining a new class that has all the attributes and methods from a parent class. The new class is a derived class; the parent is the super class or base class. It represents an "is a" relationship.

Polymorphism allows the same method to take on different behaviours depending on which class is instantiated. Methods can be redefined for derived classes.

Aggregation (containment) is where one class or object contains another. It represents a "has a" relationship.

Examiner insight

“Candidates should not use the name of a given term to explain its meaning, for example, for inheritance, ‘a new class of objects inherits the attributes and methods from a parent class’, would be not enough and would be better, for example, as ‘a new class of objects derives the attributes and methods from a parent class’.” “Some candidates created a new class and redefined the attributes instead of using the parent class.”

Worked example 14 marks

Complete the table by filling in the missing OOP terms and descriptions.

OOP termdescription
A method used to change values in private attributes in a class.
An occurrence of a class during the execution of a program.
aggregation
inheritance
  1. 1

    setter [1]

  2. 2

    instance(s) // object(s) [1]

  3. 3

    aggregation: one class / object contains another, a "has a" relationship [1]

  4. 4

    inheritance: deriving new classes / objects from existing ones, methods and data copied from a super class to a derived class, an "is a" relationship [1]

Recap

  • Four paradigms: low-level (the processor's instruction set), imperative (explicit sequence of commands that update the program state), object-oriented (classes, inheritance, encapsulation, polymorphism) and declarative (the desired result, not how).
  • Name the paradigm; do not describe what the code does.
  • Declarative: facts and rules, with queries to satisfy goals. Clauses are lower-case and end with a full stop.
  • A rule names the relationship, then IF, then conditions joined by AND, with NOT for something that has not happened.
  • A class is a template; an object is an instance of it. Properties are declared PRIVATE.
  • A getter returns a property and takes no parameter. A setter updates a property and takes the new value.
  • A pseudocode class ends with ENDCLASS; the constructor is PUBLIC PROCEDURE NEW and assigns its parameters.
  • Inheritance is an "is a" relationship, aggregation a "has a" relationship; polymorphism redefines methods in derived classes.

End-of-chapter exercise

Test yourself on the whole chapter. Work through these before moving on.

  1. Name the programming paradigm of each code sample. (a) `Start: LDD Total`, `INC ACC`, `STO Total` (b) `WHILE Count < 10`, `Count ← Count + 1`, `ENDWHILE` (c) `parent(ann, bob).` (d) `CLASS Box` ... `ENDCLASS`4 marks
    Answer
    1. (a) Low-level (assembly).
    2. (b) Imperative (procedural).
    3. (c) Declarative.
    4. (d) Object-oriented (OOP).

    Trap: Examiners reported answers that described what the code was doing instead of naming the paradigm.

  2. A student writes: "An imperative program states the result wanted and leaves the computer to find how to get it." Explain what is wrong with this statement and describe an imperative program correctly.2 marks
    Answer
    1. That describes a declarative program, which instructs a program on what needs to be done instead of how to do it.
    2. An imperative program is a sequence of commands run in the order written, using variables changed by assignment.

    Trap: Examiners reported imperative and declarative answers swapped.

  3. A declarative language records facts about a library: `member(asha).`, `member(ben).`, `book(dune).`, `book(emma).` and `borrowed(asha, dune).` Cara joins as a member and borrows emma. Write the additional clauses.2 marks
    Answer
    1. `member(cara).`
    2. `borrowed(cara, emma).`

    Trap: Clauses use no capital letters and end with a full stop; `Member(Cara)` or a missing full stop loses the mark.

  4. The library facts include `borrowed(asha, dune).`, `borrowed(ben, dune).`, `borrowed(cara, emma).` and `borrowed(ben, emma).` (a) Write the result of the goal `borrowed(Who, emma)`. (b) Write a goal, using the variable Book, to find every book that ben has borrowed.2 marks
    Answer
    1. (a) `Who = cara, ben`
    2. (b) `borrowed(ben, Book)`

    Trap: The variable goes in the place of the value wanted: `borrowed(Book, ben)` asks a different question.

  5. A rule is needed: M may borrow B if M is a member and B is a book and M has not already borrowed B. Write this as a rule. `may_borrow(M, B)` IF4 marks
    Answer
    1. `member(M)`
    2. `book(B)`
    3. `NOT borrowed(M, B)`
    4. Together: `IF member(M) AND book(B) AND NOT borrowed(M, B)`

    Trap: Writing only `NOT borrowed(M, B)` leaves out member(M) and book(B).

  6. A pet shop program has one class Dog. It creates objects for Rex, Bella and Milo from the class. (a) State the OOP term for Rex, Bella and Milo. (b) Give two differences between the class Dog and the object Rex.3 marks
    Answer
    1. (a) Instances (of the class Dog), or objects.
    2. (b) A class is a template / blueprint; Rex is created from it.
    3. No memory is allocated for the class, but Rex is allocated memory when created.

    Trap: The class is the template; the objects are created from it.

  7. A class Student has the private property Mark : INTEGER. (a) Explain why Mark is declared private. (b) A programmer writes the getter `GetMark(NewMark : INTEGER)`, which outputs the value of Mark. State two things wrong with this getter.3 marks
    Answer
    1. (a) So that Mark is only accessible using the class's own methods.
    2. (b) A getter takes no parameter, so `NewMark` should not be there.
    3. The getter should return the value of Mark, not output it.

    Trap: Examiners reported a parameter sent to the get method and the value output instead of returned.

  8. Each librarian record stores a name, a phone number such as 01223658721, and whether the librarian is on duty. Complete the class diagram for Librarian: properties with data types, a setter for each property and a getter for each property.4 marks
    Answer
    1. ``` Librarian Name : STRING Phone : STRING OnDuty : BOOLEAN SetName(NewName : STRING) SetPhone(NewPhone : STRING) SetOnDuty(NewDuty : BOOLEAN) GetName() GetPhone() GetOnDuty() ```
    2. Marks: properties with data types (2); setters with a parameter (1); getters with no parameter (1).

    Trap: Examiners reported Telephone given an integer data type; the mark scheme gives it as STRING.

  9. A car park app stores each parking space as an object of the class ParkingSpace. A space has an identifier such as B07, a level (a whole number) and whether it is occupied. The attributes must be available only through the class's methods. The constructor is given the identifier and the level; every new space starts unoccupied. Write pseudocode to declare the class, its attributes SpaceID, Level and Occupied, and the constructor.6 marks
    Answer
    1. ``` CLASS ParkingSpace PRIVATE SpaceID : STRING PRIVATE Level : INTEGER PRIVATE Occupied : BOOLEAN PUBLIC PROCEDURE NEW(NewSpaceID : STRING, NewLevel : INTEGER) SpaceID ← NewSpaceID Level ← NewLevel Occupied ← FALSE ENDPROCEDURE ENDCLASS ```
    2. Marks: CLASS and ENDCLASS (1); PRIVATE attributes with types (1); PUBLIC PROCEDURE NEW ... ENDPROCEDURE (1); two typed parameters (1); SpaceID and Level assigned from them (1); Occupied ← FALSE (1).

    Trap: Examiners reported missing ENDCLASS and wrong identifiers; Occupied is not a parameter, so it is set to FALSE.

  10. In a game, the classes Wizard and Warrior are derived from the class Character, which has the properties Name and Health and a method Describe(). Each derived class redefines Describe() to give different text. A class Party holds several Character objects. (a) Without using the word "inherits", explain how Wizard gets Name, Health and Describe(). (b) State the OOP term for Describe() behaving differently in Wizard and Warrior. (c) State the relationship between Party and Character, and the phrase that describes it. (d) A programmer declares Name again inside Wizard. Explain what is wrong.6 marks
    Answer
    1. (a) Wizard is a derived class of the super class Character.
    2. It derives all the attributes and methods from the parent class: a Wizard "is a" Character.
    3. (b) Polymorphism.
    4. (c) Aggregation (containment): a "has a" relationship, a Party contains Characters.
    5. (d) Wizard already has Name from the parent class; declaring it again redefines the attribute instead of using the parent class.

    Trap: Examiners reported a term used to explain itself and attributes redefined instead of using the parent class.

Say this, not that

Wording that loses the mark, against the wording the mark scheme credits.

Loses the markGets the mark
A new class of objects inherits the attributes and methods from a parent class.A new class of objects derives the attributes and methods from a parent class.
A getter is a method used to get the value of a property.A getter is a method used to return the value of a property.
Declarative code explains, step by step, how to get the result.A declarative program instructs a program on what needs to be done instead of how to do it.
The code runs a loop 20 times and multiplies X.The paradigm is procedural / imperative.

Frequently asked questions

What are the four programming paradigms?
The four programming paradigms are low-level, imperative (procedural), object-oriented and declarative. Low-level programs use the instruction set of a processor. Imperative programs give an explicit sequence of commands that update the program state. Object-oriented programs use class, inheritance, encapsulation and polymorphism. Declarative programs specify the desired result rather than how to get to it.
What is the difference between a class and an object?
A class is a template or blueprint from which objects are created. An object is an instance of a class, an occurrence created from it. No memory is allocated when a class is defined, but objects are allocated memory when they are created.
What are getters and setters, and why are properties declared private?
Properties are declared PRIVATE, so they are only accessible using the class's own methods. This is encapsulation. A getter returns the value of a property and takes no parameter. A setter updates the value of a property and takes the new value as a parameter.
What are inheritance, polymorphism and aggregation in object-oriented programming?
Inheritance defines a new derived class that has all the attributes and methods from a parent class, an is a relationship. Polymorphism lets the same method take on different behaviours, because methods can be redefined for derived classes. Aggregation is where one class or object contains another, a has a relationship.
How do you write facts and rules in a declarative programming language?
A fact is a clause with a lower-case name and lower-case values in brackets, and it ends with a full stop, such as type(leopard, wild). A goal asks a question of the facts, and a variable with a capital letter stands where the answer goes. A rule names the relationship, then IF, then conditions joined by AND, with NOT for something that has not happened.
Why do I lose marks when I define inheritance using the word inherits?
Candidates should not use the name of a given term to explain its meaning. Saying a new class of objects inherits the attributes and methods from a parent class is not enough. Write that a new class of objects derives the attributes and methods from a parent class.

Free notes are just the start.

Unlock every Workbook and Chapter at a Glance, and generate your own worksheets and predicted papers.

Explore plans

Related chapters