EuraStudy
Every program is built from a small set of ideas: values of a given data type held in variables, combined by the three structured constructs of sequence, selection and iteration, and packaged into reusable subroutines. This chapter develops those foundations rigorously, adds the call stack and recursion, and then raises the level of abstraction to the object-oriented paradigm of classes, inheritance and polymorphism. These skills are exercised directly in the on-screen Paper 1 and in the practical project.
5 sections~18 min reading time3 competenciesLevel Foundation 2 · Standard 2 · Advanced 1
basic level
AS-Level expects confident use of data types, the three constructs, subroutines with parameters, and simple string handling and exception handling.
higher level
The full A-Level adds recursion and the call stack, and the whole object-oriented paradigm - classes, encapsulation, inheritance, polymorphism and composition - which are examined in Paper 1 and demanded by the NEA.
Reading depth: In depth
Text size: Standard
Division identity
For integers with , the quotient and remainder always reconstruct the original: e.g. .
Using only integer division and modulo, describe how to obtain the units, tens and hundreds digits of the integer 472, and give each value.
The units digit is the remainder on division by 10: .
Remove the units digit with , then take that modulo 10: .
Divide twice by 10: , and .
Result: Units 2, tens 7, hundreds 4. Repeated MOD 10 and DIV 10 peel digits off the right - the same idea underlies converting a number to any base.
Typical mistakes
Active revision
For each of these values give the most appropriate data type and one reason: a person's age, whether a light is on, a product price, a UK postcode. Then evaluate , and interpreted as a Boolean condition.
Active recall
Recall the key points — then reveal.
Sources: GCE AS and A level subject content for computer science (Department for Education) · AQA A-level Computer Science 7517 specification (AQA)
The control flow of a condition-controlled (WHILE) loop
Trace the value printed by: total = 0; FOR i = 1 TO 3; FOR j = 1 TO 2; total = total + i*j; NEXT j; NEXT i; PRINT total.
j = 1 adds (total 1); j = 2 adds (total 3).
j = 1 adds (total 5); j = 2 adds (total 9).
j = 1 adds (total 12); j = 2 adds (total 18).
Result: The inner loop runs times in total and the program prints 18.
Typical mistakes
Active revision
Write pseudocode using a WHILE loop that reads numbers until the user enters 0 and prints their total, then state exactly how it behaves if the very first number entered is 0.
Active recall
Recall the key points — then reveal.
Sources: AQA A-level Computer Science 7517 specification (AQA)
The call stack during nested subroutine calls
A procedure increment(x) sets x = x + 1. A caller sets n = 5 and calls increment(n). State the value of n after the call when x is passed (a) by value and (b) by reference.
increment receives a copy of 5 in its own local x. It sets that copy to 6, then its frame is popped. The caller's n is untouched.
increment receives the address of n, so x and n are the same location. Setting x = x + 1 writes 6 into n itself.
Result: (a) n is still 5; (b) n is 6. The only difference is whether the parameter is a copy or an alias of the caller's variable.
Typical mistakes
Active revision
A function swap(a, b) exchanges its two parameters. Explain why, if a and b are passed by value, the caller's variables are unchanged after the call, and what must change for the swap to work.
Active recall
Recall the key points — then reveal.
Sources: AQA A-level Computer Science 7517 specification (AQA)
Recursive factorial
The base case stops the recursion; the recursive case reduces towards 0.
Trace factorial(4), where factorial(n) returns 1 if n = 0 else n * factorial(n-1). Show the descent to the base case and the returns unwinding.
factorial(4) needs 4factorial(3); factorial(3) needs 3factorial(2); factorial(2) needs 2factorial(1); factorial(1) needs 1factorial(0).
factorial(0) returns 1 - the recursion stops here and the frames now unwind.
factorial(1) = ; factorial(2) = ; factorial(3) = ; factorial(4) = .
Result: factorial(4) = 24. Five frames were pushed (n = 4 down to 0) and popped in reverse - the call stack in action.
Typical mistakes
Active revision
Write a recursive function factorial(n), state its base case, and trace factorial(4) call by call, showing the returns unwinding back up the stack.
Active recall
Recall the key points — then reveal.
Sources: GCE AS and A level subject content for computer science (Department for Education)
A class inheritance hierarchy
Animal defines describe() returning "An animal". Dog inherits from Animal and overrides describe() to return "A dog that barks". If a variable of type Animal actually references a Dog object and describe() is called, what is returned, and what is this called?
The variable's declared type is Animal, but at run time it references a Dog object. Method calls resolve on the actual object's class, not the declared type.
Because Dog overrides describe(), the Dog version is chosen. If Dog had not overridden it, the inherited Animal version would run instead.
Result: It returns "A dog that barks". Selecting the overriding method at run time from the object's real class is polymorphism (dynamic dispatch).
Typical mistakes
Active revision
Design a class hierarchy for a library with a base class Item and subclasses Book and DVD. State one attribute and one method for Item that all items share, and one attribute unique to each subclass, and explain where polymorphism would be useful.
Active recall
Recall the key points — then reveal.
Sources: AQA A-level Computer Science 7517 specification (AQA)
References & sources
Department for Education