Chhetri AcademyGCSE & A level Paper Builder

2.2.2Data types and casting

OCR GCSE Computer Science (J277) · Algorithms and programming (Paper 2) › Programming fundamentals

Practise Data types and casting. 12 exam-style questions plus unlimited generated ones on this subtopic, at up to four difficulty levels, with full mark schemes and a progress tracker. Free, no account needed.

Build a paper on this topic

▶ Watch videos on Data types and casting (Craig 'n' Dave OCR on YouTube) · Practise all of Programming fundamentals

Quick recall

Cover the answers and test yourself. The app has these as flashcards that come back just before you'd forget them.

State the data type of the value stored in price after line 01 has been executed.
String

Sample questions

Written for this site in the style of OCR exam questions. They are not taken from real past papers.

Question 1Easy4 marks
(a) Which data type is the most suitable for storing a telephone number such as 07700 900123?
Tick (✓) one box.[1]
  • Integer
  • Real
  • String
  • Boolean
(b) Explain why this data type is the most suitable.[2]
(c) State what is meant by casting.[1]
Show the answer and mark scheme
(a) Answer: String
(b)
  • the leading zero would be lost if it was stored as a number
  • no calculations are carried out on telephone numbers
  • it may contain spaces or other characters such as +
(c)
  • converting a value from one data type to another (e.g. a string to an integer)
Question 2Medium5 marks
Look at this program.
01 age = input("Enter your age")
02 if age >= 18 then
03    print("Adult")
04 endif
05 print("You will be " + (age + 1) + " next year")
(a) Explain why line 02 could cause an error.[2]
(b) Rewrite line 01 so that line 02 works correctly.[1]
(c) After line 01 has been corrected, line 05 still causes an error. Explain why, and write a corrected line 05.[2]
Show the answer and mark scheme
(a)
  • input returns a string, so age holds a string
  • a string cannot be compared with the number 18 / the comparison is not numeric
(b)
  • age = int(input("Enter your age"))
(c)
  • age + 1 is an integer, which cannot be joined (concatenated) to a string
  • print("You will be " + str(age + 1) + " next year")
Question 3Hard7 marks
Look at this program.
01 items = 3
02 price = 4.5
03 total = items * price
04 msg = "Total: " + str(total)
05 paid = total > 10
06 code = "ABC123".substring(3, 3)
07 num = int(code) * 2
(a) State the data type of each of these variables after the program has run: items, price, total, msg, paid, code, num.[4]
(b) State the final values of msg, paid and num.[3]
Show the answer and mark scheme
(a) Answer: items integer; price real; total real; msg string; paid Boolean; code string; num integer
  • items and num: integer
  • price and total: real
  • msg and code: string
  • paid: Boolean
(b) Answer: msg = "Total: 13.5", paid = True, num = 246
  • msg: Total: 13.5
  • paid: True
  • num: 246

Stuck? Get 1-to-1 help. Chhetri Academy tutors GCSE and A level Maths and Science online, with a free 30-minute trial lesson.

Book a free trial