Skip to the worksheet
Revision Library
Choose a subject

Programming Practice: Writing and Refining Programs - Worksheets, Questions and Revision

15 original exam-style questions - 8 pages of questions with a full mark scheme - free printable PDF.

Grades 1-9

This topic is chapter 5 of GCSE Computer Science Topic Workbook 2 and chapter 15 of GCSE Computer Science 10-Minute Tests.

Revision Library
revisionlibrary.co.uk
GCSE · Programming

2.7 Programming Practice: Writing and Refining Programs

OCR J277 · Calculators not allowed · about 80 minutes
Total Marks
Name: _______________________________    Date: ____ / ____ / ______
Answer ALL questions. Show all your working.
1
Write an algorithm, using a count-controlled loop, that outputs the sum of the whole numbers from 1 to 20 inclusive.
(Total for Question 1 is 3 marks)
2
A simple program advises drivers based on a temperature reading (in degrees Celsius). Write an algorithm that inputs a temperature and outputs:
- "Ice warning" if the temperature is less than or equal to 0
- "Mild" if the temperature is greater than 0 and less than 20
- "Warm" if the temperature is 20 or greater
(Total for Question 2 is 4 marks)
3
An array called scores stores the test scores of 8 students:
scores = [56, 78, 45, 90, 33, 67, 82, 71]

Write an algorithm that uses iteration to calculate and output the average score of the class.
(Total for Question 3 is 5 marks)
4
Write an algorithm that repeatedly asks the user to input a whole number between 1 and 10 (inclusive), rejecting any input outside this range, until a valid number is entered. The algorithm should then output the valid number.
(Total for Question 4 is 4 marks)
5
Write a function called applyDiscount that takes two parameters, price (a real number, in pounds) and percentageOff (an integer), and returns the price after the percentage discount has been subtracted.
(Total for Question 5 is 4 marks)
6
A student has written the following algorithm, which is intended to output the numbers 1 to 5, but it only outputs 1 to 4:
total = 0
FOR i = 1 TO 4
    OUTPUT i
NEXT i
(a)A student has written the following algorithm, which is intended to output the numbers 1 to 5, but it only outputs 1 to 4:
total = 0
FOR i = 1 TO 4
    OUTPUT i
NEXT i

Identify the error in the algorithm.
(1)
(b)Write the corrected FOR loop line so that the algorithm outputs 1 to 5.(2)
(Total for Question 6 is 3 marks)
7
Write a function called countVowels that takes a string parameter word and returns the number of vowels (a, e, i, o, u) in the string, ignoring case.
(Total for Question 7 is 5 marks)
8
A text file called "attendance.txt" contains one student name per line. Write an algorithm that opens the file, reads every line until the end of the file, and outputs the total number of names in the file.
(Total for Question 8 is 4 marks)
9
Write an algorithm that outputs the numbers 1 to 30. For multiples of 3, output "Fizz" instead of the number. For multiples of 5, output "Buzz" instead of the number. For multiples of both 3 and 5, output "FizzBuzz" instead of the number.
(Total for Question 9 is 5 marks)
10
A function is intended to return the total of all the numbers in an array called numbers, but it returns an incorrect result:
FUNCTION calculateTotal(numbers)
    total = 0
    FOR i = 1 TO LEN(numbers)
        total = total + numbers[i]
    NEXT i
    RETURN total
ENDFUNCTION
(a)A function is intended to return the total of all the numbers in an array called numbers, but it returns an incorrect result:
FUNCTION calculateTotal(numbers)
    total = 0
    FOR i = 1 TO LEN(numbers)
        total = total + numbers[i]
    NEXT i
    RETURN total
ENDFUNCTION

The array is 0-indexed, so a 5-element array has valid indices 0 to 4.

Identify the error in the function.
(1)
(b)Rewrite the FOR loop line so that the function works correctly.(2)
(Total for Question 10 is 3 marks)
11
An array called names stores the names of students in a class:
names = ["Priya", "Oliver", "Fatima", "Callum", "Grace"]

Write a function called findStudent that takes the array names and a string searchName as parameters, and returns the index of searchName in the array if found, or -1 if the name is not found.
(Total for Question 11 is 5 marks)
12
A 2D array called grid stores temperature readings (in degrees Celsius) for a 3 by 3 grid of sensors, accessed using grid[row, column], with rows and columns numbered 0 to 2.

