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

Writing programs for a scenario

Practise 35 exam-style questions on Writing programs for a scenario 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.

  • 35 questions
  • 2–8 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

4 marks

A cinema sells adult tickets for £9.50 each and child tickets for £6.00 each.

Write a program, using OCR Exam Reference Language or a high-level programming language, that:
- asks the user to enter the number of adult tickets
- asks the user to enter the number of child tickets
- calculates the total cost of the tickets
- outputs the total cost.

Model answer

adults = int(input("Number of adult tickets: "))
children = int(input("Number of child tickets: "))
total = adults * 9.50 + children * 6.00
print("Total cost: £" + str(total))

Explanation

input always returns a string, so both values are cast with int() before any arithmetic. Multiplication is done before addition, so adults * 9.50 + children * 6.00 needs no brackets. Adding the numbers of tickets first and multiplying by one price is wrong because the two kinds of ticket cost different amounts. For 2 adults and 1 child the program outputs 25 (2 × 9.50 + 6.00).

Mark scheme
  1. 1 mark

    inputs the number of adult tickets and the number of child tickets (two separate inputs)

    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. Accept any variable names and any (or no) prompt text. Both inputs are needed for this mark.

  2. 1 mark

    converts (casts) both inputs to numbers, e.g. adults = int(input(...))

    accept int(), float() or real(). Do not award if either input is left as a string.

  3. 1 mark

    calculates the total correctly: adults × 9.50 + children × 6.00

    e.g. total = adults * 9.50 + children * 6. Accept 9.5 and 6, any order, one step or several (e.g. adultCost and childCost added together). Do not accept (adults + children) * 9.50 or other calculations that give a wrong total. Award this mark even if the inputs were not cast - missing casting is penalised only at mark point 2.

  4. 1 mark

    outputs the calculated total

    e.g. print(total), print("Total cost: £" + str(total)) or print("Total:", total). Accept printing the calculation directly. The value output must be the calculated total. Treat a missing str() in the output message as a minor error. No currency formatting is needed. Award this mark even if the inputs were not cast - missing casting is penalised only at mark point 2.

Question 2

5 marks

A quiz program asks the user how many questions they would like to answer. The number must be a whole number from 5 to 20 inclusive.

Write a program, using OCR Exam Reference Language or a high-level programming language, that:
- asks the user to enter the number of questions
- keeps asking until a number from 5 to 20 is entered
- then outputs a message such as "You will answer 12 questions", showing the number entered.

Model answer

do
    questions = int(input("How many questions (5 to 20)? "))
until questions >= 5 AND questions <= 20
print("You will answer " + str(questions) + " questions")

Explanation

A validation loop must keep asking for as long as the input is invalid, so it needs a loop, not a single if. A do ... until loop asks at least once and stops when the number is in range; a while loop needs one input before the loop and another inside it. The condition must include both 5 and 20: use >= and <= for the valid range, or < 5 OR > 20 for the invalid range. Using AND for the invalid test (num < 5 AND num > 20) is a common error, because no number is both.

Mark scheme
  1. 1 mark

    inputs the number and converts it to an integer

    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.

  2. 1 mark

    uses a loop that repeats the input while the number is invalid (or until it is valid)

    e.g. while ... endwhile or do ... until. A single if statement that asks only once more does not score.

  3. 1 mark

    uses a correct condition so that only 5 to 20 inclusive is accepted

    e.g. while num < 5 OR num > 20; until num >= 5 AND num <= 20; while NOT (num >= 5 AND num <= 20). Accept num < 5 OR num > 20 written as num <= 4 OR num >= 21. Do not accept num <= 5 OR num >= 20 (rejects 5 and 20) or num < 5 AND num > 20 (never true). Award this mark even if the input was not cast - missing casting is penalised only at mark point 1.

  4. 1 mark

    inputs the number again inside the loop

    so an invalid number can be corrected. A do ... until loop with the input inside it meets this point.

  5. 1 mark

    outputs the message with the valid number after the loop

    e.g. print("You will answer " + str(num) + " questions"). The message must come after the loop, so it shows the accepted number. Treat a missing str() as a minor error.

