Level:
Correct: 0
Incorrect: 0
Marks: 0/0
Exam Practice
Course:
Topics:
OCR GCSE Computer Science (Paper 2) Smart practice A mixed round aimed at your weak spots
Unit 1
Algorithms
Unit 2
Programming fundamentals
Unit 3
Producing robust programs
Unit 4
Boolean logic
Unit 5
Programming languages and IDEs
Unit 6
Practical programming (Section B)
Avg
score:
–
Topics
done:
0/31

Testing and refining programs

Practise 27 exam-style questions on Testing and refining programs for OCR GCSE Computer Science (Paper 2). Write your answer as you would in the exam and get it marked in seconds against the mark scheme, point by point, with the Study Helper on hand if you get stuck.

  • 27 questions
  • 2–7 marks each
  • Instant marking
  • No sign-up needed
20 questions, marked as you go

Sample questions with model answers

Here are 4 of the questions in this topic, each with a model answer, an explanation of why it earns the marks and the mark scheme an examiner would use.

Question 1

3 marks

This program should count down from a number entered by the user and then output Lift off!

01countdown = int(input("Start countdown from: "))
02while countdown > 0
03    print(countdown)
04endwhile
05print("Lift off!")

When the user enters 5, the program outputs 5 over and over again and never stops.

Explain why, and state the line of code that must be added and where it must go.

Model answer

The value of countdown is never changed inside the loop, so countdown > 0 stays true for ever and the loop never ends.

Add this line inside the loop, after print(countdown), i.e. between lines 03 and 04:

    countdown = countdown - 1

Explanation

A condition-controlled loop only stops when its condition becomes false, so something inside the loop must change the variable being tested. Taking 1 off countdown on each pass makes it reach 0, and then the loop ends and Lift off! is output. Putting the line after the print gives 5, 4, 3, 2, 1; putting it before the print would skip 5 and output 0. Testing with a small value such as 5 quickly shows this kind of logic error.

Mark scheme
  1. 1 mark

    countdown never changes inside the loop, so the condition countdown > 0 is always true (an infinite loop)

    the idea that the value is never updated, so the loop cannot end.

  2. 1 mark

    add the line countdown = countdown - 1

    accept countdown -= 1 or countdown = countdown - 1 in any form.

  3. 1 mark

    it goes inside the loop, after the print: between lines 03 and 04

    accept "after line 03" or "before endwhile". Do not accept before the print (between lines 02 and 03): the program would then output 4, 3, 2, 1, 0 instead of 5, 4, 3, 2, 1.

Question 2

4 marks

This quiz program asks two questions and outputs the player's score:

name = input("Enter your name: ")
score = 0
answer = input("What is the capital of France? ")
if answer.lower == "paris" then
    score = score + 1
endif
answer = input("How many sides does a hexagon have? ")
if answer == "6" then
    score = score + 1
endif
print(name + " scored " + str(score))

The program must be extended so that, at the end, it writes the player's name and score on one line, separated by a comma (for example Sam,2), at the end of an existing text file called results.txt.

Write the extra code needed, using OCR Exam Reference Language or a high-level programming language.

Model answer

resultsFile = open("results.txt")
resultsFile.writeLine(name + "," + str(score))
resultsFile.close()

Explanation

The file already exists, so it is opened with open() rather than created with newFile(), which would start an empty file and lose the earlier results. In OCR Exam Reference Language, writeLine() adds the line at the end of the file. The name, a comma and the score (cast to a string) are joined into one string so they go on the same line. Closing the file makes sure the data is saved.

Mark scheme
  1. 1 mark

    opens the file results.txt

    Answers must be in OCR Exam Reference Language or a high-level programming language (e.g. Python); pseudocode, natural English or bullet points score 0. Ignore minor syntax errors. e.g. resultsFile = open("results.txt"). Accept Python open("results.txt", "a") (append). Do not accept newFile("results.txt") or Python mode "w", which would replace the results already in the file.

  2. 1 mark

    writes a line to the file

    e.g. resultsFile.writeLine(...); accept Python write() with a new line character added.

  3. 1 mark

    the line written is the name, a comma and the score joined together

    e.g. name + "," + str(score). The comma must be included and both values on one line. Treat a missing str() as a minor error.

  4. 1 mark

    closes the file

    e.g. resultsFile.close(). Accept a Python with block.

