OCR GCSE Computer Science June 2022 Paper 2, Question 2(d): casting and the staff ID trace table

OCR J277/02 June 2022, Question 2(d): define casting and find it in the algorithm, then trace a while loop that pads a staff ID with x until it is 10 characters long, for Kofi and 2021. Worked through, with the algorithm to run.

Past paper questionOCR J277/02June 2022 Paper 26 marksTrace table

Question 2(d) of the OCR GCSE Computer Science Paper 2 sat on 27 May 2022 (J277/02) shows a seven line algorithm that builds a staff ID. Part (i) is about casting (2 marks). Part (ii) is a trace table (4 marks).

We do not copy the exam paper here. Open it beside this page: OCR June 2022 J277/02 question paper (PDF). When you have finished, check the mark scheme too.

The question in short

The algorithm reads a surname (line 01) and a starting year (line 02). Line 03 joins the surname to str(year) to make staffID. Then, while staffID is shorter than 10 characters (line 04), line 05 adds an "x" to the end. Line 07 prints "ID " and the staff ID.

Part (i): casting

  • Definition: changing a value from one data type to another.
  • Line number: 03, where str(year) turns the year into a string so that it can be joined to the surname.

"Changing it to a string" is refused as a definition. That is what happens in this example, not what casting means.

Part (ii): trace it for Kofi and 2021

Lines 01 and 02 are filled in for you.

Line 03. "Kofi" joined to "2021" is Kofi2021. Count the characters: 8.

Line 04. Is 8 less than 10? Yes. Line 05: Kofi2021x, which is 9 characters.

Line 04. Is 9 less than 10? Yes. Line 05: Kofi2021xx, which is 10.

Line 04. Is 10 less than 10? No. The loop ends.

Line 07. The output is ID Kofi2021xx.

Line number surname year staffID Output
01 Kofi
02 2021
03 Kofi2021
05 Kofi2021x
05 Kofi2021xx
07 ID Kofi2021xx

One mark for each of the four rows you add.

Where the marks are lost

  • A third x. The loop runs while the length is less than 10. At exactly 10 it stops. Count the characters each time, do not guess.
  • Kofi 2021. Joining strings adds no space. There is a space in the output, because it is inside the quotation marks of "ID ".
  • Putting the new staffID on line 04. Line 04 is the test. The change happens on line 05.
  • Leaving "ID" off the output. The print joins two things. Both are output.
  • Quotation marks round part of the output. "ID" Kofi2021xx is marked wrong.

Run it

The algorithm in Python, printing the line number whenever something changes. Try a long surname: the loop may not run at all.

Kofi and 2021 give the rows 03, 05, 05 and 07. Try Fernandez and 2019: the ID is already longer than 10, so line 05 never runs.
The program
from bugbot import *
connect()

surname = input("Enter surname: ")
year = int(input("Enter starting year: "))
staffID = surname + str(year)
print("03 staffID =", staffID, "(" + str(len(staffID)) + " characters)")
while len(staffID) < 10:
    staffID = staffID + "x"
    print("05 staffID =", staffID)
print("07 output: ID " + staffID)
Put this demo on your own site

Paste it into a school website, Moodle, Google Sites or a blog. More options on the embed page.

Questions

What is the answer to OCR J277 June 2022 Paper 2 Question 2(d)(ii)?

Line 03 sets staffID to Kofi2021. Line 05 makes it Kofi2021x, then Kofi2021xx. Line 07 outputs ID Kofi2021xx.

What is casting?

Casting is converting a value from one data type to another, for example the integer 2021 to the string "2021" with str(), or the string "7" to the integer 7 with int().

Why does the year need to be cast before it is joined to the surname?

The + operator joins two strings or adds two numbers. It cannot mix a string and an integer, so the year is cast to a string first.

More from this paper

Every OCR J277 question we have worked · Guide: Trace tables explained

Learn it step by step

  1. F1.8 Data types and casting Programming basics
  2. F13.2 Trace tables Exam preparation
  3. F2.6 Condition-controlled loops: while Decisions and loops
Open the lessons

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.