Oxford Cambridge and RSA
GCSE (9-1) Computer Science
Algorithms & Programming
J277/02
Topic: Data Types & Casting
Time allowed: 40 minutes
Centre number Candidate number
First name Last name
INSTRUCTIONS INFORMATION
Turn over
Section A: Low-Frequency "Ghost Topics"
1
A common misconception is that casting a Real number to an Integer rounds it to the nearest whole number.

(a) State the value of the variable x after the following line of code is run:
x = int(9.9)
[1]

(b) Explain how the computer arrived at this value (refer to the specific behavior of the int() function).
[1]
2
The OCR specification requires knowledge of ASC() and CHR().
  • ASC(character) casts a character to its ASCII integer value.
  • CHR(integer) casts an integer to its ASCII character value.
Given that the ASCII value for 'A' is 65, complete the trace table for the following algorithm.
char1 = 'A' val1 = ASC(char1) val2 = val1 + 2 char2 = CHR(val2) print(char2)
[4]
char1 val1 val2 char2 Output
'A'
3
Determine the final value of the variable result in each case.
[3]
Code Final Value Data Type
result = str(10 + 5)
result = int("12") + 5
result = float(int(4.8))
4
A student wants to convert the user's input (the text "True" or "False") into a real Boolean value.
They write this code:
userInput = input("Enter True or False: ") isValid = bool(userInput)
(a) Explain why this code logic fails if the user types "False".
(Hint: In Python, bool("non-empty string") evaluates to True).
[2]

(b) Rewrite the code using an IF statement to correctly set the variable isValid to the Boolean False if the input is "False".
[2]
Turn over
5
Write a short algorithm that generates a random capital letter (A–Z).
  • You must generate a random Integer between 65 and 90.
  • You must Cast this integer to a Character before printing it.
Note: random(min, max) generates a random number.
[3]
Section B: High-Frequency Baseline Skills
6
The following code attempts to calculate a user's age in months and print it.
It contains a Type Mismatch Error.
01 ageYears = int(input("Enter age in years: ")) 02 ageMonths = ageYears * 12 03 print("You are " + ageMonths + " months old.")
(a) Identify the line number where the error occurs.
[1]
Line:

(b) Rewrite the line to fix the error using casting.
[2]
7
A program needs to calculate the area of a circle. The radius may be a decimal number (e.g., 5.5).
Fill in the missing keywords to ensure the calculation works.
[3]
radiusString = input("Enter radius: ") radiusVal = ..............................(radiusString) # Must handle decimals area = 3.142 * (radiusVal * radiusVal) print("The area is " + ..............................(area))
8
State the output of the following code.
x = "10" y = "2" print(x + y)
[2]
9
Explain why casting a Real value to an Integer is considered a "narrowing" conversion, and what risk this poses to the accuracy of data.
[2]
END OF QUESTION PAPER