Computer Science
Algorithms, programming and logic — Paper 2
- 1.
Write pseudocode that repeatedly inputs a test score until the value is between 0 and 100 inclusive, then outputs ‘valid’. Use a post-test or pre-test loop.
[4 marks] · no calculatorMarking points
- Inputs a score before or within a suitable loop.
- Tests both lower and upper bounds correctly.
- Repeats while the score is outside 0 to 100, with correct Boolean logic.
- Outputs the valid message only after the loop terminates.
Examiner tip: For an invalid value use score < 0 OR score > 100; using AND makes the condition impossible.
- 2.
A program calculates delivery charges from parcel weight. Design four test cases covering normal, boundary, invalid and extreme data. State the input and purpose of each case.
[4 marks] · no calculatorMarking points
- Provides a plausible normal value and labels its purpose.
- Provides an exact boundary value relevant to a charging band.
- Provides invalid data such as a negative weight or wrong data type.
- Provides an extreme but valid value and explains the stress being tested.
Examiner tip: A boundary test uses the exact limit; values just below and above it are additional boundary checks.
- 3.
Write pseudocode to find and output the largest value in an array of 10 integers called Scores.
[4 marks] · no calculatorMarking points
- Declares a variable to hold the largest value, initialised to the first array element.
- Uses a loop to traverse the remaining elements of the array.
- Compares each element to the current largest value and updates it when a bigger value is found.
- Outputs the largest value after the loop ends.
Examiner tip: Initialise the 'largest' variable with the first element, not zero — scores could be negative or the array could contain only small positive values.
- 4.
The list [5, 2, 8, 1] is sorted into ascending order using a bubble sort. State the list after the first full pass.
[3 marks] · no calculatorMarking points
- Compares 5 and 2, swaps them to give [2, 5, 8, 1].
- Compares 5 and 8, no swap needed, giving [2, 5, 8, 1].
- Compares 8 and 1, swaps them to give the final first-pass result [2, 5, 1, 8].
Examiner tip: A bubble sort compares each adjacent pair in turn during a pass, swapping whenever the left value is greater than the right.
- 5.
A list of 1,000 names is already sorted alphabetically. Which search algorithm will generally find a specific name fastest? A Linear search B Binary search C Bubble sort D Insertion sort
[1 mark] · no calculatorMarking points
- Selects B: binary search repeatedly halves a sorted list, finding items far faster than checking each one in turn.
Examiner tip: Binary search requires the list to already be sorted; it cannot be used correctly on unsorted data.
- 6.
Write pseudocode using nested IF statements that outputs a grade for a given Score: 'A' if the score is 90 or above, 'B' if it is 75 or above but below 90, and 'C' otherwise.
[3 marks] · no calculatorMarking points
- Tests the highest condition first: IF Score >= 90 THEN output 'A'.
- Nests an ELSE IF (or ELSE containing IF) testing Score >= 75 to output 'B'.
- Provides a final ELSE branch that outputs 'C' for any remaining score.
Examiner tip: Testing from the highest boundary downward avoids a lower condition like Score >= 75 wrongly catching a 95 before it reaches the 'A' check.
- 7.
Explain one advantage of using a function (subroutine) rather than repeating the same code several times in a program.
[2 marks] · no calculatorMarking points
- Explains that a function keeps the logic in one place, so the program is shorter and easier to read.
- Explains that if the logic needs to change, it only needs to be updated once inside the function, reducing errors and maintenance effort.
Examiner tip: Repeated code must be found and changed everywhere it appears; a function only needs to be changed once.
- 8.
A form asks a user to enter their age as a whole number between 1 and 120. Identify two different validation checks that could be applied, and explain what each one checks for.
[4 marks] · no calculatorMarking points
- Identifies a range check.
- Explains that a range check confirms the value falls between 1 and 120 inclusive.
- Identifies a type check.
- Explains that a type check confirms the value entered is a whole number (integer), not text or a decimal.
Examiner tip: A range check and a type check examine different properties of the same input and are often both needed together.
- 9.
A loop runs the pseudocode 'Count ← Count + 1' three times, starting with Count = 0. Complete a trace table showing the value of Count after each of the three iterations.
[3 marks] · no calculatorMarking points
- States that Count = 1 after the first iteration.
- States that Count = 2 after the second iteration.
- States that Count = 3 after the third iteration.
Examiner tip: A trace table tracks how a variable's value changes step by step as a program or loop executes.
- 10.
Write pseudocode to calculate and output the average of 5 numbers stored in an array called Marks.
[4 marks] · no calculatorMarking points
- Declares a total variable initialised to zero before the loop.
- Uses a loop to add each of the 5 array elements to the total.
- Divides the final total by 5 after the loop ends to calculate the average.
- Outputs the calculated average.
Examiner tip: Always initialise a running total to zero before a loop begins adding to it; forgetting this gives an incorrect result.
- 11.
Explain the difference between a variable and a constant in a program.
[2 marks] · no calculatorMarking points
- Explains that a variable's value can be changed while the program is running.
- Explains that a constant's value is fixed and set once, and cannot be changed while the program runs.
Examiner tip: Use a constant for a value that should never change during execution, such as the number of days in a week, to prevent it being accidentally altered.
- 12.
Explain why a 2D array would be a suitable data structure to store the seating plan of a cinema with rows and columns of seats.
[2 marks] · no calculatorMarking points
- Explains that a 2D array organises data using two indices, which can directly represent a row number and a column number.
- Explains that this makes it easy to store and access the status of a seat at any specific row and column position, matching the real layout.
Examiner tip: A 2D array is a natural fit whenever real-world data is already organised in a grid, such as rows and columns of seats.
- 13.
Explain what the string length function would return when applied to the text 'Cambridge', and give one reason a programmer might need this value.
[2 marks] · no calculatorMarking points
- States that the length function would return 9, since 'Cambridge' has 9 characters.
- States a valid reason, such as validating that a username or password meets a minimum or maximum length requirement.
Examiner tip: String length functions are commonly used in validation, to check an input is neither too short nor too long before accepting it.
- 14.
Distinguish between a count-controlled loop and a condition-controlled loop, using an example of when each would be appropriate.
[2 marks] · no calculatorMarking points
- Explains that a count-controlled loop (such as a FOR loop) repeats a fixed, known number of times, suitable for processing every element of an array of known size.
- Explains that a condition-controlled loop (such as a WHILE loop) repeats until a condition becomes true or false, suitable when the number of repetitions is not known in advance, such as repeating until valid input is entered.
Examiner tip: Choose a count-controlled loop when you know exactly how many repetitions you need in advance; choose a condition-controlled loop when you do not.
- 15.
The list [8, 3, 6, 1] is sorted into ascending order using an insertion sort. State the list after the algorithm has correctly inserted the second element (3) into its sorted position among the first two elements.
[2 marks] · no calculatorMarking points
- Compares 3 with 8 and finds 3 is smaller.
- Shifts 8 to the right and inserts 3 before it, giving [3, 8, 6, 1].
Examiner tip: Insertion sort builds a sorted section at the start of the list one element at a time, shifting larger elements right to make room.
- 16.
Write pseudocode for a linear search that looks for a Target value in an array called Items, and outputs whether the value was found.
[4 marks] · no calculatorMarking points
- Uses a loop to check each element of the array in turn, starting from the first.
- Compares each element to the Target value.
- Outputs that the value was found if a match is found during the loop.
- Outputs that the value was not found if the loop completes without finding a match.
Examiner tip: A linear search checks every element one at a time until a match is found or the list ends — simple, but slower than binary search on large sorted lists.
- 17.
Explain what a decision (diamond-shaped) symbol represents in a flowchart.
[2 marks] · no calculatorMarking points
- Explains that a decision symbol represents a point where the program checks a condition, which is either true or false.
- Explains that the flowchart branches into different paths depending on the result of this check.
Examiner tip: Every path leaving a decision symbol should be clearly labelled, usually 'Yes'/'No' or 'True'/'False'.
- 18.
Write pseudocode to count how many values in an array called Scores are greater than 50.
[4 marks] · no calculatorMarking points
- Declares a counter variable initialised to zero before the loop.
- Uses a loop to check every element of the array in turn.
- Increases the counter by 1 only when an element is greater than 50.
- Outputs the final counter value after the loop ends.
Examiner tip: A counting algorithm only increases its counter when the condition is met, unlike a totaling algorithm, which adds the actual value every time.
- 19.
Explain one advantage of writing a procedure that accepts parameters, rather than one that always works on fixed, built-in values.
[2 marks] · no calculatorMarking points
- Explains that a procedure with parameters can be reused with different input values each time it is called, instead of only ever working on one fixed value.
- Explains that this makes the code more flexible and avoids having to write a near-identical procedure for every different input value needed.
Examiner tip: Parameters let one procedure handle many different cases, rather than needing a separate, duplicated procedure for each specific value.
- 20.
A form requires a user to enter their name. Explain what a presence check would do, and why it is needed for this field.
[2 marks] · no calculatorMarking points
- Explains that a presence check confirms that the user has not left the field completely empty.
- Explains that this is needed because a name field is a required piece of data, and the program should not accept a submission that is missing it.
Examiner tip: A presence check is the simplest form of validation: it only confirms that something was entered, not that it is the right kind of data.
- 21.
A system requires a new password to be at least 8 characters long. Explain what a length check would do when this password is entered.
[2 marks] · no calculatorMarking points
- Explains that a length check counts the number of characters entered.
- Explains that it rejects the input if this count is below the minimum (or above a maximum, if one is set) allowed length.
Examiner tip: A length check specifically counts characters; it does not check whether those characters are of the correct type.
- 22.
Explain, in simple terms, what it means for a function to be recursive, using a function that calculates factorials as an example.
[2 marks] · no calculatorMarking points
- Explains that a recursive function is a function that calls itself as part of its own execution.
- Applies this to the example: a factorial function can calculate factorial(n) by calling itself to calculate factorial(n-1), and so on, until it reaches a base case.
Examiner tip: Every recursive function needs a base case — a point where it stops calling itself — otherwise it would repeat forever.
- 23.
Explain one feature of an Integrated Development Environment (IDE) that helps a programmer write code with fewer errors.
[2 marks] · no calculatorMarking points
- Identifies a valid feature, such as syntax highlighting, auto-complete, or a built-in debugger.
- Explains how this feature helps, such as syntax highlighting making it easier to spot a missing bracket or misspelled keyword.
Examiner tip: IDE tools exist specifically to catch common mistakes early, before the program is even run.
- 24.
Explain the purpose of using meaningful variable names, such as 'studentAge' instead of 'x', when writing a program.
[2 marks] · no calculatorMarking points
- Explains that a meaningful name makes it clear what kind of data the variable stores, without needing to read the surrounding code carefully.
- Explains that this makes the program easier for the original programmer, or someone else, to read, understand and maintain later.
Examiner tip: Meaningful names cost nothing to the computer running the program, but save real time for any human who later reads the code.