Further programming
Cambridge AS & A Level Computer Science 9618 · Paper 4 · Unit 20
Paper 4 · Unit 20. Programming paradigms, OOP, Prolog, random files and exception handling.
1. Programming paradigms
| Paradigm | Description |
|---|---|
| Low-level | Assembly or machine code, close to the hardware; uses addressing modes (immediate, direct, indirect, indexed, relative) |
| Imperative (procedural) | A sequence of statements that change the program state, organised into procedures and functions |
| Object-oriented | Programs are built from objects that combine data (attributes) and behaviour (methods) |
| Declarative | States facts and rules; the system works out the answer to a query (e.g. Prolog) |
2. Object-oriented programming
| Term | Meaning |
|---|---|
| Class | A blueprint defining attributes and methods |
| Object | An instance of a class |
| Constructor | A method that runs when an object is created, setting initial values |
| Encapsulation | Keeping attributes private and accessing them through methods (getters and setters) |
| Inheritance | A subclass takes on the attributes and methods of a superclass and can add its own |
| Polymorphism | A method with the same name behaves differently in different classes (overriding) |
| Containment / aggregation | An object contains other objects as attributes |
CLASS Pet
PRIVATE Name : STRING
PUBLIC PROCEDURE NEW(GivenName : STRING)
Name ← GivenName
ENDPROCEDURE
PUBLIC FUNCTION GetName() RETURNS STRING
RETURN Name
ENDFUNCTION
PUBLIC FUNCTION Speak() RETURNS STRING
RETURN "..."
ENDFUNCTION
ENDCLASS
CLASS Dog INHERITS Pet
PUBLIC FUNCTION Speak() RETURNS STRING // overrides
RETURN "Woof"
ENDFUNCTION
ENDCLASS
MyDog ← NEW Dog("Rex")Try it: name the OOP concept
3. Declarative programming
In Prolog, facts state what is true, rules define relationships, and queries ask the system to find solutions by matching (backtracking through the facts).
parent(ali, sara). parent(sara, omar). grandparent(X, Z) :- parent(X, Y), parent(Y, Z). ?- grandparent(ali, Who). Who = omar
4. File processing and exceptions
Random (direct-access) files store records at calculated positions:
OPENFILE "students.dat" FOR RANDOM SEEK "students.dat", Address GETRECORD "students.dat", Student PUTRECORD "students.dat", Student CLOSEFILE "students.dat"
An exception is an unexpected event during execution (e.g. file not found, division by zero, invalid input). Exception handling catches it so the program can recover or end cleanly instead of crashing:
TRY OPENFILE "data.txt" FOR READ EXCEPT OUTPUT "File not found" ENDTRY
5. Examiner tips
- Encapsulation: mention private attributes and public methods.
- Inheritance vs polymorphism: reusing vs overriding.
- Prolog: facts end with a full stop; variables start with a capital letter.
- Random files use SEEK, GETRECORD and PUTRECORD.
- Exception handling prevents crashes and gives useful messages.
6. Practice questions
Practice: answer and check instantly
Type a short answer, then press Check or Enter.
Show answer
An object.
Show answer
The constructor.
Show answer
Encapsulation.
Show answer
Inheritance.
Show answer
Polymorphism.
Show answer
Declarative.
Show answer
SEEK.
Show answer
TRY.
Exam-style questions
9. Explain encapsulation and why it is used. [3]
Show mark guide
Attributes are declared private within the class [1] · they are accessed/changed only through public methods (getters/setters) [1] · this protects data from invalid changes and hides implementation details [1].
10. Using the Prolog facts parent(ali, sara) and parent(sara, omar), write a rule for grandparent and state the result of grandparent(ali, X). [3]
Show mark guide
grandparent(X, Z) :- parent(X, Y), parent(Y, Z). [2] · X = omar [1].
7. Topic test: predict your grade
Topic test: predict your grade
15 questions, about 10 minutes. Answer them all, then press Mark my test.
Frequently asked questions
What are the main OOP concepts for A Level?
The key concepts are classes, objects, encapsulation, inheritance, polymorphism and containment.
What is the difference between inheritance and polymorphism?
Inheritance lets a subclass reuse the attributes and methods of a superclass. Polymorphism lets a subclass override a method so the same method name behaves differently.
What is exception handling?
Exception handling catches unexpected run-time events, such as a missing file, using TRY and EXCEPT, so the program can recover instead of crashing.