Question 3

4 marks

A programmer has written this code to add up the prices of some items in pence and output the total in pounds. The code works, but it is hard to maintain.

a = 0
b = int(input("How many? "))
for c = 1 to b
d = int(input("Price in pence: "))
a = a + d
next c
print(a / 100)

Describe two ways the code could be improved to make it easier to maintain. For each way, refer to this code.

Model answer

1. Use meaningful variable names: for example, rename a to totalPence, b to numberOfItems and d to price, so it is clear what each variable stores.
2. Indent the code inside the loop: the two lines between for c = 1 to b and next c should be indented, so it is clear which lines are repeated.

Explanation

Maintainable code is easy for another programmer (or you, later) to read, understand and change. Single-letter names such as a and d hide what the data is; descriptive names explain it. Indentation shows the structure, here which lines are inside the loop. Comments can explain less obvious steps, such as dividing by 100 to convert pence to pounds, and a named constant could replace the bare number 100.

Mark scheme
  1. 1 mark

    first way: a valid maintainability technique, e.g. use meaningful variable names / indent the code inside the loop / add comments / use a constant / use a sub program

    the answer may be in natural English. Do not accept changes that do not affect maintainability, e.g. making the code run faster or adding validation.

  2. 1 mark

    applies the first way to this code

    e.g. names: rename a to totalPence, b to numItems, d to price; indentation: indent the two lines between for and next; comments: explain that dividing by 100 converts pence to pounds, or what the loop does; constant: const PENCE_IN_POUND = 100; sub program: a function that converts pence to pounds.

  3. 1 mark

    second way: a different valid maintainability technique

    must be different from the first way. The same technique applied to different lines is not a second way.

  4. 1 mark

    applies the second way to this code

    as for the first application.

Question 4

6 marks

A game awards badges: bronze for scores from 50 to 79, silver for 80 to 99 and gold for 100 or more. Scores below 50 get no badge.

01score = int(input("Enter score: "))
02if score > 100 then
03    print("Gold")
04elseif score > 80 then
05    print("Silver")
06elseif score >= 50 then
07    print("Bronze")
08else
09    print("No badge")
10endif

The code contains two logic errors. For each error:
- give the line number
- give a test value that would show the error
- write the corrected line.

Model answer

Line 02: test value 100 outputs Silver instead of Gold. Corrected line: if score >= 100 then
Line 04: test value 80 outputs Bronze instead of Silver. Corrected line: elseif score >= 80 then

Explanation

Both errors are at boundaries: "100 or more" includes 100, and "80 to 99" includes 80, but > leaves those values out. Boundary test data finds this kind of error: a score of 100 falls through to the silver test and a score of 80 falls through to the bronze test. Values such as 101 or 85 would not reveal the errors, because they give the right badge. Changing > to >= (or using > 99 and > 79) fixes both lines.

Mark scheme
  1. 1 mark

    first error: line 02

    accept line 2.

  2. 1 mark

    test value 100

    100 should give Gold but the code outputs Silver. 100 is the only value that shows this error.

  3. 1 mark

    corrected line 02: if score >= 100 then

    accept if score > 99 then.

  4. 1 mark

    second error: line 04

    accept line 4.

  5. 1 mark

    test value 80

    80 should give Silver but the code outputs Bronze. 80 is the only value that shows this error.

  6. 1 mark

    corrected line 04: elseif score >= 80 then

    accept elseif score > 79 then.

Ready to try the rest? Every answer you write is marked against the mark scheme, so you can see exactly where the marks are.

Study Helper Hints first, answers after you try

Stuck on this one? Ask for a hint and I’ll point you in the right direction, without giving the answer away. Once you’ve checked your answer, I’ll show you how the marks are awarded.