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.
Programming Practice: Writing and Refining Programs revision guideMore GCSE Computer Science practiceMock exams
This topic is chapter 5 of GCSE Computer Science Topic Workbook 2 and chapter 15 of GCSE Computer Science 10-Minute Tests.
2.7 Programming Practice: Writing and Refining Programs
- "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
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 = 0
FOR i = 1 TO 4
OUTPUT i
NEXT itotal = 0
FOR i = 1 TO 4
OUTPUT i
NEXT iIdentify the error in the algorithm.(1)
FUNCTION calculateTotal(numbers)
total = 0
FOR i = 1 TO LEN(numbers)
total = total + numbers[i]
NEXT i
RETURN total
ENDFUNCTIONFUNCTION calculateTotal(numbers)
total = 0
FOR i = 1 TO LEN(numbers)
total = total + numbers[i]
NEXT i
RETURN total
ENDFUNCTIONThe array is 0-indexed, so a 5-element array has valid indices 0 to 4.
Identify the error in the function.(1)
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.
Write an algorithm that uses nested iteration to calculate and output the total of all 9 readings in the grid.
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
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 ENDWHILECLOSEFILE(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 ENDWHILECLOSEFILE(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 ENDWHILECLOSEFILE(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.