OCR J277 GCSE Computer Science · 2.2 Programming Fundamentals
Stretch tasks Data Types and Casting
Quiz Challenge
A quiz game against four computer players, then short and long written answers.
- Quiz Blitz12 questions against the clock
- Quick Fire4 short answers
- Big Think2 long answers
About 20 minutes
This or That
Pixel the cat asks 8 questions. Each has two answers: pick the right one.
- Two answers to every question
- A fish treat for every right answer
- Streaks win Pixel shades, then a party hat
About 4 minutes
Memory Match
Flip the cards two at a time to match each term card to its meaning card.
- 12 cards: each term has a meaning
- One peek at every card
- Combo points for pairs found in a row
6 pairs, about 4 minutes
Sort It
Sort each statement into the right box, like the tick-box tables in the exam.
- 11 cards to sort into 4 boxes
- Drag a card, tap a box or press its number
- Combo points for cards sorted in a row
11 cards, about 4 minutes
The games need JavaScript. Every question is below, with the answers at the end.
Read the lesson: Data Types and Casting
Data Types and Casting: the questions
Quiz Challenge, round 1: Quiz Blitz
Which data type suits a person's age in years?
- Integer
- Real
- String
- Boolean
Which data type suits a phone number?
- Integer
- Real
- Boolean
- String
Which data type suits whether a user is logged in?
- String
- Integer
- Character
- Boolean
input() returns an integer when the user types a number.
- True
- False
The user types 5, then 3. What does this print?
a = input("First: ") b = input("Second: ") print(a + b)What is casting?
- Converting a value from one data type to another
- Copying a variable
- Renaming a variable
- Storing a value in a file
Which conversion makes this line work?
score = 42 print("Your score is " + score)float(score)bool(score)str(score)int(score)
Which TWO should be stored as strings?
Tick 2.
- A phone number
- An age
- A price
- A number of goals
- A postcode
What does int("7") + 2 give?
Which of these is a real (float) value?
- 42
- "3.14"
- True
- 3.14
A Boolean can only be True or False.
- True
- False
Why is money sometimes stored as a whole number of pence?
- Pence cannot be negative
- Strings are faster to add up
- Reals are approximations, which can cause rounding errors
- Integers can hold letters
Quiz Challenge, round 2: Quick Fire
State the most suitable data type for a price and for a postcode. [2 marks]
Explain why a phone number should be stored as a string, not an integer. [2 marks]
Explain why values from input() must often be cast before they are used. [2 marks]
Give two reasons why choosing the right data type matters. [2 marks]
Quiz Challenge, round 3: Big Think
A program adds two numbers typed by the user, but prints 53 when the user types 5 and 3. Explain why, and how to fix it. [4 marks]
A sports club stores each member's ID, name, phone number, membership fee, and whether the fee is paid. Choose a suitable data type for four of these and justify each choice. [6 marks]
This or That: tick the right answer
Which data type is 'A'?
- Boolean
- Character
What is str(42)?
- The integer 42
- The string "42"
What does float("19.99") give?
- The string "19.99"
- The real number 19.99
Which uses less memory?
- An integer
- A real
What is bool(1)?
- False
- True
To join a score to text with +, cast the score to...
- An integer
- A string
Which type is "" (two quote marks with nothing between)?
- An empty string
- A Boolean
A price is stored as an integer number of pounds. What goes wrong?
- It cannot be printed
- The pence are lost
Memory Match: match each term to its meaning
| Term | Meaning |
|---|---|
| A Integer | 1 A sequence of characters |
| B Real | 2 A whole number, such as -7 |
| C Boolean | 3 A number with a decimal part |
| D Character | 4 True or False only |
| E String | 5 Converting a value to another type |
| F Casting | 6 One single symbol, such as ? |
Sort It: tick one box in each row
Which data type suits each value?
| Statement | Integer | Real | Boolean | String |
|---|---|---|---|---|
| Whether a user has paid | ||||
| Whether a door is locked | ||||
| A height in metres, such as 1.62 | ||||
| A price such as 4.99 | ||||
| A person's age in years | ||||
| The number of pupils in a class | ||||
| A phone number such as 07700 900123 | ||||
| A temperature such as -3.5 | ||||
| A username | ||||
| The number of goals scored | ||||
| A postcode such as SW1A 1AA |
Answers
Round 1: Quiz Blitz
- Integer Ages in years are whole numbers.
- String It starts with 0, which an integer would drop, and you never do arithmetic on it.
- Boolean Only two states: True or False.
- False input() always returns a string, even when the user types digits.
- 53 Both are strings, so + joins them: "5" + "3" is "53".
- Converting a value from one data type to another int(), float(), str() and bool() all cast a value to a new type.
str(score)To join a number to text with +, it must be cast to a string first.- A postcode; A phone number Postcodes contain letters, and phone numbers start with 0. Neither is used in arithmetic.
- 9 int("7") is the number 7, and 7 + 2 = 9.
- 3.14 42 is an integer, "3.14" in quotes is a string, and True is a Boolean.
- True Exactly two values: never yes, no or maybe.
- Reals are approximations, which can cause rounding errors Storing 499 pence avoids the tiny rounding errors of storing 4.99.
Round 2: Quick Fire
- Price: real
- Postcode: string
Model answerA price should be a real, because it needs pence. A postcode should be a string, because it contains letters.
- An integer would lose the leading zero
- You never do arithmetic on it, and it may contain spaces
Model answerA phone number starts with 0, which an integer would drop. You also never do arithmetic on a phone number, and it may contain spaces.
- input() always returns a string
- So it must be cast to an integer or real for arithmetic or numeric comparison
Model answerinput() always returns a string, even when the user types digits. It must be cast with int() or float() before it is used in arithmetic or a numeric comparison.
- Memory: some types use less
- Operations: the wrong type causes errors
- Validation: wrong input can be rejected
- Accuracy: reals are approximations
Model answerAn integer uses less memory than a real, which matters across millions of records. Using the wrong type causes errors, because you cannot do arithmetic on strings.
Round 3: Big Think
- input() returns strings
- + joins strings together: concatenation
- Fix: cast each input to an integer
- Then + adds them, giving 8
Model answerinput() always returns a string, so the program has the strings "5" and "3". The + operator joins strings together, which is concatenation, so it prints 53. The fix is to cast each input to an integer with int(), for example a = int(input("First number: ")). Then + adds the numbers and prints 8.
- Member ID: integer
- Name: string
- Phone number: string, because of the leading 0
- You never do arithmetic on a phone number
- Fee: real
- Because the fee needs pence
- Paid: Boolean
- Because it only has two values
Model answerThe member ID is an integer, because it is a whole number. The name is a string, because it is a sequence of characters. The phone number is a string, because it starts with 0, which an integer would lose, and you never do arithmetic on it. The membership fee is a real, because it needs pence after the decimal point. Whether the fee is paid is a Boolean, because it has only two values: true or false.
This or That
- Character A single character, such as A, 7 or ?, is the character type.
- The string "42" str() casts a value to a string.
- The real number 19.99 float() casts the string to a real number you can calculate with.
- An integer Across millions of records, choosing integers where you can saves real space.
- True 1 becomes True, and 0 becomes False.
- A string You cannot join a number to text, so use str(score).
- An empty string It is a string with no characters in it.
- The pence are lost An integer has no decimal part, so 4.99 cannot be stored as pounds.
Memory Match
- A 2 Integer: A whole number, such as -7
- B 3 Real: A number with a decimal part
- C 4 Boolean: True or False only
- D 6 Character: One single symbol, such as ?
- E 1 String: A sequence of characters
- F 5 Casting: Converting a value to another type
Sort It
- Boolean Whether a user has paid Yes or no is a Boolean.
- Boolean Whether a door is locked Only two states: True or False.
- Real A height in metres, such as 1.62 Heights need decimals.
- Real A price such as 4.99 A price needs a decimal part for the pence.
- Integer A person's age in years Ages in years are whole numbers.
- Integer The number of pupils in a class You cannot have half a pupil.
- String A phone number such as 07700 900123 It starts with 0 and contains a space, and it is never added up.
- Real A temperature such as -3.5 It has a decimal part, so it is a real.
- String A username A username is a sequence of characters.
- Integer The number of goals scored Goals are whole numbers.
- String A postcode such as SW1A 1AA It contains letters, digits and a space.