Question bank › GCSE › GCSE Computer Science › Programming fundamentals

Sub programs and random numbers

10 questions43 marksabout 50 min

Answer on paper first, then reveal the mark scheme one mark at a time.

1Grade 6–75 marks

Look at the subprogram below.

function calc(a, b)
    if a > b then
        return a - b
    else
        return a * b
    endif
endfunction

(a) State the output of: (i) print(calc(9, 4)) [1] (ii) print(calc(3, 5)) [1]

(b) Identify one parameter in the subprogram. [1]

(c) Give one difference between a function and a procedure. [1]

(d) Give one benefit of using local variables in subprograms. [1]

2Grade 1–34 marks

A board game program uses the OCR Exam Reference Language line below to simulate rolling a dice.

dice = random(1,6)

(a) State the smallest and the largest value that can be stored in dice. [1]

(b) A bonus card is simulated using the line below.

bonus = random(1,6) * 2 + 1

State the smallest and the largest value that can be stored in bonus. [2]

(c) Give one use of random numbers in a program other than games. [1]

3Grade 1–33 marks

A program contains the function below, written in OCR Exam Reference Language.

function area(w, h)
    result = w * h
    return result
endfunction

size = area(4, 5)
print(size)

(a) Identify the two parameters of the function. [1]

(b) Write out the line of code that calls the function. [1]

(c) State the value that is output. [1]

Stuck on Sub programs and random numbers?

Get it sorted in a free 30-minute session

We work through your weakest questions on this topic on Zoom. I reply the same day.

4Grade 4–54 marks

A developer is writing a large program to run a school library. The developer decides to split the program into sub programs (functions and procedures).

Explain two advantages of writing the program using sub programs. [4]

5Grade 4–54 marks

A program is written in OCR Exam Reference Language.

global limit = 20

function check(value)
    extra = 5
    return value + extra
endfunction

print(check(limit))
print(extra)

(a) State the value output by the line print(check(limit)). [1]

(b) Explain why the line print(extra) causes an error. [2]

(c) State the scope of the variable limit. [1]

6Grade 4–54 marks

A program simulates tossing a coin 100 times and counts how many times it lands on heads.

Write a program in OCR Exam Reference Language or a high-level programming language that:

  • repeats 100 times
  • each time generates a random number to represent the toss, where 1 means heads
  • counts the number of heads
  • outputs the total number of heads at the end.

[4]

7Grade 6–74 marks

A function is written in OCR Exam Reference Language. It is passed an array and the number of items in the array.

function highest(nums, size)
    big = nums[0]
    for i = 1 to size - 1
        if nums[i] > big then
            big = nums[i]
        endif
    next i
    return big
endfunction

array scores = [12, 7, 19, 3, 15]
print(highest(scores, 5))

(a) State the value output by the program. [1]

(b) The line if nums[i] > big then is changed to if nums[i] < big then. State the new output. [1]

(c) Explain one advantage of passing the array into the function as a parameter rather than the function using a global array. [2]

8Grade 6–75 marks

A teacher stores students' test marks in an array called marks. The first n items of the array hold valid marks.

Write a function called average in OCR Exam Reference Language or a high-level programming language. The function must:

  • take the array marks and the number of marks n as parameters
  • add up the first n marks
  • return the mean of the marks.

You do not need to write any code to call the function. [5]

9Grade 6–74 marks

A programmer writes the following in OCR Exam Reference Language.

procedure double(n)
    print(n * 2)
endprocedure

answer = double(6)
print(answer + 1)

(a) Explain why this program does not work as the programmer intended. [2]

(b) Rewrite double as a function in OCR Exam Reference Language so that print(answer + 1) outputs 13. [2]

10Grade 8–96 marks

A lottery program needs to pick 6 different whole numbers, each between 1 and 59 inclusive.

Write a function called lottery in OCR Exam Reference Language or a high-level programming language. The function must:

  • create an array that can hold 6 numbers
  • generate random numbers between 1 and 59 inclusive
  • only add a number to the array if it is not already in the array
  • stop when the array holds 6 numbers
  • return the array.

[6]

Stuck on this topic?

Book a free 30-minute session on WhatsApp and we'll find exactly which step is costing you the marks. Or build a timed paper on it.

Free 30-minute session