You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
One of the most challenging question types in OCR J277 Paper 2 asks you to write a program (or section of code) based on a given specification. These questions typically carry 4-8 marks and test your ability to translate a problem description into working code.
Follow this systematic approach:
OCR Exam Tip: You can use either OCR pseudocode or Python. The mark scheme accepts both. Choose whichever you are more comfortable with, but be consistent within your answer.
The four-step approach to a program-writing question can be drawn as a workflow showing how time is allocated and where to loop back if a check fails:
flowchart TD
A[Read specification twice] --> B[Annotate inputs, processing, outputs]
B --> C[Plan variables and control structures]
C --> D[Write code with clear indentation]
D --> E[Trace with sample input]
E --> F{Output matches specification?}
F -->|No| C
F -->|Yes| G[Submit answer]
Specification: Ask the user to enter a number between 1 and 100. Keep asking until a valid number is entered.
// OCR pseudocode
do
num = int(input("Enter a number between 1 and 100: "))
until num >= 1 AND num <= 100
print("You entered: " + str(num))
# Python
num = int(input("Enter a number between 1 and 100: "))
while num < 1 or num > 100:
num = int(input("Enter a number between 1 and 100: "))
print("You entered:", num)
Specification: Ask the user to enter 10 test scores. Calculate and display the total and average.
// OCR pseudocode
total = 0
for i = 1 to 10
score = int(input("Enter score: "))
total = total + score
next i
average = total / 10
print("Total: " + str(total))
print("Average: " + str(average))
# Python
total = 0
for i in range(10):
score = int(input("Enter score: "))
total += score
average = total / 10
print("Total:", total)
print("Average:", average)
Specification: Search an array of names for a given name and report whether it was found.
// OCR pseudocode
names = ["Alice", "Bob", "Charlie", "Diana", "Eve"]
search = input("Enter name to search for: ")
found = false
for i = 0 to 4
if names[i] == search then
found = true
endif
next i
if found == true then
print("Name found")
else
print("Name not found")
endif
# Python
names = ["Alice", "Bob", "Charlie", "Diana", "Eve"]
search = input("Enter name to search for: ")
found = False
for name in names:
if name == search:
found = True
if found:
print("Name found")
else:
print("Name not found")
Specification: Find and display the largest value in an array of numbers.
// OCR pseudocode
numbers = [23, 45, 12, 67, 34]
highest = numbers[0]
for i = 1 to 4
if numbers[i] > highest then
highest = numbers[i]
endif
next i
print("The highest value is: " + str(highest))
Specification: Write a function that takes a temperature in Celsius and returns it in Fahrenheit (F = C x 9/5 + 32).
// OCR pseudocode
function celsiusToFahrenheit(celsius)
return celsius * 9 / 5 + 32
endfunction
temp = int(input("Enter temperature in Celsius: "))
result = celsiusToFahrenheit(temp)
print(str(result) + " degrees Fahrenheit")
# Python
def celsius_to_fahrenheit(celsius):
return celsius * 9 / 5 + 32
temp = int(input("Enter temperature in Celsius: "))
result = celsius_to_fahrenheit(temp)
print(f"{result} degrees Fahrenheit")
Examiners typically award marks for:
| Criterion | Marks typically awarded for |
|---|---|
| Correct use of variables | Declaring and using appropriate variables |
| Input | Reading input from the user correctly |
| Processing | Correct calculation or logic |
| Selection | Correct use of if/elseif/else |
| Iteration | Correct use of for/while/do-until |
| Output | Displaying the correct result |
| Syntax | Correct, consistent syntax throughout |
OCR Exam Tip: Even if your program is not perfect, you can earn partial marks for correct elements. Never leave a code question blank — write what you can.
| Mistake | How to avoid |
|---|---|
| Not reading input as a number | Use int(input(...)) or float(input(...)) for numbers |
| Off-by-one errors in loops | Check whether your range includes the correct start and end values |
| Forgetting closing keywords | Every if needs endif, every for needs next, every while needs endwhile |
| Using = instead of == for comparison | = assigns; == compares |
| Not initialising variables | Set counters to 0 and flags to false before use |
The best way to approach a long code-writing question is to decompose the specification into inputs, processes, and outputs before writing any code. Here is a worked walkthrough of a typical 8-mark Paper 2 task.
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.