Write an algorithm that uses nested iteration to calculate and output the total of all 9 readings in the grid.
(Total for Question 12 is 5 marks)
13
Write a function called findMax that takes an array of integers, values, as a parameter and returns the largest value in the array. You may assume the array contains at least one element.
(Total for Question 13 is 5 marks)
14
A text file called "results.txt" contains one exam mark (an integer out of 100) per line, for every student in a class. Write an algorithm that reads every mark from "results.txt", and writes only the marks that are 40 or above to a new file called "pass.txt", with one mark per line. The algorithm should output the total number of passing marks written.
(Total for Question 14 is 6 marks)
15
A shop stores its stock data in a text file called "stock.txt". The file lists each item on its own line, followed immediately by its quantity on the next line, repeated for every item, for example:
Kettle
14
Toaster
3

Write a program that:
- reads all the stock data from "stock.txt" into two parallel arrays, itemNames and quantities, keeping track of how many items were read in a variable itemCount
- includes a function called findItem that takes itemNames, itemCount and a search name as parameters, and returns the index of the item, or -1 if not found
- asks the user to input an item name to check
- uses findItem to look up the item and outputs its quantity, or a suitable message if the item is not found
- if the item is found and its quantity is less than 5, additionally outputs a low stock warning
(Total for Question 15 is 8 marks)
Mark scheme · 2.7 Programming Practice: Writing and Refining Programs

Question 1

  • a total (accumulator) variable initialised to 0 before the loopB1
  • correct FOR loop from 1 TO 20 that adds the loop counter to the total on each iterationB1
  • OUTPUT of the total placed after the loop has finished (oe)B1
  • Answer:
    total = 0
    FOR i = 1 TO 20
        total = total + i
    NEXT i
    OUTPUT total

Question 2

  • INPUT statement storing the temperatureB1
  • correct condition and output for the "Ice warning" branch (temperature ≤ 0)B1
  • correct condition and output for the "Warm" branch (temperature ≥ 20), using a nested/ELSE IF selection structureB1
  • correct output for "Mild" as the remaining case, with all three outcomes mutually exclusive (oe; accept any correctly ordered nested selection)B1
  • Answer:
    INPUT temperature
    IF temperature <= 0 THEN
        OUTPUT "Ice warning"
    ELSE
        IF temperature < 20 THEN
            OUTPUT "Mild"
        ELSE
            OUTPUT "Warm"
        ENDIF
    ENDIF

Question 3

  • a total variable initialised to 0 before the loopB1
  • correct count-controlled loop that iterates over every element of the array (indices 0 to 7, or equivalent)B1
  • correct accumulation, total = total + scores[i], inside the loopB1
  • correct division of the total by 8 (the number of elements) after the loop, to calculate the averageB1
  • OUTPUT of the average valueB1
  • Answer:
    scores = [56, 78, 45, 90, 33, 67, 82, 71]
    total = 0
    FOR i = 0 TO 7
        total = total + scores[i]
    NEXT i
    average = total / 8
    OUTPUT average

Question 4

  • correct condition-controlled loop structure with the exit-check after the input (e.g. REPEAT...UNTIL), or an equivalent entry-check loop that repeats correctlyB1
  • INPUT statement placed inside the loopB1
  • correct UNTIL/loop condition testing number ≥ 1 AND number ≤ 10 (oe)B1
  • OUTPUT of the valid number placed after the loop has endedB1
  • Answer:
    REPEAT
        INPUT number
    UNTIL number >= 1 AND number <= 10
    OUTPUT number

Question 5

  • correct FUNCTION header with two parameters, e.g. FUNCTION applyDiscount(price, percentageOff)B1
  • correct calculation of the discount amount, e.g. discount = price * percentageOff / 100 (oe)B1
  • correct calculation of the new price by subtracting the discount from the original price (oe)B1
  • correct RETURN of the new price, with the function correctly closed using ENDFUNCTION (any correct logic; ignore minor syntax)B1
  • Answer:
    FUNCTION applyDiscount(price, percentageOff)
        discount = price * percentageOff / 100
        newPrice = price - discount
        RETURN newPrice
    ENDFUNCTION

Question 6

  • (a) identifies that the loop's upper bound is set to 4 instead of 5, so the loop only executes for i = 1, 2, 3, 4 and misses 5 (oe, must reference the incorrect bound value)B1
  • (a) Answer: The FOR loop's upper bound is set to 4 instead of 5, so it only outputs 1, 2, 3 and 4, and stops before it can output 5.
  • (b) correct corrected FOR loop line: FOR i = 1 TO 5B1
  • (b) the rest of the algorithm (NEXT i and OUTPUT i) is left unchanged and still functionally correctB1
  • (b) Answer: FOR i = 1 TO 5

