Level:
Correct: 0
Incorrect: 0
Marks: 0/0
Exam Practice
Course:
Topics:
OCR GCSE Computer Science (Paper 2) Smart practice A mixed round aimed at your weak spots
Unit 1
Algorithms
Unit 2
Programming fundamentals
Unit 3
Producing robust programs
Unit 4
Boolean logic
Unit 5
Programming languages and IDEs
Unit 6
Practical programming (Section B)
Avg
score:
–
Topics
done:
0/31

Sub programs: functions and procedures

Practise 34 exam-style questions on Sub programs: functions and procedures for OCR GCSE Computer Science (Paper 2). Write your answer as you would in the exam and get it marked in seconds against the mark scheme, point by point, with the Study Helper on hand if you get stuck.

  • 34 questions
  • 1–8 marks each
  • Instant marking
  • No sign-up needed
20 questions, marked as you go

Sample questions with model answers

Here are 4 of the questions in this topic, each with a model answer, an explanation of why it earns the marks and the mark scheme an examiner would use.

Question 1

2 marks

A programmer is writing a large program for a school's online quiz.

Give two benefits of using sub programs when writing this program.

Model answer

A sub program, such as one that checks an answer, can be called every time it is needed instead of writing the same code again. Each sub program can also be tested on its own, which makes errors easier to find.

Explanation

Sub programs produce structured code: the program is split into named parts that each do one job. This avoids repeated code, lets each part be tested and fixed separately, lets a team share the work and means a change only has to be made in one place. Vague answers such as 'it is easier' or 'it saves time' do not score unless they say what is easier and why.

Mark scheme
  1. 1 mark

    code can be reused / called many times without being written out again

    1 mark for each different benefit, max 2. Accept 'less duplicated code / the program is shorter' as this benefit. Do not accept 'saves time' or 'easier' on their own.

  2. 1 mark

    each sub program can be tested / debugged on its own

  3. 1 mark

    easier to maintain / update: a change only needs to be made once, in the sub program

  4. 1 mark

    different programmers can work on different sub programs at the same time

  5. 1 mark

    the program is easier to read / understand because it is split into named, manageable parts

    Accept 'gives the program a clear structure'.

  6. 1 mark

    sub programs can be saved in a library and used in other programs

Question 2

2 marks

A sports league makes a code for each team.

function makeCode(team, town, year)
    part1 = team.left(3)
    part2 = town.right(2)
    code = part1.upper + part2.upper + str(year MOD 100)
    return code
endfunction

print(makeCode("Falcons", "Leeds", 2009))
print(makeCode("Otters", "Bath", 2011))

State the two lines that are output.

Model answer

FALDS9
OTTTH11

Explanation

left(3) takes the first three characters of the team ("Fal", "Ott") and right(2) takes the last two characters of the town ("ds", "th"); both are converted to upper case. year MOD 100 is the remainder after dividing by 100: 2009 MOD 100 = 9 and 2011 MOD 100 = 11. Because 9 is an integer, str(9) is "9", not "09" - a common trap.

Mark scheme
  1. 1 mark

    FALDS9

    Do not accept FALDS09: 2009 MOD 100 is the integer 9, so str gives "9". Must be upper case.

  2. 1 mark

    OTTTH11

    Must be upper case.

Question 3

3 marks

Here is a program.

function makeList(start, gap)
    array result[5]
    for i = 0 to 4
        result[i] = start + i * gap
    next i
    return result
endfunction

values = makeList(3, 4)
print(values[2])
print(values[4] - values[0])

State the contents of the array returned by makeList(3, 4), and the two values that are output.

Model answer

The array returned is [3, 7, 11, 15, 19].

Outputs:
11
16

Explanation

Each element is start + i × gap, with multiplication done before addition: 3 + 0 × 4 = 3, then 7, 11, 15 and 19. The whole array is returned and stored in values. values[2] is the third element, 11, because arrays are indexed from 0, and values[4] - values[0] = 19 - 3 = 16. A common slip is to treat values[2] as the second element (7).

Mark scheme
  1. 1 mark

    [3, 7, 11, 15, 19]

    Accept the five values in order in any clear form.

  2. 1 mark

    11 (first output)

    ecf: the value at index 2 of the student's array.

  3. 1 mark

    16 (second output)

    ecf: the student's last element minus their first element.

Question 4

5 marks

A student measures the heights of five seedlings in centimetres, as real numbers, and stores them in an array.

Write a function called average that takes an array of five real numbers as a parameter and returns the mean of the five values.

Write the function using OCR Exam Reference Language or a high-level programming language.

Model answer

function average(heights)
    total = 0
    for i = 0 to 4
        total = total + heights[i]
    next i
    return total / 5
endfunction

Explanation

The function adds the five elements to a running total and divides by 5 only once, after the loop has finished. Dividing inside the loop, or forgetting to set the total to 0 first, gives a wrong result. The / operator gives a real result, which suits measurements such as 4.6 cm. Returning the mean rather than printing it lets the calling program use the value, for example to compare two groups of seedlings.

Mark scheme
  1. 1 mark

    function defined with the name average and one (array) parameter

    Answers must be in OCR Exam Reference Language or a high-level programming language (e.g. Python); pseudocode, natural English or bullet points score 0. Ignore minor syntax errors.

  2. 1 mark

    sets a total to 0 before the loop

  3. 1 mark

    loops through all five elements (index 0 to 4)

    Accept adding the five elements in one statement, e.g. total = h[0] + h[1] + h[2] + h[3] + h[4], for this mark and the next.

  4. 1 mark

    adds each element to the total

  5. 1 mark

    returns the total divided by 5, after the loop

    Accept dividing by the length of the array. Do not award if the division is inside the loop or the mean is only output.

Ready to try the rest? Every answer you write is marked against the mark scheme, so you can see exactly where the marks are.

Study Helper Hints first, answers after you try

Stuck on this one? Ask for a hint and I’ll point you in the right direction, without giving the answer away. Once you’ve checked your answer, I’ll show you how the marks are awarded.