OCR GCSE Computer Science sample Paper 2, Question 2: complete a comparison, write a loop
OCR J277/02 sample assessment material, Question 2: fill five gaps in pseudocode that outputs the larger of two numbers, then write an algorithm that doubles inputs until a negative number is entered. Worked through, with both programs to run.
Question 2 of OCR's sample assessment material for GCSE Computer Science Paper 2 (J277/02, Computational thinking, algorithms and programming) is two 5 mark parts: fill the gaps in a comparison (a), and write a short loop from scratch (b). The sample paper is the one OCR published to show what the real papers look like, and every school has it.
We do not copy the paper here. Open it beside this page: OCR J277/02 sample question paper and mark scheme (PDF). The mark scheme is in the same document.
Part (a): output the larger of two numbers
The program inputs two numbers and outputs the larger. The skeleton gives you num2 = input("enter second number") and an else, with five gaps.
num1 = input("enter first number")
num2 = input("enter second number")
if num1 > num2 then
print(num1)
else
print(num2)
endif
The five gaps: the first input, the word if, num2 after the >, print(num1), and print(num2). One mark each. Variables must not be in quotation marks: print("num1") prints the word.
Part (b): double until a negative number
Input a number, double it and print the result, and repeat until the user enters a number less than 0. Any form of algorithm is accepted here, including plain English.
number = 0
while number >= 0
number = input("enter a number")
print(number * 2)
endwhile
The five marks: a condition-controlled loop; a condition that checks for 0 or more; the input inside the loop; multiplying by 2; outputting the result. The mark scheme ignores whether number is given a value before the loop.
Where the marks are lost
"num1"in quotation marks in part (a). The mark scheme says so directly.- A
forloop in part (b). The number of repeats is unknown, so it is awhile. while number > 0. The rule is "less than 0" stops it, so 0 itself carries on. The check is>= 0.- Input before the loop only. The same number would be doubled for ever.
Run it
Both parts. The robot drives the larger number in centimetres, then doubles what you type until you enter a negative number.
The program
from bugbot import *
connect()
num1 = int(input("enter first number: "))
num2 = int(input("enter second number: "))
if num1 > num2:
print(num1, "is bigger")
forward(60, distance=min(num1, 60))
else:
print(num2, "is bigger")
forward(60, distance=min(num2, 60))
number = 0
while number >= 0:
number = int(input("enter a number (negative to stop): "))
print(number * 2)
Questions
What is the answer to OCR J277 sample Paper 2 Question 2(a)?
num1 = input("enter first number"), then if num1 > num2 then print(num1) else print(num2) endif.
What is the answer to Question 2(b)?
A while loop that runs while the number is 0 or more. Inside it, input a number and print the number times 2.
Can I write an algorithm in English on OCR Paper 2?
In Section A, yes: pseudocode, OCR Exam Reference Language, plain structured English or a flowchart all count. In Section B, questions that say so must be in OCR Exam Reference Language or a real programming language.
More from this paper
- Question 4: Usernames from a flowchart, then an algorithm for teachers and students 8 marks
- Question 8(b) to (d): A range check with OR and its tests, index a 2D array, refine repeated lines into a loop 10 marks
Every OCR J277 question we have worked
Learn it step by step
- F2.2 Selection: if Decisions and loops
- F2.6 Condition-controlled loops: while 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.