Question 7

  • correct FUNCTION header with one string parameter, e.g. FUNCTION countVowels(word)B1
  • a count variable initialised to 0 before iteratingB1
  • correct iteration through every character of the string (e.g. FOR i = 0 TO LEN(word) - 1, oe) using a case-conversion function so case is ignoredB1
  • correct selection testing the current character against the five vowels, combined with OR, and incrementing count when trueB1
  • correct RETURN of count after the loop has endedB1
  • Answer:
    FUNCTION countVowels(word)
        count = 0
        FOR i = 0 TO LEN(word) - 1
            letter = UPPER(SUBSTRING(i, 1, word))
            IF letter == "A" OR letter == "E" OR letter == "I" OR letter == "O" OR letter == "U" THEN
                count = count + 1
            ENDIF
        NEXT i
        RETURN count
    ENDFUNCTION

Question 8

  • correct file opened for reading, e.g. myFile = OPENFILE("attendance.txt", READ)B1
  • a count variable initialised to 0 before the loopB1
  • correct condition-controlled loop that continues WHILE NOT EOF(myFile), reading a line with READFILE(myFile) and incrementing count on each iterationB1
  • OUTPUT of count placed after the loop, with the file closed using CLOSEFILE(myFile) (oe; accept the close statement in either position relative to the output)B1
  • Answer:
    myFile = OPENFILE("attendance.txt", READ)
    count = 0
    WHILE NOT EOF(myFile)
        line = READFILE(myFile)
        count = count + 1
    ENDWHILE
    CLOSEFILE(myFile)
    OUTPUT count

Question 9

  • correct FOR loop from 1 to 30B1
  • correct condition, tested first, for multiples of both 3 and 5 (e.g. number MOD 15 == 0, or number MOD 3 == 0 AND number MOD 5 == 0)B1
  • correct condition and output for multiples of 3 only (number MOD 3 == 0)B1
  • correct condition and output for multiples of 5 only (number MOD 5 == 0)B1
  • correct final ELSE branch outputting the number itself when none of the above conditions are trueB1
  • Answer:
    FOR number = 1 TO 30
        IF number MOD 15 == 0 THEN
            OUTPUT "FizzBuzz"
        ELSE
            IF number MOD 3 == 0 THEN
                OUTPUT "Fizz"
            ELSE
                IF number MOD 5 == 0 THEN
                    OUTPUT "Buzz"
                ELSE
                    OUTPUT number
                ENDIF
            ENDIF
        ENDIF
    NEXT number

Question 10

  • (a) identifies that the loop runs from index 1 to LEN(numbers), which skips index 0 (the first element) and attempts to access index LEN(numbers), which is out of range (oe; accept identification of either half of the error)B1
  • (a) Answer: The loop starts at index 1 instead of 0, so it misses the first element (index 0), and it runs up to LEN(numbers), which is one index beyond the last valid index (LEN(numbers) - 1), causing an out-of-range array access.
  • (b) correct loop start changed to 0B1
  • (b) correct loop end changed to LEN(numbers) - 1B1
  • (b) Answer: FOR i = 0 TO LEN(numbers) - 1

Question 11

  • correct FUNCTION header taking both the array and the search name as parameters, e.g. FUNCTION findStudent(names, searchName)B1
  • correct iteration through every index of the array (e.g. FOR i = 0 TO LEN(names) - 1, oe)B1
  • correct selection comparing the current array element to searchName, e.g. IF names[i] == searchName THENB1
  • correct RETURN of the current index i inside the loop when a match is foundB1
  • correct RETURN of -1 placed after the loop, only reached when no match has been foundB1
  • Answer:
    FUNCTION findStudent(names, searchName)
        FOR i = 0 TO LEN(names) - 1
            IF names[i] == searchName THEN
                RETURN i
            ENDIF
        NEXT i
        RETURN -1
    ENDFUNCTION

Question 12

  • a total variable initialised to 0 before the loopsB1
  • correct outer loop iterating rows from 0 to 2 (oe)B1
  • correct inner loop iterating columns from 0 to 2, correctly nested inside the outer loop (oe)B1
  • correct accumulation, total = total + grid[row, column], inside the inner loopB1
  • OUTPUT of total placed after both loops have completedB1
  • Answer:
    total = 0
    FOR row = 0 TO 2
        FOR column = 0 TO 2
            total = total + grid[row, column]
        NEXT column
    NEXT row
    OUTPUT total

Question 13

  • correct FUNCTION header with one array parameter, e.g. FUNCTION findMax(values)B1
  • correct initialisation of a running maximum variable, set to the first element of the array, values[0] (oe)B1
  • correct loop iterating through the remaining elements of the array (e.g. FOR i = 1 TO LEN(values) - 1, oe)B1
  • correct selection comparing each element to the running maximum and updating it whenever a larger value is foundB1
  • correct RETURN of the maximum value after the loop has endedB1
  • Answer:
    FUNCTION findMax(values)
        maximum = values[0]
        FOR i = 1 TO LEN(values) - 1
            IF values[i] > maximum THEN
                maximum = values[i]
            ENDIF
        NEXT i
        RETURN maximum
    ENDFUNCTION

