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

Data types and casting

Practise 27 exam-style questions on Data types and casting 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
  • 1–6 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

1 mark

A sports day program records how far each javelin is thrown, in metres, for example 42.75.

Which data type is the most suitable for the distance?

Choose your answer.

  • Boolean
  • character
  • integer
  • real

Model answer

real

Explanation

Distances such as 42.75 m have a decimal part, so they need the real (float) data type. An integer would lose the .75, and character and Boolean cannot store numbers like this at all.

Mark scheme
  1. 1 mark

    real

    Only this option. Accept float. If more than one option is given, 0 marks.

Question 2

2 marks

A cafe's till program stores the price of each item in pounds, for example 2.45.

State the most suitable data type for the price and explain why an integer is not suitable.

Model answer

Real. An integer can only store whole numbers, so the .45 (the pence) could not be stored and the price would be wrong.

Explanation

A real (float) stores numbers with a fractional part, so 2.45 can be stored. An integer has no decimal part, so the pence would be lost and every bill would be wrong. (Some programs store money as a whole number of pence, 245, but when the price is stored in pounds it must be real.)

Mark scheme
  1. 1 mark

    real

    Accept float. Do not accept integer: the value stored is in pounds with a decimal part.

  2. 1 mark

    an integer can only hold whole numbers, so the pence (the decimal part, .45) would be lost / 2.45 could not be stored exactly

    Accept 'the price would become 2'.

Question 3

3 marks

A game stores these details about a player's character.

name = "Zara"
lives = 3
speed = 2.5
hasShield = False

State the data type of lives, the data type of speed and the data type of hasShield.

Model answer

lives: integer
speed: real
hasShield: Boolean

Explanation

3 is a whole number, so lives is an integer. 2.5 has a decimal part, so speed is real. False (with no quotation marks) is one of the two Boolean values. Only name, in quotation marks, is a string - a value such as "False" in quotes would be a string, not a Boolean.

Mark scheme
  1. 1 mark

    lives: integer

    Accept int.

  2. 1 mark

    speed: real

    Accept float.

  3. 1 mark

    hasShield: Boolean

    Accept bool. Do not accept string (False is not in quotation marks).

Question 4

4 marks

A program calculates a worker's pay. The number of hours worked is always a whole number and the hourly rate is in pounds, for example 11.44.

01hours = input("Enter hours worked")
02rate = input("Enter hourly rate")
03pay = hours * rate
04print("Your pay is " + pay)

The program does not work because of the data types used. Rewrite each line that needs to change, using OCR Exam Reference Language or a high-level programming language, and explain why the changes are needed.

Model answer

hours = int(input("Enter hours worked"))
rate = real(input("Enter hourly rate"))
pay = hours * rate
print("Your pay is " + str(pay))

Lines 01 and 02 change because input returns strings, which cannot be multiplied together. Line 04 changes because pay is a number, which must be cast to a string before it is joined to the text.

Explanation

Hours is a whole number, so it is cast to an integer; the rate has pence, so it is cast to a real. Only then can line 03 multiply them. Line 04 joins text to the number with +, which needs str(). Line 03 itself is fine once the inputs are numbers - a common mistake is to rewrite the calculation but leave the output line unchanged.

Mark scheme
  1. 1 mark

    line 01: hours = int(input("Enter hours worked"))

    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 real/float. Also accept casting in line 03 instead, e.g. pay = int(hours) * real(rate), which is correct and earns this mark and the next.

  2. 1 mark

    line 02: rate = real(input("Enter hourly rate"))

    Accept float. Do not accept int (the pence would be lost).

  3. 1 mark

    line 04: print("Your pay is " + str(pay))

    Accept print("Your pay is", pay) (Python).

  4. 1 mark

    explanation: input returns strings, so hours and rate must be cast to numbers before they can be multiplied, and pay must be cast to a string to be joined to the text

    Either part of the explanation is enough for this mark.

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.