Paper 4 · Unit 20. Programming paradigms, OOP, Prolog, random files and exception handling.

1. Programming paradigms

ParadigmDescription
Low-levelAssembly 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-orientedPrograms are built from objects that combine data (attributes) and behaviour (methods)
DeclarativeStates facts and rules; the system works out the answer to a query (e.g. Prolog)

2. Object-oriented programming

TermMeaning
ClassA blueprint defining attributes and methods
ObjectAn instance of a class
ConstructorA method that runs when an object is created, setting initial values
EncapsulationKeeping attributes private and accessing them through methods (getters and setters)
InheritanceA subclass takes on the attributes and methods of a superclass and can add its own
PolymorphismA method with the same name behaves differently in different classes (overriding)
Containment / aggregationAn 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

  1. Encapsulation: mention private attributes and public methods.
  2. Inheritance vs polymorphism: reusing vs overriding.
  3. Prolog: facts end with a full stop; variables start with a capital letter.
  4. Random files use SEEK, GETRECORD and PUTRECORD.
  5. 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.

1. Assembly language is…
2. Procedural programming uses…
3. A class is…
4. A getter method…
5. A private attribute can be accessed…
6. Inheritance allows…
7. Overriding a method is an example of…
8. Containment means…
9. Prolog facts…
10. In Prolog, variables start with…
11. Prolog finds answers by…
12. GETRECORD…
13. An exception is…
14. Exception handling…
15. Declarative programming focuses on…

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.