Question 14

  • correct read file opened for reading, e.g. inFile = OPENFILE("results.txt", READ)B1
  • correct write file opened for writing, e.g. outFile = OPENFILE("pass.txt", WRITE)B1
  • a count variable initialised to 0 before the loopB1
  • correct condition-controlled loop reading each line WHILE NOT EOF(inFile), casting each value to an integer with INT()B1
  • correct selection testing mark ≥ 40 and, when true, writing the mark to outFile using WRITEFILE (with STR() to cast back to a string) and incrementing countB1
  • both files closed with CLOSEFILE after the loop, and count output (oe; accept close statements in either order, provided both files are closed)B1
  • Answer:
    inFile = OPENFILE("results.txt", READ)
    outFile = OPENFILE("pass.txt", WRITE)
    count = 0
    WHILE NOT EOF(inFile)
        mark = INT(READFILE(inFile))
        IF mark >= 40 THEN
            WRITEFILE(outFile, STR(mark))
            count = count + 1
        ENDIF
    ENDWHILE
    CLOSEFILE(inFile)
    CLOSEFILE(outFile)
    OUTPUT count

Question 15

  • correct file opened for reading and a condition-controlled loop that reads until WHILE NOT EOF(stockFile) becomes falseB1
  • correct reading of a name line then a quantity line (cast to an integer with INT()) inside the loop, storing each into the respective array at index itemCount and incrementing itemCount (oe; accept declared fixed-size arrays with a running counter, or any other correct method of building the two arrays)B1
  • correct findItem function header taking the array, itemCount and searchName as parametersB1
  • correct iteration (e.g. FOR i = 0 TO itemCount - 1) and selection returning the matching index i inside the loopB1
  • correct RETURN of -1 placed after the loop, only reached if no match was foundB1
  • correct INPUT for the search name, and a call to findItem storing the result in a variableB1
  • correct selection: IF index == -1 THEN a not-found message is output, ELSE quantities[index] is outputB1
  • correct nested check, quantities[index] < 5, correctly nested inside the found branch, outputting a low stock warning when trueB1
  • Answer:
    itemCount = 0
    DECLARE itemNames : ARRAY[0:49] OF STRING
    DECLARE quantities : ARRAY[0:49] OF INTEGER
    stockFile = OPENFILE("stock.txt", READ)
    WHILE NOT EOF(stockFile)
        itemNames[itemCount] = READFILE(stockFile)
        quantities[itemCount] = INT(READFILE(stockFile))
        itemCount = itemCount + 1
    ENDWHILE
    CLOSEFILE(stockFile)
    FUNCTION findItem(itemNames, itemCount, searchName)
        FOR i = 0 TO itemCount - 1
            IF itemNames[i] == searchName THEN
                RETURN i
            ENDIF
        NEXT i
        RETURN -1
    ENDFUNCTION
    
    INPUT searchName
    index = findItem(itemNames, itemCount, searchName)
    IF index == -1 THEN
        OUTPUT "Item not found"
    ELSE
        OUTPUT quantities[index]
        IF quantities[index] < 5 THEN
            OUTPUT "Low stock warning"
        ENDIF
    ENDIF

Mark your answers

Check answers without an account. Some answers can be checked automatically; use the mark scheme to mark the rest.

Question 1

3 marks
Did your answer earn the marks?

Question 2

4 marks
Did your answer earn the marks?

Question 3

5 marks
Did your answer earn the marks?

Question 4

4 marks
Did your answer earn the marks?

Question 5

4 marks
Did your answer earn the marks?

Question 6

3 marks
Did your answer earn the marks?

Question 7

5 marks
Did your answer earn the marks?

Question 8

4 marks
Did your answer earn the marks?

Question 9

5 marks
Did your answer earn the marks?

Question 10

3 marks
Did your answer earn the marks?

Question 11

5 marks
Did your answer earn the marks?

Question 12

5 marks
Did your answer earn the marks?

Question 13

5 marks
Did your answer earn the marks?

Question 14

6 marks
Did your answer earn the marks?

Question 15

8 marks
Did your answer earn the marks?
Mark my answers

Worksheets are written for Revision Library and come with a full mark scheme; a few English packs quote an out-of-copyright text, and name it on the page. Exam board names and specification codes say which qualification a pack suits: we are not affiliated with, endorsed by or approved by any exam board, and no past-paper question is reproduced here. Rights queries: hello@revisionlibrary.co.uk