← IGCSE Computer Science Paper 2

Error: wrong data type or undeclared variable

An error caused by using a value its data type does not allow (e.g. a STRING stored in an INTEGER, or an index outside the array) or by using a variable that was never DECLAREd.

Understanding Error: wrong data type or undeclared variable in IGCSE Computer Science Paper 2

What it does: The array only has elements 1 to 5, so Scores[6] is outside the bounds and is not allowed. DECLARE sets the data type first (STRING), and the value stored then matches that type, so there is no error. The array has indexes 1 to 5 only. Index 6 does not exist, so Marks[6] is an error. The mistake is a missing DECLARE. Changing the assignment symbol does not declare the variable. REAL stores numbers with decimals. The STRING "free" cannot be stored in a REAL variable. Age was declared as INTEGER, so it can only hold whole numbers. Storing the text "twelve" in it breaks the data type rule.

Common mistakes: Age is declared as INTEGER, so it cannot store the STRING "fifteen". It should be Age ← 15. Score is used for arithmetic and is given the number 0, so it must be an INTEGER. Declaring it as STRING makes these lines data type errors.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 242–253) and word search (Modules 232–253).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line causes a data type error?
DECLARE Age : INTEGER Age ← "sixteen"DECLARE Age : INTEGER Age ← 16
Matches
DECLARE Count : INTEGER Count ← "ten"
↓
A STRING is assigned to an INTEGER variable. Fix: store a whole number such as Count ← 10, or declare Count as STRING.
Applying
Meanings
Term
Data type error
Meaning
An error caused by using a value its data type does not allow (e.g. a STRING stored in an INTEGER, or an index outside the array) or by using a variable that was never DECLAREd.
Practice
DECLARE Score : INTEGER
Testing
Gaps
Score : INTEGER
Check Answer
Spots
3Name ← "Sam"
4Age ← "fifteen"

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Lion' badge — Inheritance King, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which line causes a data type error?
A. DECLARE Age : INTEGER Age ← 16
B. DECLARE Age : INTEGER Age ← "sixteen" ✓
An INTEGER variable can only store whole numbers. "sixteen" is a STRING, so storing it in Age is a data type error.
2. Which program uses a variable that was never declared?
A. Score ← 10 OUTPUT Score ✓
B. DECLARE Score : INTEGER Score ← 10 OUTPUT Score
In the first program Score is used without DECLARE Score : INTEGER, so it is an undeclared variable error.
3. DECLARE Marks : ARRAY[1:5] OF INTEGER Which statement uses an index outside the bounds?
A. Marks[5] ← 70
B. Marks[6] ← 70 ✓
The array has indexes 1 to 5 only. Index 6 does not exist, so Marks[6] is an error.
4. DECLARE Name : STRING Which assignment is a data type error?
A. Name ← 42 ✓
B. Name ← "Maya"
Name is a STRING variable, so the number 42 is the wrong data type. Text in double quotes is correct.
5. Which fix corrects this error? Total ← 0 (Total was never declared)
A. Add DECLARE Total : INTEGER before the assignment ✓
B. Change ← to = in the assignment
The mistake is a missing DECLARE. Changing the assignment symbol does not declare the variable.

Match the Pairs

DECLARE Age : INTEGER Age ← "twelve" → A data type error: a STRING value is assigned to a variable declared as INTEGER
Total ← Total + 5 → An error if Total was never declared: the variable is used without a DECLARE statement
An array index outside the bounds is a data type error because that element does not exist → DECLARE Scores : ARRAY[1:5] OF INTEGER Scores[6] ← 10
A variable must be declared with a data type before a value is stored in it → DECLARE Name : STRING Name ← "Ava"
DECLARE Count : INTEGER Count ← "ten" → A STRING is assigned to an INTEGER variable. Fix: store a whole number such as Count ← 10, or declare Count as STRING.
Price ← 4.50 OUTPUT Price → Price is used without DECLARE. Fix: add DECLARE Price : REAL before assigning the value.

Key Term

Data type error
An error caused by using a value its data type does not allow (e.g. a STRING stored in an INTEGER, or an index outside the array) or by using a variable that was never DECLAREd.

Fill in the Blanks

1. DECLARE Score : INTEGER Score ← 10
Hint: Type the keyword that must be used to create a variable before you assign a value to it.
2. DECLARE Name : STRING Name ← "Ava"
Hint: Type the data type that can store text, so a text value does not cause a type error.
3. DECLARE Marks : ARRAY[1:5] OF INTEGER Marks[5] ← 90
Hint: Type the highest index that is inside the array bounds, so it is not out of range.
4. DECLARE Price : REAL Price ← 4.5
Hint: Type the data type for numbers with a decimal part, so an INTEGER is not wrongly used.
5. DECLARE Total : INTEGER Total ← 0 OUTPUT Total
Hint: Type the name of the variable that was declared above, not a different undeclared one.

Spot the Error

Question 1
A DECLARE Age : INTEGER
B DECLARE Name : STRING
C Name ← "Sam"
D Age ← "fifteen"
Line D — Age is declared as INTEGER, so it cannot store the STRING "fifteen". It should be Age ← 15.
Question 2
A DECLARE Score : STRING
B Score ← 0
C Score ← Score + 10
D OUTPUT Score
Line A — Score is used for arithmetic and is given the number 0, so it must be an INTEGER. Declaring it as STRING makes these lines data type errors.
Question 3
A DECLARE Total : INTEGER
B DECLARE Counter : INTEGER
C Count ← 3
D Total ← Count * 2
Line B — The program declares Counter but then uses Count, which was never declared. The line should be DECLARE Count : INTEGER.
Question 4
A DECLARE Marks : ARRAY[1:5] OF INTEGER
B Marks[1] ← 70
C Marks[6] ← 85
D OUTPUT Marks[5]
Line C — The array only has indexes 1 to 5, so index 6 is outside the bounds. It should be Marks[5] ← 85.
Question 5
A DECLARE Passed : BOOLEAN
B DECLARE Mark : INTEGER
C Passed ← "yes"
D Mark ← 62
Line C — A BOOLEAN variable can only store TRUE or FALSE, not the STRING "yes". It should be Passed ← TRUE.

Student with a class code? Join your class →