Type Match
A type is a promise about what you can do with a value, not a label for what it looks like. Which is why a telephone number is not a number, and why the box you typed into hands you text.
Get the method right under pressure
Free interactive practice on the steps that lose marks under exam pressure.
Start revising freeWhat you'll cover
A type is a promise about what you can do
It is tempting to learn five types as five definitions and expect that to be enough. It is not what the questions ask, and it is not really what a type is.
A type is a promise about what the program can DO with a value. Say a value is a whole number and you are promising it can be counted, added, and used where a whole number belongs, which is why DIV and MOD on the previous topic only make sense on those. Say a value is true or false and you are promising it can be tested, which is what every condition you have written evaluates to.
⚠️ That is why the classic traps are traps. A telephone number looks like a number. You will never add two of them together, and losing the zero on the front breaks it. It looks like a number and it is not one, and once you ask what the program does with a value rather than what it looks like, that stops being a surprise.
The five types this paper names
Five types and the operation that moves a value between them. The wording matters: your specification uses these names.
Looks like a number, is not a number
The question that settles almost every choice on this topic is not what the value looks like. It is what the program is going to do with it.
How to justify a type choice
These questions almost always say choose AND justify, and the second word carries most of the marks. ⚠️ The commonest weak justification restates the value. Saying you picked a whole number because the value is a whole number has told the examiner nothing they could not see. It is circular, and it scores accordingly. A justification has two clauses. What the program is going to DO with the value, and what would go wrong if you chose the alternative. Both halves, every time. The second half is what turns a guess into an argument. ⚠️ And here is the quick test for anything that looks numeric: would you ever add two of them together? If the answer is no, it is probably not a number, whatever it looks like on the form.
Match each value to the type that fits
- How many of an item are in stock
- The price of one item
- Whether an order has been paid
- A customer telephone number
- a whole number you will count with and subtract from, so an integer
- a number that needs a fractional part, so a real
- a value with only two possible states, so a Boolean
- a sequence you never do arithmetic on and whose leading zero must survive, so a string
Why the phone number is not a number
A form stores a customer telephone number. Which is the best reason for holding it as a string rather than as an integer?
- A zero at the front has to be kept, and no arithmetic is ever done on the value
- Telephone numbers are too long to fit inside an integer
- Strings can be searched faster than integers
- A telephone number sometimes contains a decimal point
The four casts, and the one that fails
Your specification names the conversion functions exactly, and getting the names right is free marks. str() converts a value to text. int() converts it to a whole number. float() converts it to a number with a fractional part, and you will also see this written as real(). bool() converts it to true or false.
⚠️ Now the fact that explains more bugs than anything else on this topic. WHAT COMES BACK FROM A KEYBOARD IS TEXT. A value the user typed arrives as a string, even when they typed digits. So if you read two values and add them without converting, you do not get a sum: you get the two pieces of text joined end to end. "7" and "3" give you "73", not 10. Cast first, then do arithmetic.
⚠️ And casting is not always possible. int("hello") has no sensible answer, because there is no whole number that text was standing for. A program that assumes every input will convert breaks the first time somebody types a word into a box that wanted a number, which is exactly the sort of thing the defensive design topic is about.
True about casting
Select the TWO statements that are true.
- A value read in from a keyboard arrives as text until it is converted
- Converting text that does not represent a number can fail, since there is no sensible result
- Using
str()on the value 5 gives back an integer - Every string can safely be converted to an integer
Complete the data type paragraph
A whole number with no fractional part is an _____. A number that can carry a fractional part is a _____. A value that can only be true or false is a _____. A single symbol on its own is a _____, while a sequence of symbols is a _____.
Name the cast
Five in a row, three lives. Your specification names these functions exactly, so learn the exact names.
How many characters is that
A password box holds the text open24. Counting every symbol in it, including the digits, how many characters does that string contain?
Spot the true data type facts
Tap the TWO statements that are true.
- A telephone number is held as a string because the zero at the front has to survive
- A value read in from the keyboard arrives as text until it is converted
- A character and a string are two names for the same thing
- A Boolean can hold any one of several different values
Three type decisions
Three situations. Choose the answer that gives a reason as well as a type.
- A shop system needs to record how many of an item are in stock. Which type, and on what grounds?
- A form asks for a house number, and some addresses on the street are numbered like 221B. What should the field hold?
- A program reads a number typed by the user and adds 10 to it, but the answer is wrong every single time. What is most likely wrong?
Choose and justify three types
A friend is building a simple shop program and has decided to store everything as text because "it all displays the same anyway". Write the answer that puts them right.
- Name the five data types this specification uses and say what each one holds
- Choose a type for a stock count and justify it by what the program does with the value
- Choose a type for a telephone number and justify it, saying what would go wrong with the alternative
- Name the casting functions and say what each one converts a value to
- Finish by explaining why a value typed in at the keyboard usually has to be converted before any arithmetic