A-level Computer Science Year 12 · Topic 1
Programming and data structures
Programming and data structures at A-level builds the skills to write well-structured, reliable programs.
We will review programming fundamentals and data structures, subroutines, parameter passing and scope.
Then we use recursion, strings, files and exceptions, and the object-oriented ideas of classes, inheritance, encapsulation and polymorphism.
- 6 subtopics
- 173 questions
- Free: 1.1 notes and 10 questions
In this topic
- 1.1Programming fundamentalsFree below · 28 questions
- 1.2Data structuresOpen in Full Marks · 24 questions
- 1.3Subroutines, structured programming, parameter passing and scopeOpen in Full Marks · 31 questions
- 1.4RecursionOpen in Full Marks · 24 questions
- 1.5String handling, file handling, random numbers and exception handlingOpen in Full Marks · 32 questions
- 1.6Object-oriented programming: classes, objects, inheritance, encapsulation and polymorphismOpen in Full Marks · 34 questions
1.1 · Free notes
Programming fundamentals
Key points
- Programs are built from sequence, selection and iteration, with subroutines (procedures and functions) providing structure and reuse.
- Parameters can be passed by value (a copy) or by reference (the original is changed). Variables have local or global scope.
- Recursion is a subroutine calling itself; it needs a base case to stop, and uses the call stack, which can overflow if recursion is too deep.
- Object-oriented programming uses classes and objects, with encapsulation (bundling data and methods, hiding internal state), inheritance and polymorphism.
- Good practice includes meaningful identifiers, comments, modular design and defensive input validation.
In more detail
- Worked integer division: 17 DIV 5 = 3 and 17 MOD 5 = 2; −7 DIV 2 is −4 in Python (floor division), so check your language's rule.
- Worked nested selection: IF age ≥ 18 THEN (IF hasID THEN entry ← True) ELSE entry ← False — tidier as IF age ≥ 18 AND hasID.
- Worked loop choice: FOR when the number of repeats is known (10 questions); WHILE when it depends on a condition (until the user types "quit").
- Worked Boolean logic: NOT (x > 5 AND y < 3) is the same as x ≤ 5 OR y ≥ 3 (De Morgan).
- Worked type error: "Score: " + 10 fails in Python; use "Score: " + str(10).
Practice
Programming fundamentals: 10 questions
Pick an answer to check it, or open the answer for the explanation.
1. What is required for recursion to terminate?
- A global variable
- A base case
- An infinite loop
- A compiler
Show answer
B: A base case. Without a base case, the stack eventually overflows.
2. What is encapsulation in OOP?
- Using many classes
- Bundling data and methods together and hiding internal state
- Copying objects
- Inheriting from two classes
Show answer
B: Bundling data and methods together and hiding internal state. Attributes are accessed through methods rather than directly.
3. What happens when a parameter is passed by reference?
- A copy is made
- The original variable can be changed by the subroutine
- The program crashes
- It becomes a constant
Show answer
B: The original variable can be changed by the subroutine. Passing by value copies the data instead.
4. What is the scope of a local variable?
- The whole program
- Only the subroutine where it is declared
- All subroutines
- Only global code
Show answer
B: Only the subroutine where it is declared. This helps avoid unintended side effects.
5. What does polymorphism allow?
- Only one class in a program
- Objects of different classes to be treated through a common interface
- Faster compilation
- Direct access to private data
Show answer
B: Objects of different classes to be treated through a common interface. Methods can behave differently depending on the object.
6. What is a global variable?
- A variable used in one subroutine only
- A variable accessible throughout the entire program
- A constant
- A parameter
Show answer
B: A variable accessible throughout the entire program. Overuse can cause unintended side effects.
7. What is inheritance in OOP?
- Copying code manually
- A class acquiring the attributes and methods of a parent class
- Hiding data
- Passing parameters
Show answer
B: A class acquiring the attributes and methods of a parent class. It supports reuse and hierarchy.
8. Why is input validation important?
- It speeds up code
- It prevents invalid or malicious data causing errors or security issues
- It reduces file size
- It removes comments
Show answer
B: It prevents invalid or malicious data causing errors or security issues. Defensive design anticipates misuse.
9. A recursive function fact(n) returns n * fact(n-1) and has no base case. What will most likely happen when fact(3) is called?
- It loops without using any memory
- It returns 0
- A stack overflow error occurs
- It returns 6
Show answer
C: A stack overflow error occurs. With no base case the calls never stop. Each call adds a stack frame, so the call stack eventually runs out of space. It does not return 6, because that needs a base case.
10. function f(n): if n == 0 return 0, else return n + f(n-1). What value does f(4) return?
- 24
- 4
- 10
- 6
Show answer
C: 10. f(4) = 4 + 3 + 2 + 1 + f(0) = 10. It is a running sum, not a product, so 24 is wrong.
Keep going in Full Marks
Full Marks has 28 questions on Programming fundamentals (18 more than here), set out in practice levels. You also get notes and 3D models for all 6 subtopics of Programming and data structures, plus games, timed exams and past papers.