A-Level Computer Science: Programming and Paradigms

Most of these have to be traced. A loop whose body contains two additions rather than one runs four times and prints fourteen; the wrong options are the sum of the counters with the constant forgotten, that sum with the loop read as running to five, and the value you get by adding the constant once after the loop instead of once on every pass. A recursive function three calls deep returns six, with the value produced by a base case that stops one step early sitting beside it.

Two questions are about where a name points. A procedure declares a local variable that happens to share a name with one outside it, assigns to it, prints it, and returns — twenty, then five, because there were two variables all along. The explanation makes the point that the declaration is what settles this: without it, whether an assignment inside a procedure creates a local or writes to the outer variable depends on the language, and a question that leaves that out has no single right answer. The other is the same idea at the boundary of a call: one procedure, one line, doubling its parameter, run once by value and once by reference, printing seven in one case and fourteen in the other.

Object orientation is done by matching rather than reciting. A class keeps its balance where nothing outside can reach it and offers methods that check before they change — encapsulation. A subclass supplies its own version of a method its parent already has, so the same call behaves differently depending on the object it lands on — polymorphism, and confusing that with the inheritance that makes it possible is the usual slip. Then a Printer and a Scanner that both need to log something, where a subclass relationship would assert that a printer is a kind of scanner, and composition is the alternative.

The last four are judgement rather than trace. A paradigm is chosen against a stated requirement — a pipeline of transformations that must be reproducible — with the right answer available for the wrong reason ("it is the most modern") as a distractor. A function valid from one to a hundred is tested at zero, one, a hundred and a hundred and one, not in the comfortable middle. A program that runs to the end and prints the wrong number has a logic error, which tells you where to look. And a refactor that pulls four repeated lines into one procedure buys maintainability, not speed, because nothing in the case said anything about speed.

Every program described is invented, and every trace has been checked by hand. Nothing is reproduced from any exam board specification, past paper or mark scheme.

Zestly is an independent study tool. It is not affiliated with any exam board and it is not an exam centre.

  • Trace a loop whose body changes the accumulator more than once, and avoid the off-by-one
  • Trace a recursive call down to its base case and back
  • Explain shadowing, and say what determines whether an assignment writes to a local or an outer variable
  • Say what a procedure prints when its argument is passed by value and when it is passed by reference
  • Tell encapsulation, inheritance, polymorphism and abstraction apart from what a class actually does
  • Recognise where composition belongs and a subclass relationship does not
  • Choose a paradigm against a stated requirement rather than by fashion
  • Pick test values that exercise a boundary, and diagnose a logic error from its symptom

Ten invented programs and design cases: a four-pass loop with two additions in its body, calc(3) returning six, a local declaration shadowing an outer count, one procedure doubling its parameter by value and by reference, a BankAccount and a SavingsAccount, a Printer that is not a kind of Scanner, a reproducible pipeline of transformations, a function valid from one to a hundred, a wrong total from an initialiser, and four lines repeated three times. Twelve flashcards carry the vocabulary — iteration, recursion, base case, stack frame, pass by value and by reference, local and global scope, encapsulation, inheritance, polymorphism, boundary testing.

Sample question

Consider this pseudocode: ```pseudocode count = 5 PROCEDURE tally() DECLARE count AS LOCAL count = 20 PRINT count END PROCEDURE CALL tally() PRINT count ``` What two values are printed, in that order?

See the answer

Twenty and then five: the declaration inside the procedure creates a second variable that merely happens to share the name, so the assignment lands on that one, and the outer variable is untouched once the procedure ends.

There are two variables here, not one. The line declaring count as local inside the procedure creates a new variable that exists only while the procedure runs, and from that point until the procedure ends the name refers to the inner one. So the assignment of twenty and the print inside the procedure both concern the local variable, giving twenty; when the procedure ends, that variable is gone and the name refers to the outer one again, which never changed, giving five. This is called shadowing, and it is worth noticing that the declaration is what settles it — without it, whether an assignment inside a procedure creates a local or writes to the outer variable depends on the language, and a question that leaves it out has no single right answer. Believing both prints give twenty is treating the two as one variable. Believing both give five ignores the print inside the procedure. And nothing here defers an update until afterwards; the outer variable is never written to at all.

Try this quiz →Try this exam →Practice these flashcards →Try this written work →

← Computer Science

↑ A-Level

A levels are awarded by AQA, Pearson Edexcel, OCR, WJEC/Eduqas and CCEA, and regulated by Ofqual, Qualifications Wales and CCEA Regulation, the regulators for England, Wales and Northern Ireland. Zestly is not affiliated with or endorsed by Ofqual, UCAS or any awarding body. These materials are our own and reproduce no past-paper content.