Mission 1 · Spec 4.1.1
Programming concepts
Data types including records, arrays and pointers; iteration and selection; string handling; random numbers; and exception handling.
- Starter 5 min
- Learn 15 min
- Lab 20 min
- Quiz 10 min
- Exam 15 min
Divide by zero
A program asks for the number of people sharing a bill and divides the total by it. What happens if the user types 0, or "three"? How should a well-written program respond?
Reveal
Without protection it crashes with a run-time error. Exception handling (try … except/catch) lets the program detect the error and recover, for example by asking again.
Key ideas
Data types
Integer, real/float, Boolean, character, string, date/time, pointer/reference, records, arrays. User-defined types are built from existing types, e.g. a record type.
Iteration and selection
Definite iteration (count-controlled) and indefinite iteration (condition at the start or end). Nested structures. Meaningful identifiers make code readable.
String handling
Length, position, substring, concatenation, character ↔ code, and conversions string ↔ integer, float and date/time.
Exception handling
Code that might fail goes in a try block. If an exception is thrown, control jumps to the catch/except block, which handles it instead of the program crashing.
TRY people ← STRING_TO_INT(USERINPUT) share ← total / peopleCATCH OUTPUT 'Please enter a whole number above 0'ENDTRY
Loop lab
String workbench
Choose the data type
Exam-style questions
1. Explain what is meant by exception handling and why it is used.
[3 marks]Mark scheme
- Code that may cause a run-time error is placed in a protected (try) block (1)
- If an error / exception occurs, control passes to a handler (catch / except) block (1)
- so the program can recover or fail gracefully instead of crashing (1)
2. Explain the difference between definite and indefinite iteration, and give an example situation for each.
[4 marks]Mark scheme
- Definite: the number of repetitions is known before the loop starts (1), e.g. processing every element of a 10-item array (1)
- Indefinite: repeats until a condition is met, number not known in advance (1), e.g. asking for input until it is valid (1)
TUTOR NOTES
- Practical: have the student add exception handling to their own NEA-style input routines.
- Language link: Python
try/except, C#try/catch, VB.NETTry/Catch.
Mission 2 · Spec 4.1.1
Subroutines, stack frames and recursion
Parameters and return values, local and global variables, what goes in a stack frame, and recursive techniques.
- Starter 5 min
- Learn 15 min
- Lab 20 min
- Quiz 10 min
- Exam 15 min
Where was I?
Subroutine A calls B, which calls C. When C finishes, how does the computer know to go back to the middle of B, with B's variables intact?
Reveal
Each call pushes a stack frame onto the call stack, holding the return address, the parameters and the local variables. When C returns, its frame is popped and B carries on.
Key ideas
Subroutines
Named blocks of code. Functions return a value; procedures don't. Parameters pass data in; interfaces should be clear so subroutines are self-contained.
Local variables
Exist only while the subroutine runs, and can only be accessed inside it. Good practice: they keep subroutines independent.
Stack frames
Hold return addresses, parameters and local variables for each active subroutine call.
Recursion
A subroutine that calls itself, with a base case that stops it. Elegant for recursive problems but uses more memory and risks stack overflow.
Inside a subroutine call
Recursion and the call stack
Exam-style questions
1. State three items stored in a stack frame.
[3 marks]Mark scheme
- Return address (1)
- Parameters (1)
- Local variables (1)
2. Explain why a recursive subroutine must have a base case.
[2 marks]Mark scheme
- Without a base case the subroutine would keep calling itself forever (1)
- so the call stack would run out of memory / stack overflow (1)
TUTOR NOTES
- Lab prompt: what is the maximum stack depth for factorial(6)? For fib(6)?
Mission 3 · Spec 4.1.2
Object-oriented programming
Classes and objects, instantiation, encapsulation, inheritance, aggregation and composition, polymorphism and overriding, abstract, virtual and static methods, class diagrams and design principles.
- Starter 5 min
- Learn 20 min
- Lab 15 min
- Quiz 10 min
- Exam 15 min
A car and its engine
A car has an engine; a garage has cars. If the car is scrapped, does the engine still exist? If the garage closes, do the cars?
Reveal
The engine is destroyed with the car: that's composition (strong ownership). The cars still exist without the garage: that's aggregation (a weaker "has a" relationship).
Key ideas
Classes and objects
A class defines attributes and methods; an object is an instance created by instantiation using a constructor.
Encapsulation
Bundling data with the methods that act on it, with attributes private and accessed via public methods. Access modifiers: public (+), private (−), protected (#).
Inheritance
"Is a" relationship: a subclass inherits attributes and methods from its superclass and can add or override them.
Association
Aggregation: "has a", the parts can exist independently (hollow diamond). Composition: parts are destroyed with the whole (filled diamond).
Polymorphism and overriding
Objects of different classes respond to the same method call in their own way. A subclass overrides a method by redefining it.
Abstract, virtual and static
Abstract method: declared but not implemented; subclasses must implement it. Virtual: has an implementation that may be overridden. Static: belongs to the class, not an object.
Design principles: encapsulate what varies; favour composition over inheritance; program to interfaces, not implementation.
OOP concepts
Which relationship?
Exam-style questions
1. Explain the difference between aggregation and composition, with an example of each.
[4 marks]Mark scheme
- Aggregation: the contained object can exist independently of the container (1), e.g. players in a team (1)
- Composition: the contained object is destroyed when the container is (1), e.g. rooms in a building (1)
2. Explain why "favour composition over inheritance" is considered a good design principle.
[3 marks]Mark scheme
- Inheritance creates tight coupling between subclass and superclass (1)
- Changes to a superclass can have unintended effects on subclasses / deep hierarchies become rigid (1)
- Composition allows behaviour to be combined and changed flexibly, even at run time (1)
TUTOR NOTES
- Class diagrams: practise drawing inheritance (hollow arrow), aggregation (hollow diamond) and composition (filled diamond).
- Exam context: the Paper 1 skeleton program is object-oriented; use it for examples.