AQA 8525 GCSE Computer Science · 3.2 Programming
Stretch tasks Data Structures, Strings and Files
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 operation card to its result card.
- 12 cards: each operation has a result
- 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 3 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 Structures, Strings and Files
Data Structures, Strings and Files: the questions
Quiz Challenge, round 1: Quiz Blitz
What is an array?
- A single value that cannot change
- A file stored on disk
- Many values of the same type under one name, found by index
- Several values of different types about one thing
What is a record?
- Many values of the same type
- A loop that repeats
- A sorted list
- Several related values of different types, each in a named field
scores ← [14, 9, 21, 6, 18]. What is scores[2]?
A five-item array has a last index of 5.
- True
- False
board ← [[1, 0, 0], [0, 1, 0], [0, 0, 1]]. What is board[1][1]?
What is output?
OUTPUT POSITION('Computing', 'p')What is output?
OUTPUT SUBSTRING(0, 2, 'Computing')What is output?
OUTPUT CHAR_TO_CODE('A')What does STRING_TO_INT('25') give?
- The character code of 2
- An error
- The integer 25
- The string '25'
Why must a file be closed after use?
- To delete it
- To make it read only
- To sort its contents
- Buffered output may not be written otherwise
Data read from a text file arrives as a string.
- True
- False
Why are a computer's random numbers called pseudo-random?
- They are only between 1 and 6
- They are always even
- A person types them in
- They come from an algorithm and a seed, so they can be predicted
Quiz Challenge, round 2: Quick Fire
Explain the difference between an array and a record. [2 marks]
Explain why looping FOR i ← 0 TO LEN(scores) reads past the end of the array. [2 marks]
Explain how CHAR_TO_CODE and CODE_TO_CHAR can turn 'B' into 'b'. [2 marks]
Explain why numbers read from a text file must be changed before doing arithmetic with them. [2 marks]
Quiz Challenge, round 3: Big Think
A program stores each student's name, year group and average mark. Explain why an array of records is a suitable data structure. [4 marks]
Write an algorithm that displays every line of the text file results.txt. [4 marks]
This or That: tick the right answer
Which holds values of different types?
- An array
- A record
In 'Computing', which index holds the g?
- 9
- 8
What is output?
OUTPUT CODE_TO_CHAR(66)- B
- A
What do you get by concatenating 'Comp' and 'uting'?
- 'Comp uting'
- 'Computing'
What happens if a program reads past the end of a file?
- It starts again from the top
- An error
The same seed is used twice. What do you get?
- The same random sequence
- A different random sequence
What does board[2][0] pick?
- The first item in row 2
- Column 2 of row 0
How far apart are the codes for A and a?
- 26
- 32
Memory Match: match each operation to its result
| Operation | Result |
|---|---|
A LEN('Computing') | 1 65 |
B POSITION('Computing', 'p') | 2 9 |
C SUBSTRING(0, 2, 'Computing') | 3 '25' |
D CHAR_TO_CODE('A') | 4 3 |
E INT_TO_STRING(25) | 5 Computing |
F 'Comp' + 'uting' | 6 Com |
Sort It: tick one box in each row
Array, record or text file?
| Statement | Array | Record | Text file |
|---|---|---|---|
| Every item is found by an index counted from 0 | |||
| Read line by line until endOfFile() is true | |||
| Everything read from it arrives as a string | |||
student.name ← 'Amira' | |||
scores[3] ← 11 | |||
| Several fields of different types about one thing | |||
| Many values of the same type under one name | |||
| A grid, such as a seating plan | |||
| Keeps data after the program closes | |||
| Must be opened before use and closed afterwards | |||
| A name, a year group and an average, kept together |
Answers
Round 1: Quiz Blitz
- Many values of the same type under one name, found by index One loop can then handle any number of items.
- Several related values of different types, each in a named field For example, a student's name, year and average, kept together.
- 21 Indexing starts at 0: 14 is at 0, 9 at 1, 21 at 2.
- False Its indexes are 0 to 4. LEN is 5, but the last index is LEN - 1.
- 1 Row 1 is [0, 1, 0], and position 1 in it is 1.
- 3 C is 0, o is 1, m is 2 and p is 3.
- Com Indexes 0, 1 and 2, both ends included.
- 65 Characters are stored as numbers: A is 65.
- The integer 25 It converts the string to a number you can do arithmetic with.
- Buffered output may not be written otherwise Output is often held in a buffer until the file is closed.
- True Numbers must be converted before arithmetic.
- They come from an algorithm and a seed, so they can be predicted The same seed gives the same sequence every time.
Round 2: Quick Fire
- An array holds many values of the same type, found by index
- A record holds several values of different types, in named fields
Model answerAn array holds many values of the same type under one name, each found by its index. A record holds several related values of different types, each in a named field.
- LEN gives the number of items
- The last index is one less, because indexing starts at 0
Model answerLEN gives the number of items, but indexing starts at 0, so the last index is LEN(scores) - 1. Looping to LEN(scores) asks for one item too many.
- CHAR_TO_CODE('B') gives 66
- Add 32 to get 98, then CODE_TO_CHAR turns it back into a letter
Model answerCHAR_TO_CODE('B') gives 66. Lower case letters are always 32 further on, so 66 + 32 = 98, and CODE_TO_CHAR(98) gives 'b'.
- Everything read from a text file is a string
- So it must be cast to a number, for example with STRING_TO_INT
Model answerEverything read from a text file arrives as a string, so it must be cast to a number, for example with STRING_TO_INT, before any arithmetic.
Round 3: Big Think
- A record keeps one student's different values together in fields
- The fields are a string, an integer and a real
- An array holds one record per student, all the same type
- So a loop can process every student
Model answerEach student needs a name, a year group and an average mark, which are different types: a string, an integer and a real. A record can hold them together in named fields. An array then holds one record for each student, all of the same record type, so a FOR loop can go through every student by index.
- Opens the file
- Reads it with readLine
- Loops until the end of the file
- Closes the file at the end
Model answerAQA pseudo-coderesults ← OPEN('results.txt') line ← results.readLine() WHILE NOT results.endOfFile() OUTPUT line line ← results.readLine() ENDWHILE results.close()
This or That
- A record An array holds many of the same thing. A record holds the different things about one thing.
- 8 There are 9 characters, so the indexes run from 0 to 8.
- B A is 65, so 66 is B. Lower case b would be 98.
- 'Computing' Concatenation joins strings end to end, adding nothing between them.
- An error That is why the loop must test for the end of the file.
- The same random sequence Pseudo-random numbers are fully determined by the seed.
- The first item in row 2 The first index chooses the row, the second the position in it.
- 32 A is 65 and a is 97, and every letter pair is 32 apart.
Memory Match
- A 2
LEN('Computing'): 9 - B 4
POSITION('Computing', 'p'): 3 - C 6
SUBSTRING(0, 2, 'Computing'): Com - D 1
CHAR_TO_CODE('A'): 65 - E 3
INT_TO_STRING(25): '25' - F 5
'Comp' + 'uting': Computing
Sort It
- Array Every item is found by an index counted from 0 Arrays are indexed from 0.
- Text file Read line by line until endOfFile() is true Reading past the end of a file is an error.
- Text file Everything read from it arrives as a string Text files hold text, so numbers must be converted.
- Record
student.name ← 'Amira'Setting the name field of a record. - Array
scores[3] ← 11Changing the item at index 3 of an array. - Record Several fields of different types about one thing That is the definition of a record.
- Array Many values of the same type under one name That is the definition of an array.
- Array A grid, such as a seating plan A two-dimensional array is an array of arrays.
- Text file Keeps data after the program closes Writing to a file makes data persist.
- Text file Must be opened before use and closed afterwards Files are opened and closed.
- Record A name, a year group and an average, kept together Different types for one student: a record.