Skip to main content
EnglandComputer ScienceSyllabus dot point

What are variables, constants and data types, and how do you choose and convert between them?

The use of variables and constants, the common data types (integer, real, Boolean, character and string), choosing an appropriate data type, and casting (converting) between data types.

An OCR J277 2.2.1 answer on variables and constants, the common data types (integer, real, Boolean, character, string), choosing an appropriate data type for data, and casting between data types.

Generated by Claude Opus 4.810 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this dot point is asking
  2. Variables and constants
  3. The common data types
  4. Choosing an appropriate data type
  5. Casting (converting between types)
  6. Try this

What this dot point is asking

OCR wants you to use variables and constants, know the common data types (integer, real, Boolean, character, string), choose the most appropriate type for a piece of data, and cast (convert) between types. This is foundational for all of Component 02 and is examined in Paper 2, often as "state the data type" or "explain the difference between a variable and a constant".

Variables and constants

Using a constant for a fixed value is good practice: if a tax rate appears in twenty calculations, naming it VAT = 0.20 once means a rate change is a one-line edit, and nobody can accidentally overwrite it.

The common data types

Choosing an appropriate data type

Casting (converting between types)

Try this

Q1. State the most appropriate data type for storing a temperature such as 21.5 degrees. [1 mark]

  • Cue. Real (float), because it has a decimal part.

Q2. State the difference between a variable and a constant. [1 mark]

  • Cue. A variable's value can change while the program runs; a constant's value is fixed once set and cannot change.

Q3. A user types their age into an input box. Write a line that reads it and stores it as a whole number. [1 mark]

  • Cue. age = int(input("Enter your age: ")).

Exam-style practice questions

Practice questions written in the style of OCR exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

OCR 20214 marksState the most appropriate data type for each of the following, and justify one of your choices: (a) a person's age in whole years, (b) the price of an item in pounds and pence, (c) whether a user is logged in, (d) a person's full name.
Show worked answer →

Award one mark per correct data type, to a maximum of four (including the justification).

(a) Integer, because an age in whole years is a whole number. (b) Real (also called float), because a price has a decimal part (pounds and pence). (c) Boolean, because it is either true or false (logged in or not). (d) String, because a name is a sequence of characters.

Justification example: a price must be a real because integers cannot store the pence (the decimal part), so 4.99 would be lost.

Markers reward the correct type for each and a clear reason for the chosen one. A common error is giving real for age or integer for price.

OCR 20223 marksExplain the difference between a variable and a constant, and give one reason a programmer might use a constant.
Show worked answer →

Variable (1 mark): a named storage location whose value can change while the program runs.

Constant (1 mark): a named storage location whose value is set once and cannot change while the program runs.

Reason to use a constant (1 mark): it makes the program clearer and easier to maintain, because a value used in many places (for example a VAT rate or pi) is named once and can be updated in a single place, and it prevents the value being changed by accident.

Markers reward the can-change versus cannot-change distinction and a genuine reason (clarity, single point of change, or safety), not just "it stays the same".

Related dot points

Sources & how we know this