TopMyGrade

GCSE/Computer Science/OCR

2.2.3Data types: integer, real, Boolean, character, string; choosing an appropriate type

Notes

Data types in programming

A data type defines what kind of data a variable can hold and what operations are valid on it. OCR J277 requires you to identify, use and justify appropriate data types. Questions may ask you to identify types in given code, or to state the correct type for a given scenario.

The five main data types (OCR J277)

1. Integer

  • Stores whole numbers (no decimal point): ..., -2, -1, 0, 1, 2, 100, -500.
  • Operations: +, -, *, DIV (integer division), MOD (remainder).
  • Examples of use: age, number of items, a loop counter, a score, a year.
age ← 16
count ← 0
score ← 100

2. Real (float/double)

  • Stores numbers with a decimal point: 3.14, -0.5, 100.0, 2.718.
  • Operations: +, -, *, / (and all standard arithmetic).
  • Examples of use: a price, a temperature, a distance, a percentage, a weight.
price ← 4.99
temperature ← 36.6

Note: integers are a subset of real numbers, but storing whole numbers as integers is more memory-efficient and avoids floating-point rounding errors.

3. Boolean

  • Stores only True or False (one of two values).
  • Used in conditions and logical expressions.
  • Examples of use: isLoggedIn, hasWon, isPrime, doorOpen.
isLoggedIn ← False
hasWon ← True

4. Character (Char)

  • Stores a single character: a letter, digit, punctuation mark or symbol.
  • Enclosed in single quotes in many languages.
  • Examples: 'A', '3', '?', ' ' (space).
grade ← 'A'
initial ← 'H'

5. String

  • Stores a sequence of characters (text of any length, including spaces).
  • Enclosed in double quotes in OCR pseudocode.
  • Examples of use: a name, a message, a password, an address, a postcode.
name ← "Alice"
message ← "Enter your PIN: "
postcode ← "SW1A 1AA"

Choosing the right data type

ScenarioCorrect typeReason
Number of students in a classIntegerWhole number; can't have 0.5 students
Average exam scoreRealMay be a decimal (e.g. 68.4)
Whether a user has admin rightsBooleanOnly True or False
First letter of a student's surnameCharacterSingle character only
A student's full nameStringMultiple characters
A phone number (stored, not calculated)StringContains leading zeros and spaces; arithmetic not needed

The phone number trap

  • "07700 900123" contains a leading zero and a space.
  • If stored as Integer, the leading zero is lost: 7700900123 (wrong) and spaces are not allowed.
  • Phone numbers, postcodes, and National Insurance numbers should be stored as String, not Integer.

Type casting / conversion

Sometimes you need to convert between types:

  • String to integer: INT("42") → 42.
  • Integer to string: STR(42) → "42".
  • String to real: FLOAT("3.14") → 3.14.
  • Character to ASCII code: ASC('A') → 65.

Common OCR exam mistakes

  1. Choosing Integer for prices — prices have decimals → use Real.
  2. Choosing Integer for phone numbers — leading zeros are lost → use String.
  3. Confusing Character and String — Character is exactly one character; String is zero or more characters.
  4. Forgetting Boolean can only be True or False — it cannot store "yes"/"no" (those are strings).

AI-generated · claude-opus-4-7 · v3-ocr-computer-science

Practice questions

Try each before peeking at the worked solution.

  1. Question 15 marks

    Identifying data types

    State the most appropriate data type for each of the following:
    (a) The number of pages in a book. [1]
    (b) The price of an item in pounds. [1]
    (c) Whether a student has paid their fees. [1]
    (d) A student's middle initial. [1]
    (e) A student's email address. [1]

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-ocr-computer-science

  2. Question 23 marks

    Phone number data type

    A student wants to store a mobile phone number (e.g. 07700 900123) in a variable. They suggest using an Integer data type.

    Give two reasons why Integer is not appropriate and state the correct data type. [3 marks]

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-ocr-computer-science

  3. Question 36 marks

    Data type selection with justification

    A programmer is writing a program to track a student's exam results. For each variable below, state an appropriate data type and justify your choice.

    (a) studentName [2]
    (b) examScore (can be a decimal) [2]
    (c) hasPassed [2]

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-ocr-computer-science

Flashcards

2.2.3 — Data types: integer, real, Boolean, character, string; choosing an appropriate type

8-card SR deck for OCR Computer Science (J277) topic 2.2.3

8 cards · spaced repetition (SM-2)