OCR J277 GCSE Computer Science · 2.2 Programming Fundamentals

Stretch tasks Data Types and Casting

Task 1

Quiz Challenge

A quiz game against four computer players, then short and long written answers.

  1. Quiz Blitz12 questions against the clock
  2. Quick Fire4 short answers
  3. Big Think2 long answers

About 20 minutes

Task 2

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

Task 3

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

Task 4

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

  1. Which data type suits a person's age in years?

    • Integer
    • Real
    • String
    • Boolean
  2. Which data type suits a phone number?

    • Integer
    • Real
    • Boolean
    • String
  3. Which data type suits whether a user is logged in?

    • String
    • Integer
    • Character
    • Boolean
  4. input() returns an integer when the user types a number.

    • True
    • False
  5. The user types 5, then 3. What does this print?

    a = input("First: ")
    b = input("Second: ")
    print(a + b)

  6. What is casting?

    • Converting a value from one data type to another
    • Copying a variable
    • Renaming a variable
    • Storing a value in a file
  7. Which conversion makes this line work?

    score = 42
    print("Your score is " + score)
    • float(score)
    • bool(score)
    • str(score)
    • int(score)
  8. Which TWO should be stored as strings?

    Tick 2.

    • A phone number
    • An age
    • A price
    • A number of goals
    • A postcode
  9. What does int("7") + 2 give?

  10. Which of these is a real (float) value?

    • 42
    • "3.14"
    • True
    • 3.14
  11. A Boolean can only be True or False.

    • True
    • False
  12. 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

  1. State the most suitable data type for a price and for a postcode. [2 marks]

  2. Explain why a phone number should be stored as a string, not an integer. [2 marks]

  3. Explain why values from input() must often be cast before they are used. [2 marks]

  4. Give two reasons why choosing the right data type matters. [2 marks]

Quiz Challenge, round 3: Big Think

  1. 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]

  2. 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

  1. Which data type is 'A'?

    • Boolean
    • Character
  2. What is str(42)?

    • The integer 42
    • The string "42"
  3. What does float("19.99") give?

    • The string "19.99"
    • The real number 19.99
  4. Which uses less memory?

    • An integer
    • A real
  5. What is bool(1)?

    • False
    • True
  6. To join a score to text with +, cast the score to...

    • An integer
    • A string
  7. Which type is "" (two quote marks with nothing between)?

    • An empty string
    • A Boolean
  8. 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

TermMeaning
A Integer1 A sequence of characters
B Real2 A whole number, such as -7
C Boolean3 A number with a decimal part
D Character4 True or False only
E String5 Converting a value to another type
F Casting6 One single symbol, such as ?

Sort It: tick one box in each row

Which data type suits each value?

StatementIntegerRealBooleanString
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

  1. Integer Ages in years are whole numbers.
  2. String It starts with 0, which an integer would drop, and you never do arithmetic on it.
  3. Boolean Only two states: True or False.
  4. False input() always returns a string, even when the user types digits.
  5. 53 Both are strings, so + joins them: "5" + "3" is "53".
  6. Converting a value from one data type to another int(), float(), str() and bool() all cast a value to a new type.
  7. str(score) To join a number to text with +, it must be cast to a string first.
  8. A postcode; A phone number Postcodes contain letters, and phone numbers start with 0. Neither is used in arithmetic.
  9. 9 int("7") is the number 7, and 7 + 2 = 9.
  10. 3.14 42 is an integer, "3.14" in quotes is a string, and True is a Boolean.
  11. True Exactly two values: never yes, no or maybe.
  12. 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 answer

    A 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 answer

    A 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 answer

    input() 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 answer

    An 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 answer

    input() 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 answer

    The 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

  1. Character A single character, such as A, 7 or ?, is the character type.
  2. The string "42" str() casts a value to a string.
  3. The real number 19.99 float() casts the string to a real number you can calculate with.
  4. An integer Across millions of records, choosing integers where you can saves real space.
  5. True 1 becomes True, and 0 becomes False.
  6. A string You cannot join a number to text, so use str(score).
  7. An empty string It is a string with no characters in it.
  8. 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

  1. Boolean Whether a user has paid Yes or no is a Boolean.
  2. Boolean Whether a door is locked Only two states: True or False.
  3. Real A height in metres, such as 1.62 Heights need decimals.
  4. Real A price such as 4.99 A price needs a decimal part for the pence.
  5. Integer A person's age in years Ages in years are whole numbers.
  6. Integer The number of pupils in a class You cannot have half a pupil.
  7. String A phone number such as 07700 900123 It starts with 0 and contains a space, and it is never added up.
  8. Real A temperature such as -3.5 It has a decimal part, so it is a real.
  9. String A username A username is a sequence of characters.
  10. Integer The number of goals scored Goals are whole numbers.
  11. String A postcode such as SW1A 1AA It contains letters, digits and a space.