Choosing a suitable data type for a value

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

Choosing a suitable data type for a value

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A program stores a student's age in whole years. Which data type and reason is best?
A INTEGER because age in whole years is a whole number
B REAL because age is a number
2. A program stores the price of a notebook, such as 4.75. Which data type and reason is best?
A REAL because the price has pence (a decimal part)
B INTEGER because a price is a number
3. A program stores a member's initial, for example 'J'. Which data type is best?
A STRING because it is a name
B CHAR because it is a single character
4. A program stores an address such as "12 High Street". Which data type is best?
A CHAR because it is one piece of data
B STRING because it is several characters of text
5. A program records whether a customer is a member: either yes or no. Which data type is best?
A BOOLEAN because it has only two possible values (TRUE or FALSE)
B STRING because it could be typed as the word yes or no

Match the Pairs

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

Left columnRight column
1DECLARE Price : REAL ADECLARE Age : INTEGER
2DECLARE IsMember : BOOLEAN BPrice holds a money amount with pence, so it needs a number type that can store decimal places
3An age is a whole number that is used in calculations, so it is INTEGER because it has no decimal part CA price has pence, so INTEGER would lose the decimal part. It should be REAL because it can hold decimals
4A single letter such as a first initial is stored as CHAR because it is exactly one character DDECLARE Initial : CHAR
5DECLARE Address : INTEGER EIsMember holds a two-valued fact that is either TRUE or FALSE, so BOOLEAN is chosen
6DECLARE Price : INTEGER FAn address is text made of letters, numbers and spaces and is never used in calculations, so it should be STRING, not INTEGER

Definitions

data type

Fill in the Blanks

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

1.
// a person's age in whole years DECLARE Age : _______
2.
// a price such as 2.50 with pence DECLARE Price : ____
3.
// the first letter of a name DECLARE Initial : ____
4.
// is the customer a member? TRUE or FALSE DECLARE IsMember : _______
5.
// house number, street and town DECLARE Address : ______

Spot the Error

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

Question 1
A DECLARE Age : INTEGER
B DECLARE Price : REAL
C DECLARE Initial : CHAR
D DECLARE IsMember : STRING
Question 2
A DECLARE Price : INTEGER
B Price ← 4.75
C OUTPUT Price
Question 3
A DECLARE Initial : CHAR
B Initial ← 'Kim'
C OUTPUT Initial
Question 4
A DECLARE Age : INTEGER
B Age ← 15
C DECLARE IsMember : BOOLEAN
D IsMember ← "TRUE"
Question 5
A DECLARE Price : REAL
B DECLARE Qty : BOOLEAN
C DECLARE Total : REAL
D Total ← Price * Qty

Answer Key

Choosing a suitable data type for a value
Section A: Quick Questions
Q1
A — INTEGER because age in whole years is a whole number
Q2
A — REAL because the price has pence (a decimal part)
Q3
B — CHAR because it is a single character
Q4
B — STRING because it is several characters of text
Q5
A — BOOLEAN because it has only two possible values (TRUE or FALSE)
Section B: Match the Pairs
Q1
DECLARE Price : REAL → Price holds a money amount with pence, so it needs a number type that can store decimal places
Q2
DECLARE IsMember : BOOLEAN → IsMember holds a two-valued fact that is either TRUE or FALSE, so BOOLEAN is chosen
Q3
An age is a whole number that is used in calculations, so it is INTEGER because it has no decimal part → DECLARE Age : INTEGER
Q4
A single letter such as a first initial is stored as CHAR because it is exactly one character → DECLARE Initial : CHAR
Q5
DECLARE Address : INTEGER → An address is text made of letters, numbers and spaces and is never used in calculations, so it should be STRING, not INTEGER
Q6
DECLARE Price : INTEGER → A price has pence, so INTEGER would lose the decimal part. It should be REAL because it can hold decimals
Section C: Definitions
Q1
data type — Pick INTEGER, REAL, CHAR, STRING or BOOLEAN by what the value is and what you do with it, then justify with 'because'. Eg price is REAL because it has pence.
Section D: Fill in the Blanks
Q1
// a person's age in whole years DECLARE Age : INTEGER
Q2
// a price such as 2.50 with pence DECLARE Price : REAL
Q3
// the first letter of a name DECLARE Initial : CHAR
Q4
// is the customer a member? TRUE or FALSE DECLARE IsMember : BOOLEAN
Q5
// house number, street and town DECLARE Address : STRING
Section E: Spot the Error
Q1
D — Whether someone is a member has only two possible values (yes or no), so it should be BOOLEAN, not STRING. Correct line: DECLARE IsMember : BOOLEAN.
Q2
A — A price has pence, so it needs decimal places. It must be REAL because INTEGER only stores whole numbers. Correct line: DECLARE Price : REAL.
Q3
B — An initial is one character, so a CHAR variable can only hold a single character such as 'K'. 'Kim' is three characters and belongs in a STRING.
Q4
D — With quote marks, "TRUE" is a STRING, not a BOOLEAN. A BOOLEAN value is written without quotes. Correct line: IsMember ← TRUE.
Q5
B — A quantity is a whole number that is used in a calculation, so it should be INTEGER. BOOLEAN is only for two-valued facts such as TRUE or FALSE. Correct line: DECLARE Qty : INTEGER.