Practise Paper 2 Section B practice: design, write, test and refine. 13 exam-style questions on this subtopic, at up to four difficulty levels, with full mark schemes and a progress tracker. Free, no account needed.
Written for this site in the style of OCR exam questions. They are not taken from real past papers.
Question 1Easy13 marks
TempCheck is a simple program that will ask a nurse to enter a patient's temperature, in degrees Celsius to one decimal place, and tell them whether it is Normal (36.1 to 37.2 inclusive), a Fever (above 37.2) or Too low (below 36.1).
(a) Complete {table} to show the input, the process and the output of this program.[3]
[object Object]
(b) Write a program that asks the nurse to enter a temperature and outputs Normal, Fever or Too low as appropriate. You must use either OCR Exam Reference Language or a high-level programming language that you have studied.[5]
(c) Complete the test plan in {table2} with three tests for this program: one that should give each of the three possible outputs.[3]
[object Object]
(d) State one boundary value that should also be tested, and its expected output.[2]
Show the answer and mark scheme
(a)
Input: the patient's temperature
Process: compare the temperature with 36.1 and 37.2 to decide which category it is in
Output: Normal, Fever or Too low
(b)Answer:
temp = float(input("Enter temperature"))
if temp < 36.1 then
print("Too low")
elseif temp > 37.2 then
print("Fever")
else
print("Normal")
endif
inputs the temperature and converts it to a real number (e.g. float(input(…)))
outputs Too low when the temperature is below 36.1
outputs Fever when the temperature is above 37.2
otherwise outputs Normal
the selection is structured so that every temperature gets exactly one output, including 36.1 and 37.2 giving Normal
(c)
a value in the normal range, e.g. 36.8, expected output Normal
a value above the range, e.g. 38.0, expected output Fever
a value below the range, e.g. 35.0, expected output Too low
(d)
a boundary value, e.g. 36.1 or 37.2 (allow 36.0 or 37.3, just outside the range)
the correct expected output for the value given: Normal for 36.1 or 37.2 (they are included in the normal range), Too low for 36.0, Fever for 37.3
Question 2Medium23 marks
Pixel Quest is a quiz game with 10 questions, each worth 10 points. The game keeps a running score and awards a bonus for a perfect round. Later, the questions are given three difficulty levels worth different numbers of points.
(a) {fig} shows part of a structure diagram for Pixel Quest. Complete the structure diagram by adding two suitable sub-tasks below Update score and two suitable sub-tasks below Show final score.[4]
[object Object]
(b) Write a program that:
for each of the 10 questions, asks the user to enter yes if the question was answered correctly or no if it was not
keeps a running score, adding 10 points for each correct answer
adds a bonus of 20 points if all 10 questions were answered correctly
outputs the final score.
You must use either OCR Exam Reference Language or a high-level programming language that you have studied.[7]
(c) Look at this simplified version of the program, used for a 4-question round.
01 score = 0
02 for q = 1 to 4
03 answer = input("Correct?")
04 if answer == "yes" then
05 score = score + 10
06 endif
07 next q
08 print(str(score))
Complete the trace table in {table} for the inputs yes, no, yes, yes, in that order.[5]
[object Object]
(d) The game changes so that each question is EASY (worth 5 points), MEDIUM (worth 10 points) or HARD (worth 20 points). Refine the program by writing a function called pointsFor that takes the difficulty as a parameter and returns the number of points it is worth, using a switch/case statement. Any difficulty other than "EASY", "MEDIUM" or "HARD" should return 0. You must use either OCR Exam Reference Language or a high-level programming language that you have studied.[5]
(e) Give one value of difficulty that would test the default branch of the function in part (d), and state the value the function should return.[2]
Show the answer and mark scheme
(a)Answer: Below Update score: e.g. Check answer is correct; Add 10 points. Below Show final score: e.g. Check for perfect round; Add 20-point bonus (or Display total score).
a sensible sub-task below Update score, e.g. check whether the answer is correct
a second, different sub-task below Update score, e.g. add 10 points to the score
a sensible sub-task below Show final score, e.g. check whether all 10 answers were correct
a second, different sub-task below Show final score, e.g. add the 20-point bonus / display the final score
(b)Answer:
score = 0
for q = 1 to 10
answer = input("Correct? (yes/no)")
if answer == "yes" then
score = score + 10
endif
next q
if score == 100 then
score = score + 20
endif
print(str(score))
sets the score to 0 before the loop
uses a count-controlled loop that repeats 10 times
inputs yes or no inside the loop
adds 10 to the score when the input is yes
after the loop, checks whether all 10 answers were correct (e.g. the score is 100, or a count of correct answers is 10)
adds 20 to the score only when that check is true
outputs the final score after the bonus check
(c)Answer:
q
answer
score
Output
0
1
yes
10
2
no
10
3
yes
20
4
yes
30
30
q takes the values 1, 2, 3, 4 in order
answer takes the values yes, no, yes, yes in order
score is 10 when q = 1 and is still 10 when q = 2
score is 20 when q = 3 and 30 when q = 4
30 is output once, after the loop has finished
(d)Answer:
function pointsFor(difficulty)
switch difficulty:
case "EASY":
points = 5
case "MEDIUM":
points = 10
case "HARD":
points = 20
default:
points = 0
endswitch
return points
endfunction
defines a function pointsFor that takes the difficulty as a parameter
uses a switch/case statement on the difficulty
EASY gives 5, MEDIUM gives 10 and HARD gives 20
a default case gives 0 for any other value
returns the number of points (from each case, or once after the end of the switch)
(e)Answer: Any value other than "EASY", "MEDIUM" or "HARD", e.g. "BONUS"; it should return 0.
any value other than "EASY", "MEDIUM" or "HARD", e.g. "BONUS", "hard" (the comparison is case-sensitive) or an empty string
0
Question 3Hard24 marks
Riverside Runners athletics club records the finishing time, in seconds, of every runner in its weekly 5 km time trial. A program is needed that inputs the number of runners, then each runner's name and time, checks that each time is valid, and reports the fastest runner.
(a) (i) Complete {table} to show the inputs, the processes and the output of the program. (ii) A time is only valid if it is a whole number of seconds from 600 to 3600 inclusive. Identify one validation check that should be carried out on each time, and describe what it would check.[5]
[object Object]
(b) Write a program that:
asks how many runners took part
uses arrays to store each runner's name and time
validates each time as it is entered, so that only whole numbers from 600 to 3600 are accepted, asking again whenever a time is invalid
after all the data has been entered, outputs the name and time of the fastest (lowest time) runner.
You must use either OCR Exam Reference Language or a high-level programming language that you have studied.[8]
(c) Complete the test plan in {table2} for the validation of each time in part (b). For each test, give suitable test data and the expected result.[5]
[object Object]
(d) Refine the program in part (b) by writing the search for the fastest time as a function called fastestIndex. The function should take the array of times and the number of runners as parameters, and return the index of the fastest time. Show how the main program calls the function to output the fastest runner's name and time. You must use either OCR Exam Reference Language or a high-level programming language that you have studied.[4]
(e) Explain one benefit of writing the search for the fastest time as a function, as in part (d).[2]
Show the answer and mark scheme
(a)Answer: (i) Inputs: the number of runners; each runner's name and time. Processes: validate each time; find the lowest time. Output: the name and time of the fastest runner. (ii) Range check: the time is from 600 to 3600 inclusive (or type check: the time is a whole number).
Inputs: the number of runners, and each runner's name and time
Processes: any one of: check that each time is valid (a whole number from 600 to 3600); store the names and times; compare the times to find the lowest (fastest)
Output: the name (and time) of the fastest runner
(ii) range check (allow type check or presence check)
a description that matches the check named: range check, the time is from 600 to 3600 inclusive / type check, the time is a whole number (integer) / presence check, a time has been entered
(b)Answer:
count = int(input("Enter number of runners"))
array names[count]
array times[count]
for i = 0 to count - 1
names[i] = input("Enter name")
valid = false
do
t = int(input("Enter race time in seconds"))
if t >= 600 AND t <= 3600 then
valid = true
else
print("Invalid time")
endif
until valid == true
times[i] = t
next i
fastest = 0
for i = 1 to count - 1
if times[i] < times[fastest] then
fastest = i
endif
next i
print(names[fastest])
print(str(times[fastest]))
inputs the number of runners and declares arrays (or equivalent) for the names and the times
uses a count-controlled loop that runs once for each runner
inputs each runner's name and stores it, with that runner's time, at the same index in the arrays
inputs the time inside a condition-controlled loop (e.g. do … until) that repeats until a valid time is entered
the validation condition accepts only whole numbers from 600 to 3600 inclusive
compares each stored time with the lowest time found so far
records the index (or name) of the runner with the lowest time, updating it whenever a lower time is found
outputs the name and the time of the fastest runner once every runner has been entered
(c)Answer:
Type of test
Test data
Expected result
Normal
1500
Accepted
Boundary
600
Accepted
Boundary
3600
Accepted
Invalid
4000
Rejected: the time is asked for again
Erroneous
ten
Rejected: the time is asked for again
normal: any whole number from 601 to 3599, e.g. 1500, accepted
boundary: 600, accepted
boundary: 3600, accepted
invalid: a whole number below 600 or above 3600, e.g. 599 or 4000, rejected (the time is asked for again)
erroneous: data of the wrong type, e.g. "ten" or 1500.5, rejected (the time is asked for again)
(d)Answer:
function fastestIndex(times, n)
fastest = 0
for i = 1 to n - 1
if times[i] < times[fastest] then
fastest = i
endif
next i
return fastest
endfunction
f = fastestIndex(times, count)
print(names[f])
print(str(times[f]))
defines a function fastestIndex with two parameters: the array of times and the number of runners
loops through the times, comparing each with the fastest found so far and updating the index when a lower time is found
returns the index of the fastest time (after the loop)
the main program calls fastestIndex with the times and the number of runners, and uses the returned index to output the fastest runner's name and time
(e)
identifies a benefit: the function can be reused (called again) / it can be tested on its own / the main program is shorter and easier to follow
explains it in this context, e.g. the club could call it again to find the fastest under-18 runner without rewriting the search / it can be tested with a small set of chosen times, separately from the input and validation code
Stuck? Get 1-to-1 help. Chhetri Academy tutors GCSE and A level Maths and Science online, with a free 30-minute trial lesson.