Back to the topic

The mark scheme starts on a new page, so you can print pages one and two for a student and keep the last page yourself. Everything prints in black on white whichever theme you are using.

GCSE Computer Science · 2.2.2

Data Types and Casting

Section A: 10 multiple choice questions, 10 marks. Section B: 5 written questions, 17 marks. Total 27 marks.

Name

Class

Date

Mark  / 27

Section A

Circle the letter of the correct answer. One mark each.

  1. 1Which data type should be used to store whether a light is switched on?1
    1. AString
    2. BInteger
    3. CReal
    4. DBoolean
  2. 2Why should a phone number be stored as a string rather than an integer?1
    1. ALeading zeros would be lost and no arithmetic is performed on it
    2. BStrings use less memory
    3. CIntegers cannot store more than four digits
    4. DPhone numbers are too long for an integer
  3. 3What does int("57") produce?1
    1. AAn error
    2. BThe string "57"
    3. CThe real 57.0
    4. DThe integer 57
  4. 4A program uses a = input() and b = input(), then print(a + b). The user types 4 and 6. What is printed?1
    1. AAn error
    2. B46
    3. C10
    4. D24
  5. 5Which data type is most suitable for a product price of 9.99?1
    1. AInteger
    2. BCharacter
    3. CBoolean
    4. DReal
  6. 6What is casting?1
    1. AConverting a value from one data type to another
    2. BRemoving a variable from memory
    3. CCopying a value into a new variable
    4. DDeclaring a variable for the first time
  7. 7Which is a valid reason for choosing the correct data type?1
    1. AIt makes the program run at a higher clock speed
    2. BIt converts the program into machine code
    3. CIt reduces memory use and ensures the right operations can be performed
    4. DIt removes the need for validation
  8. 8How many possible values can a Boolean hold?1
    1. A1
    2. B2
    3. C8
    4. D256
  9. 9In Python, what is needed to print "Score: " + score where score is an integer?1
    1. ACast score to a float
    2. BRemove the quotation marks
    3. CCast score to a string using str()
    4. DNothing, it works as written
  10. 10A student's exam mark out of 100 with no decimals should be stored as:1
    1. AA string
    2. BAn integer
    3. CA Boolean
    4. DA real

Section B

Answer in the space provided. The number of marks tells you how many separate points to make.

  1. 1StateState the most appropriate data type for each of the following and justify one of your choices: a customer's surname, whether an order has been dispatched, the total price of an order.4
  2. 2ExplainA program asks the user for two numbers and adds them together, but when the user enters 12 and 5 the program outputs 125. Explain why this happens and how the programmer could correct it.3
  3. 3ExplainExplain two reasons why choosing an appropriate data type is important when writing a program.4
  4. 4DefineDefine the term casting and give an example of when it would be needed.3
  5. 5ExplainA programmer stores a student's ID number, which always begins with a zero, as an integer. Explain the problem this causes and state what should be used instead.3

Mark scheme

Section A

1D2A3D4B5D6A7C8B9C10B

Section B

1 State the most appropriate data type for each of the following and justify one of your choices: a customer's surname, whether an order has been dispatched, the total price of an order. 4

One mark for each of the following, to a maximum of 4.

  • Surname is a string
  • Dispatched is a Boolean
  • Total price is a real
  • Gives a valid justification for one choice

Model answer. A customer's surname should be stored as a string, because it is a sequence of characters and no arithmetic is ever performed on it. Whether an order has been dispatched should be stored as a Boolean, because there are only two possible states, dispatched or not dispatched, and a Boolean uses the least memory of any type while making the meaning of the value completely clear. The total price of an order should be stored as a real, because prices include pence and therefore require a fractional part that an integer could not represent.

2 A program asks the user for two numbers and adds them together, but when the user enters 12 and 5 the program outputs 125. Explain why this happens and how the programmer could correct it. 3

One mark for each of the following, to a maximum of 3.

  • The input function returns a string
  • The plus operator concatenates strings rather than adding numbers
  • Cast the inputs to integers using int() before adding

Model answer. The problem is that the input function always returns a string, even when the user types digits, so the variables hold the strings "12" and "5" rather than the numbers 12 and 5. When the plus operator is applied to two strings it concatenates them, joining them end to end, which produces "125" rather than performing addition. The programmer should cast each input to an integer as it is read, for example using number1 = int(input(...)), so that the values are stored as integers and the plus operator then performs numerical addition, producing 17.

3 Explain two reasons why choosing an appropriate data type is important when writing a program. 4

One mark for each of the following, to a maximum of 4.

  • Different types use different amounts of memory
  • Choosing the smallest suitable type saves memory, which matters across large amounts of data
  • The data type determines which operations are valid
  • Using the wrong type causes errors or incorrect results

Model answer. The first reason is memory efficiency. Different data types require different amounts of storage, with a Boolean needing very little and a real needing considerably more than an integer, so choosing the smallest type that can hold the data reduces the memory the program uses. Across a database holding millions of records, or on an embedded device with very limited memory, that difference is significant. The second reason is that the data type determines which operations can validly be carried out on a value. Arithmetic can be performed on integers and reals but not on strings, and concatenation applies to strings but not to numbers, so storing a value with the wrong type either causes the program to raise an error or, worse, produces a plausible but incorrect result, such as two numbers being joined together instead of added.

4 Define the term casting and give an example of when it would be needed. 3

One mark for each of the following, to a maximum of 3.

  • Casting is converting a value from one data type to another
  • Gives a valid example such as converting user input to an integer
  • Explains why it is needed in that example

Model answer. Casting is the process of converting a value from one data type into another, for example turning the string "25" into the integer 25. It is needed whenever a value is held as one type but must be used as another. A common example is reading a number from the user: the input function returns a string, so before the program can perform any arithmetic or numerical comparison on that value it must be cast using int() or float(), otherwise the program will either produce an error or treat the value as text and concatenate it rather than adding it.

5 A programmer stores a student's ID number, which always begins with a zero, as an integer. Explain the problem this causes and state what should be used instead. 3

One mark for each of the following, to a maximum of 3.

  • An integer does not preserve leading zeros
  • The ID would be stored or displayed incorrectly
  • A string should be used instead

Model answer. Storing the ID as an integer causes the leading zero to be lost, because numerically 0451 and 451 are the same value and an integer stores only the numeric value with no record of how it was written. This means the ID would be displayed and stored as 451, which does not match the student's actual ID and would cause lookups against records held elsewhere to fail. The ID should be stored as a string instead, which preserves every character exactly as entered, and this is appropriate because no arithmetic is ever performed on an ID number.