Error: wrong data type or undeclared variable

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Error: wrong data type or undeclared variable

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line causes a data type error?
A DECLARE Age : INTEGER Age ← 16
B DECLARE Age : INTEGER Age ← "sixteen"
2. Which program uses a variable that was never declared?
A Score ← 10 OUTPUT Score
B DECLARE Score : INTEGER Score ← 10 OUTPUT Score
3. DECLARE Marks : ARRAY[1:5] OF INTEGER Which statement uses an index outside the bounds?
A Marks[5] ← 70
B Marks[6] ← 70
4. DECLARE Name : STRING Which assignment is a data type error?
A Name ← 42
B Name ← "Maya"
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

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

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

Definitions

Data type error

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
_______ Score : INTEGER Score ← 10
2.
DECLARE Name : ______ Name ← "Ava"
3.
DECLARE Marks : ARRAY[1:5] OF INTEGER Marks[_] ← 90
4.
DECLARE Price : ____ Price ← 4.5
5.
DECLARE Total : INTEGER Total ← 0 OUTPUT _____

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A DECLARE Age : INTEGER
B DECLARE Name : STRING
C Name ← "Sam"
D Age ← "fifteen"
Question 2
A DECLARE Score : STRING
B Score ← 0
C Score ← Score + 10
D OUTPUT Score
Question 3
A DECLARE Total : INTEGER
B DECLARE Counter : INTEGER
C Count ← 3
D Total ← Count * 2
Question 4
A DECLARE Marks : ARRAY[1:5] OF INTEGER
B Marks[1] ← 70
C Marks[6] ← 85
D OUTPUT Marks[5]
Question 5
A DECLARE Passed : BOOLEAN
B DECLARE Mark : INTEGER
C Passed ← "yes"
D Mark ← 62

Answer Key

Error: wrong data type or undeclared variable
Section A: Quick Questions
Q1
B — DECLARE Age : INTEGER Age ← "sixteen"
Q2
A — Score ← 10 OUTPUT Score
Q3
B — Marks[6] ← 70
Q4
A — Name ← 42
Q5
A — Add DECLARE Total : INTEGER before the assignment
Section B: Match the Pairs
Q1
DECLARE Age : INTEGER Age ← "twelve" → A data type error: a STRING value is assigned to a variable declared as INTEGER
Q2
Total ← Total + 5 → An error if Total was never declared: the variable is used without a DECLARE statement
Q3
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
Q4
A variable must be declared with a data type before a value is stored in it → DECLARE Name : STRING Name ← "Ava"
Q5
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.
Q6
Price ← 4.50 OUTPUT Price → Price is used without DECLARE. Fix: add DECLARE Price : REAL before assigning the value.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
DECLARE Score : INTEGER Score ← 10
Q2
DECLARE Name : STRING Name ← "Ava"
Q3
DECLARE Marks : ARRAY[1:5] OF INTEGER Marks[5] ← 90
Q4
DECLARE Price : REAL Price ← 4.5
Q5
DECLARE Total : INTEGER Total ← 0 OUTPUT Total
Section E: Spot the Error
Q1
D — Age is declared as INTEGER, so it cannot store the STRING "fifteen". It should be Age ← 15.
Q2
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.
Q3
B — The program declares Counter but then uses Count, which was never declared. The line should be DECLARE Count : INTEGER.
Q4
C — The array only has indexes 1 to 5, so index 6 is outside the bounds. It should be Marks[5] ← 85.
Q5
C — A BOOLEAN variable can only store TRUE or FALSE, not the STRING "yes". It should be Passed ← TRUE.