OCR GCSE Computer Science June 2023 Paper 2, Question 6(b): the alarm program
OCR J277/02 June 2023, Question 6(b): write a program that calls SoundAlarm() when the system is armed and a door or window sensor is active. The bracket trap with AND and OR, the four marks explained, and the program to run with every combination.
Question 6(b) of the OCR GCSE Computer Science Paper 2 sat on 25 May 2023 (J277/02) is a 4 mark program in Section B, so it must be written in OCR Exam Reference Language or a high-level language such as Python. It is three lines long, and one pair of brackets decides whether it is right.
We do not copy the exam paper here. Open it beside this page: OCR June 2023 J277/02 question paper (PDF). When you have finished, check the mark scheme too.
The question in short
An intruder alarm stores three Boolean variables: SystemArmed, DoorSensorActive and WindowSensorActive.
The alarm should sound only when the system is armed and the door sensor, the window sensor or both are active. A procedure called SoundAlarm() has already been written. Your program checks the variables and calls it when it should.
A model answer
In OCR Exam Reference Language:
if SystemArmed and (DoorSensorActive or WindowSensorActive) then
SoundAlarm()
endif
In Python:
if SystemArmed and (DoorSensorActive or WindowSensorActive):
SoundAlarm()
A nested version gets full marks as well, and avoids the bracket question altogether:
if SystemArmed:
if DoorSensorActive or WindowSensorActive:
SoundAlarm()
Why the brackets matter
and is worked out before or, in the same way that multiplying comes before adding. So without brackets,
SystemArmed and DoorSensorActive or WindowSensorActive
means (SystemArmed and DoorSensorActive) or WindowSensorActive. An open window would set the alarm off even when the system is switched off. The mark scheme points this out by name: it is not logically valid.
Where the four marks are
- using selection (or a condition-controlled loop);
- checking whether the system is armed;
- checking the door sensor or the window sensor, both of them;
- calling
SoundAlarm()correctly, so that it sounds in every case it should and no case it should not.
You can test a Boolean directly (if SystemArmed) or compare it (if SystemArmed == True). Both are accepted.
Where the marks are lost
- No brackets. See above. The last mark needs the alarm to be right in every situation.
andbetween the sensors. Then a burglar has to open a door and a window at once.SoundAlarmwith no brackets. A call needs().- Writing the
SoundAlarmprocedure. It is pre-written. Just call it. - Asking for input. The variables already hold the data. The mark scheme ignores inputs, but they waste your time.
Run it
This program tries all eight combinations of the three variables with your condition, and prints when the alarm sounds. BRACKETS = False shows the broken version. Look for the rows where the system is not armed.
The program
from bugbot import *
connect()
# False leaves the brackets out
BRACKETS = True
def SoundAlarm():
led("red")
print(" ALARM")
count = 0
for SystemArmed in [False, True]:
for DoorSensorActive in [False, True]:
for WindowSensorActive in [False, True]:
print("armed", SystemArmed, " door", DoorSensorActive, " window", WindowSensorActive)
if BRACKETS:
sound = SystemArmed and (DoorSensorActive or WindowSensorActive)
else:
sound = SystemArmed and DoorSensorActive or WindowSensorActive
if sound:
SoundAlarm()
count = count + 1
print("The alarm sounded in", count, "of 8 cases")
Questions
What is the answer to OCR J277 June 2023 Paper 2 Question 6(b)?
if SystemArmed and (DoorSensorActive or WindowSensorActive) then call SoundAlarm(). The brackets round the or are needed, or use a nested if.
Which is worked out first, AND or OR?
AND is worked out before OR, unless brackets say otherwise. So a and b or c means (a and b) or c. NOT comes before both.
How do you call a procedure in OCR Exam Reference Language?
Write its name followed by brackets, with any arguments inside them, such as SoundAlarm() or ResetSensor(sensorID).
More from this paper
- Question 5(c): An adding game: three random questions and a score 6 marks
- Question 6(e): A procedure with two parameters that writes to a text file 6 marks
- Question 6(f): Casting, and totalling a column of a 2D array for one date 7 marks
Every OCR J277 question we have worked · Guide: Logic gates and truth tables explained
Learn it step by step
- F2.3 else, elif and Boolean operators Decisions and loops
- F2.4 Nested selection and match Decisions and loops
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.