Question 3

5 marks

A word game gives bonus points for vowels.

Write a program, using OCR Exam Reference Language or a high-level programming language, that inputs a word and outputs the number of vowels (a, e, i, o, u) in it. The word may contain both upper-case and lower-case letters, and both must be counted.

Model answer

word = input("Enter a word: ")
word = word.lower
vowels = 0
for i = 0 to word.length - 1
    letter = word.substring(i, 1)
    if letter == "a" OR letter == "e" OR letter == "i" OR letter == "o" OR letter == "u" then
        vowels = vowels + 1
    endif
next i
print("Vowels: " + str(vowels))

Explanation

Converting the word to lower case first means only five comparisons are needed instead of ten. Strings are indexed from 0, so a word of length n has characters at indexes 0 to n - 1, and substring(i, 1) gives the character at index i. Each comparison in the condition must be complete: letter == "a" OR "e" is a common error that does not test the second vowel. For "Elephant" the program outputs 3.

Mark scheme
  1. 1 mark

    inputs the word

    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. Max 5 marks from the 6 mark points.

  2. 1 mark

    converts the word (or each letter) to lower case, or checks for both upper-case and lower-case vowels

    e.g. word = word.lower, or .upper with upper-case vowels.

  3. 1 mark

    uses a loop that visits every character of the word

    e.g. for i = 0 to word.length - 1; Python for letter in word. Do not accept for i = 0 to word.length (goes past the last character) or for i = 1 to word.length with substring(i, 1).

  4. 1 mark

    extracts each character in turn

    e.g. word.substring(i, 1) or word[i].

  5. 1 mark

    checks whether the character is one of the five vowels and adds 1 to a count that starts at 0

    e.g. if letter == "a" OR letter == "e" OR ... OR letter == "u" then count = count + 1. Accept Python if letter in "aeiou". Do not accept if letter == "a" OR "e" OR ... (each comparison must be written in full).

  6. 1 mark

    outputs the count after the loop

    once, after the loop. Treat a missing str() as a minor error.

Question 4

6 marks

A music festival stores the names of the 8 bands playing in an array called bands, at indexes 0 to 7.

Write a program, using OCR Exam Reference Language or a high-level programming language, that:
- asks the user to enter the name of a band
- searches the array for the name, stopping as soon as it is found
- outputs the index of the band if it is found
- outputs "Not playing" if the band is not in the array.

You can assume the array bands has already been filled.

Model answer

search = input("Enter a band name: ")
found = false
i = 0
while found == false AND i < 8
    if bands[i] == search then
        found = true
    else
        i = i + 1
    endif
endwhile
if found then
    print("Position: " + str(i))
else
    print("Not playing")
endif

Explanation

This is a linear search. A condition-controlled loop with two exit conditions - the band has been found, or every index has been checked - stops as soon as there is a match. In the model, i only goes up when there is no match, so when the loop ends i holds the index of the band. The result is decided after the loop, so "Not playing" is output only once, after all 8 names have been checked.

Mark scheme
  1. 1 mark

    inputs the band name

    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.

  2. 1 mark

    starts at index 0 and moves through the array one element at a time

    e.g. i = 0 before a while loop and i = i + 1 inside it, or for i = 0 to 7.

  3. 1 mark

    compares each element with the name entered

    e.g. if bands[i] == search then.

  4. 1 mark

    stops searching as soon as the band is found

    e.g. a Boolean found flag in the loop condition. Accept a for loop with break (Python), or a function that returns as soon as the band is found. Do not award if the search always checks all 8 elements.

  5. 1 mark

    stops searching after the last element (index 7) without going out of range

    e.g. while found == false AND i < 8, or i <= 7, or i < bands.length. Do not accept i <= 8.

  6. 1 mark

    outputs the index if the band is found, otherwise outputs "Not playing", once, after the search

    Do not award if "Not playing" is output for each element that does not match, or if the index output is one more than the real index.

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.