Revision Library

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

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

Download PDFJump to mark scheme (page 15)Read the revision guide
« Previous: Data Representation: Images, Sound and CompressionNext: SQL and Data Handling »
Revision Library
revisionlibrary.co.uk
GCSE · Programming

P2.7 Programming Practice: Writing and Refining Programs

OCR J277 · Calculators not allowed · about 135 minutes
Total Marks
Name: _______________________________    Date: ____ / ____ / ______
Answer ALL questions. Show all your working.
1
A program should input a whole number and output whether it is odd or even.

Write an algorithm that inputs an integer and outputs "Even" if the number is exactly divisible by 2, or "Odd" otherwise.
(a)A program should input a whole number and output whether it is odd or even.

Write an algorithm that inputs an integer and outputs "Even" if the number is exactly divisible by 2, or "Odd" otherwise.
(3)
(Total for Question 1 is 3 marks)
2
Write an algorithm, using a count-controlled loop, that outputs the sum of the whole numbers from 1 to 20 inclusive.
(a)Write an algorithm, using a count-controlled loop, that outputs the sum of the whole numbers from 1 to 20 inclusive.(3)
(Total for Question 2 is 3 marks)
3
A string variable is set as: name = "Aditi"
(a)A string variable is set as:
name = "Aditi"

State the value of LEN(name).
(1)
(b)Write one line of code that outputs the first three characters of name, using SUBSTRING(startIndex, numberOfChars, string).(2)
(Total for Question 3 is 3 marks)
4
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
(a)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
(4)
(Total for Question 4 is 4 marks)
5
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.
(a)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.
(5)
(Total for Question 5 is 5 marks)
6
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.
(a)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.(4)
(Total for Question 6 is 4 marks)
7
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.
(a)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.(4)
(Total for Question 7 is 4 marks)
8
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 8 is 3 marks)
9
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.
(a)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.
(5)
(Total for Question 9 is 5 marks)
10
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.
(a)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.(5)
(Total for Question 10 is 5 marks)
11
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.
(a)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.(4)
(Total for Question 11 is 4 marks)
12
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.
(a)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.(5)
(Total for Question 12 is 5 marks)
13
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 13 is 3 marks)
14
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.
(a)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.
(5)
(Total for Question 14 is 5 marks)
15
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.
(a)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.(5)
(Total for Question 15 is 5 marks)
16
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.
(a)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.(6)
(Total for Question 16 is 6 marks)
17
Write a function called isValidPassword that takes a string parameter password and returns a Boolean: True if the password is at least 8 characters long AND contains at least one digit (0-9), or False otherwise.
(a)Write a function called isValidPassword that takes a string parameter password and returns a Boolean: True if the password is at least 8 characters long AND contains at least one digit (0-9), or False otherwise.(6)
(Total for Question 17 is 6 marks)
18
A student has written the following function, intended to search for a value target in an array numbers and return its index, or -1 if not found. When tested, the function sometimes returns the wrong result:
FUNCTION search(numbers, target)
    FOR i = 0 TO LEN(numbers) - 1
        IF numbers[i] == target THEN
            found = i
        ELSE
            found = -1
        ENDIF
    NEXT i
    RETURN found
ENDFUNCTION
(a)A student has written the following function, intended to search for a value target in an array numbers and return its index, or -1 if not found. When tested, the function sometimes returns the wrong result:
FUNCTION search(numbers, target)
    FOR i = 0 TO LEN(numbers) - 1
        IF numbers[i] == target THEN
            found = i
        ELSE
            found = -1
        ENDIF
    NEXT i
    RETURN found
ENDFUNCTION

Identify why this function does not always work correctly, using an example to support your answer.
(2)
(b)Rewrite the function so that it works correctly for every case.(3)
(Total for Question 18 is 5 marks)
19
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
(a)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
(8)
(Total for Question 19 is 8 marks)
20
A student has written the following program, which is intended to calculate and output the average of a set of numbers entered by the user. The user enters -1 to stop entering numbers. However, the program does not work correctly if a negative number (other than -1) is entered by mistake, and it does not handle the case where no valid numbers are entered at all:
total = 0
count = 0
INPUT number
WHILE number != -1
    total = total + number
    count = count + 1
    INPUT number
ENDWHILE
average = total / count
OUTPUT average
(a)A student has written the following program, which is intended to calculate and output the average of a set of numbers entered by the user. The user enters -1 to stop entering numbers. However, the program does not work correctly if a negative number (other than -1) is entered by mistake, and it does not handle the case where no valid numbers are entered at all:
total = 0
count = 0
INPUT number
WHILE number != -1
    total = total + number
    count = count + 1
    INPUT number
ENDWHILE
average = total / count
OUTPUT average

Explain one weakness of this program, other than the lack of comments, referring to what happens if the user enters -1 as their very first input.
(2)
(b)Refine the program so that: any negative number entered (not just -1) ends the input phase in the same way as -1, and the program outputs a suitable message instead of calculating an average if no valid numbers were entered.(4)
(Total for Question 20 is 6 marks)
Mark scheme · P2.7 Programming Practice: Writing and Refining Programs

Question 1

Question 2

Question 3

Question 4

Question 5

Question 6

Question 7

Question 8

Question 9

Question 10

Question 11

Question 12

Question 13

Question 14

Question 15

Question 16

Question 17

Question 18

Question 19

Question 20

Mark your answers

This checks your answers in your browser, stores nothing on a server and needs no account.

Question 1

3 marks
Did your answer earn the marks?

Question 2

3 marks
Did your answer earn the marks?

Question 3

3 marks
Did your answer earn the marks?

Question 4

4 marks
Did your answer earn the marks?

Question 5

5 marks
Did your answer earn the marks?

Question 6

4 marks
Did your answer earn the marks?

Question 7

4 marks
Did your answer earn the marks?

Question 8

3 marks
Did your answer earn the marks?

Question 9

5 marks
Did your answer earn the marks?

Question 10

5 marks
Did your answer earn the marks?

Question 11

4 marks
Did your answer earn the marks?

Question 12

5 marks
Did your answer earn the marks?

Question 13

3 marks
Did your answer earn the marks?

Question 14

5 marks
Did your answer earn the marks?

Question 15

5 marks
Did your answer earn the marks?

Question 16

6 marks
Did your answer earn the marks?

Question 17

6 marks
Did your answer earn the marks?

Question 18

5 marks
Did your answer earn the marks?

Question 19

8 marks
Did your answer earn the marks?

Question 20

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