Records: fields with different data types

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

Records: fields with different data types

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which best describes a record?
A A structure that groups related fields, which can have different data types, into one item
B A structure that can only hold values that all share the same data type
2. A student record has the fields name, age and enrolled. Which set of data types fits best?
A name: Integer, age: Boolean, enrolled: String
B name: String, age: Integer, enrolled: Boolean
3. In a data dictionary or record design, what does each field need?
A Only a value, because the data type is never needed
B A field name and a data type
4. How many things should one record describe?
A One entity, such as a single student
B Several different entities mixed together, such as a student, a teacher and a product
5. You want to store 30 students, each with a name, age and enrolled status. What is the best structure?
A An array of records, with one record per student
B One single record holding all 30 students' names in the name field

Match the Pairs

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

Left columnRight column
1RECORD Student: name AS String, age AS Integer, enrolled AS Boolean AThe values do not match the field data types. age should be the Integer 16 and enrolled should be the Boolean True.
2students[2].name Bs1 <- Student("Ava", 16, True)
3One record describes one entity, so each field holds one fact about that same student CThe fields have no data types. Each field needs a name and a data type, such as name AS String and age AS Integer.
4An array of records is like a table: each record is a row and each field is a column DReads the name field from the third record in an array of records
5RECORD Student: name, age, enrolled EDefines a record type with three fields, each with its own data type
6Student("Ava", "sixteen", "yes") Fstudents = [Student("Ava", 16, True), Student("Liam", 17, False)]

Definitions

record

Fill in the Blanks

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

1.
Record Student name : ______ age : Integer
2.
______ Student name : String age : Integer
3.
Record Student age : Integer enrolled : _______
4.
students ← Array of _______, size 30 students[1].name ← "Ava"
5.
students[0].name ← "Ava" students[0].age ← 16 print students[0].___

Spot the Error

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

Question 1
A RECORD Student
B name AS Integer
C age AS Integer
D enrolled AS Boolean
E END RECORD
Question 2
A student = {"name" = "Ava",
B "age": 16,
C "enrolled": True}
Question 3
A student = {}
B student["name"] = "Ava"
C student["age"] = "16"
D print(student["age"] + 1)
Question 4
A p = {"name": "Rex", "age": 3}
B print(p[name])
C print(p["age"] + 1)
Question 5
A table = {}
B r = {"name": "Zoe", "age": 15}
C table.append(r)
D print(len(table))

Answer Key

Records: fields with different data types
Section A: Quick Questions
Q1
A — A structure that groups related fields, which can have different data types, into one item
Q2
B — name: String, age: Integer, enrolled: Boolean
Q3
B — A field name and a data type
Q4
A — One entity, such as a single student
Q5
A — An array of records, with one record per student
Section B: Match the Pairs
Q1
RECORD Student: name AS String, age AS Integer, enrolled AS Boolean → Defines a record type with three fields, each with its own data type
Q2
students[2].name → Reads the name field from the third record in an array of records
Q3
One record describes one entity, so each field holds one fact about that same student → s1 <- Student("Ava", 16, True)
Q4
An array of records is like a table: each record is a row and each field is a column → students = [Student("Ava", 16, True), Student("Liam", 17, False)]
Q5
RECORD Student: name, age, enrolled → The fields have no data types. Each field needs a name and a data type, such as name AS String and age AS Integer.
Q6
Student("Ava", "sixteen", "yes") → The values do not match the field data types. age should be the Integer 16 and enrolled should be the Boolean True.
Section C: Definitions
Q1
record — A data structure that groups related fields of different data types into one item describing one thing, e.g. a student's name (String), age (Integer) and enrolled (Boolean).
Section D: Fill in the Blanks
Q1
Record Student name : String age : Integer
Q2
Record Student name : String age : Integer
Q3
Record Student age : Integer enrolled : Boolean
Q4
students ← Array of Student, size 30 students[1].name ← "Ava"
Q5
students[0].name ← "Ava" students[0].age ← 16 print students[0].age
Section E: Spot the Error
Q1
B — A student's name is text, so the field must have the data type String, not Integer. It should be: name AS String.
Q2
A — In a Python dictionary record, each field name is joined to its value with a colon, not an equals sign. It should be: student = {"name": "Ava",
Q3
C — The age field should be an Integer, but the quotes make it a String. Then age + 1 would cause a type error. It should be: student["age"] = 16
Q4
B — A field is accessed using its field name as a string in quotes. Without quotes, Python looks for a variable called name. It should be: print(p["name"])
Q5
A — An array of records used as a table must be a list so rows can be added with append(). A dictionary has no append(). It should be: table = []