Assigned value must match the variable's data type

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

Assigned value must match the variable's data type

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. DECLARE Age : INTEGER Age ← "Ali"
A Valid: Age can hold any value you assign to it
B Type mismatch: a STRING cannot be stored in an INTEGER variable
2. Which assignment is correct for DECLARE Name : STRING?
A Name ← Ali
B Name ← "Ali"
3. Which assignment is correct for DECLARE Grade : CHAR?
A Grade ← 'A'
B Grade ← "A"
4. DECLARE Score : INTEGER Score ← 42 Is this assignment valid?
A Yes: 42 is a whole number, so it matches INTEGER
B No: numbers cannot be stored in variables
5. DECLARE Initial : CHAR Initial ← "Ali" What is the problem?
A A STRING of several characters is assigned to a CHAR variable
B The variable name Initial is not allowed

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 ← "Ali" AType mismatch: text is assigned to an INTEGER variable. Score should be given a whole number such as 45.
2DECLARE Grade : CHAR Grade ← 'B' BA correct assignment: a single character in single quotes matches the CHAR type
3A STRING variable must be given text inside double quotes CDECLARE Initial : CHAR Initial ← 'A'
4A CHAR variable must be given a single character inside single quotes DDECLARE Name : STRING Name ← "Ali"
5DECLARE Score : INTEGER Score ← "Ali" EA CHAR value needs single quotes, not double quotes. It should be Letter ← 'A'.
6DECLARE Letter : CHAR Letter ← "A" FA type mismatch: the text "Ali" cannot be stored in an INTEGER variable

Definitions

type mismatch

Fill in the Blanks

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

1.
DECLARE Age : _______ Age ← 15
2.
DECLARE Name : ______ Name ← "Ali"
3.
DECLARE Grade : ____ Grade ← 'A'
4.
DECLARE Town : STRING Town _ "Leeds"
5.
_______ Initial : CHAR Initial ← 'K'

Spot the Error

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

Question 1
A DECLARE Age : INTEGER
B Age ← 15
C Age ← "Ali"
D OUTPUT Age
Question 2
A DECLARE Name : INTEGER
B Name ← "Ali"
C OUTPUT Name
Question 3
A DECLARE Grade : BOOLEAN
B Grade ← 'B'
C OUTPUT Grade
Question 4
A DECLARE City : STRING
B DECLARE Age : INTEGER
C City ← Leeds
D Age ← 14
Question 5
A DECLARE Initial : CHAR
B DECLARE Name : STRING
C Initial ← "A"
D Name ← "Ali"

Answer Key

Assigned value must match the variable's data type
Section A: Quick Questions
Q1
B — Type mismatch: a STRING cannot be stored in an INTEGER variable
Q2
B — Name ← "Ali"
Q3
A — Grade ← 'A'
Q4
A — Yes: 42 is a whole number, so it matches INTEGER
Q5
A — A STRING of several characters is assigned to a CHAR variable
Section B: Match the Pairs
Q1
DECLARE Age : INTEGER Age ← "Ali" → A type mismatch: the text "Ali" cannot be stored in an INTEGER variable
Q2
DECLARE Grade : CHAR Grade ← 'B' → A correct assignment: a single character in single quotes matches the CHAR type
Q3
A STRING variable must be given text inside double quotes → DECLARE Name : STRING Name ← "Ali"
Q4
A CHAR variable must be given a single character inside single quotes → DECLARE Initial : CHAR Initial ← 'A'
Q5
DECLARE Score : INTEGER Score ← "Ali" → Type mismatch: text is assigned to an INTEGER variable. Score should be given a whole number such as 45.
Q6
DECLARE Letter : CHAR Letter ← "A" → A CHAR value needs single quotes, not double quotes. It should be Letter ← 'A'.
Section C: Definitions
Q1
type mismatch — An error when a value assigned to a variable has a different data type from the one it was declared with, such as storing a name in an INTEGER variable.
Section D: Fill in the Blanks
Q1
DECLARE Age : INTEGER Age ← 15
Q2
DECLARE Name : STRING Name ← "Ali"
Q3
DECLARE Grade : CHAR Grade ← 'A'
Q4
DECLARE Town : STRING Town ← "Leeds"
Q5
DECLARE Initial : CHAR Initial ← 'K'
Section E: Spot the Error
Q1
C — Age was declared as INTEGER, so it can only hold whole numbers. The text "Ali" is a STRING, which causes a type mismatch. It should be a whole number such as Age ← Age + 1.
Q2
A — Name is assigned the text "Ali", so it must be declared as STRING. An INTEGER variable cannot hold text. It should be DECLARE Name : STRING.
Q3
A — Grade stores the single character 'B', so it must be declared as CHAR. A BOOLEAN variable can only hold TRUE or FALSE. It should be DECLARE Grade : CHAR.
Q4
C — A STRING value needs double quotes. Without them, Leeds looks like a variable name, not text. It should be City ← "Leeds".
Q5
C — A CHAR value must be written in single quotes. Double quotes make it a STRING literal, which is a type mismatch. It should be Initial ← 'A'.