OCR GCSE Computer Science June 2024 Paper 2, Question 9(a)(ii): completing a linear search
OCR J277/02 June 2024, Question 9(a)(ii): fill four gaps in a linear search function that returns True if a student's name is in the team array. Each gap explained, why return False goes after the loop, and the function to run.
Question 9(a)(ii) of the OCR GCSE Computer Science Paper 2 sat on 21 May 2024 (J277/02) gives you a linear search function with four gaps in it. One mark each. The function is only seven lines long, and it is the pattern to learn for any "is it in the array?" question.
We do not copy the exam paper here. Open it beside this page: OCR June 2024 J277/02 question paper (PDF). When you have finished, check the mark scheme too.
The question in short
An array called theTeam holds six student names at index 0 to 5. A function, linearSearch(studentName), returns True if the name is in the array and False if it is not.
The skeleton is a for loop from 0 to gap 1. Inside, if theTeam[ gap 2 ] == gap 3 then return gap 4. After the loop comes return False.
Work it through
Gap 1 is theTeam.length - 1 (or just 5). The loop has to reach the last index. Six items means the last index is 5. In OCR's reference language a for loop includes its end value.
Gap 2 is count. The loop variable is the index. Each pass looks at the next name.
Gap 3 is studentName. That is the parameter: the name being looked for.
Gap 4 is True. A match has been found, so the function can answer straight away. return ends the function, so the loop stops early.
The finished function
function linearSearch(studentName)
for count = 0 to theTeam.length - 1
if theTeam[count] == studentName then
return True
endif
next count
return False
endfunction
Why return False is after the loop
You only know that a name is not in the array once you have looked at every item. So return False cannot be inside the loop. If it were in an else, the function would give up after looking at the first name.
Where the marks are lost
6for gap 1. Index 6 does not exist. (The mark scheme does accept 6 ortheTeam.lengthfrom students thinking in Python'srange, but 5 is right in the reference language.)studentNamefor gap 2. The index must be a number.theTeam["Eve"]means nothing."studentName"in quotation marks. That searches for the word studentName.countfor gap 4. The question says it returns True, not the position.
Run it
The function in Python. It prints each comparison, so you can see it stop early for a name near the front. The robot's light is green if the student is in the team.
The program
from bugbot import *
connect()
theTeam = ["Ali", "Eve", "Ling", "Nina", "Sarah", "Tom"]
def linearSearch(studentName):
for count in range(0, len(theTeam)):
print(" is it", theTeam[count] + "?")
if theTeam[count] == studentName:
return True
return False
name = input("Which student? ")
result = linearSearch(name)
print(result)
if result:
led("green")
else:
led("red")
Questions
What are the answers to OCR J277 June 2024 Paper 2 Question 9(a)(ii)?
theTeam.length - 1 (or 5), then count, then studentName, then True.
How does a linear search work?
It compares the target with each item in turn, starting at the first. It stops when it finds a match, or when it reaches the end of the list without one.
Does a for loop in OCR Exam Reference Language include the last value?
Yes. for count = 0 to 5 runs six times, with count taking the values 0, 1, 2, 3, 4 and 5. In Python, range(0, 6) does the same.
More from this paper
Every OCR J277 question we have worked · Guide: Linear search and binary search explained
Learn it step by step
- F5.5 Linear search Algorithms
- F4.2 Parameters and return values Functions and structured code
This is our own explanation of a published exam question. It is not written or endorsed by OCR, and the question paper and mark scheme remain OCR's copyright. Read them on OCR's site with the links on this page.