AQA 8525 GCSE Computer Science · 3.2 Programming

Stretch tasks Data Structures, Strings and Files

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

Task 4

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

  1. 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
  2. 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
  3. scores ← [14, 9, 21, 6, 18]. What is scores[2]?

  4. A five-item array has a last index of 5.

    • True
    • False
  5. board ← [[1, 0, 0], [0, 1, 0], [0, 0, 1]]. What is board[1][1]?

  6. What is output?

    OUTPUT POSITION('Computing', 'p')

  7. What is output?

    OUTPUT SUBSTRING(0, 2, 'Computing')

  8. What is output?

    OUTPUT CHAR_TO_CODE('A')

  9. What does STRING_TO_INT('25') give?

    • The character code of 2
    • An error
    • The integer 25
    • The string '25'
  10. 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
  11. Data read from a text file arrives as a string.

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

  1. Explain the difference between an array and a record. [2 marks]

  2. Explain why looping FOR i ← 0 TO LEN(scores) reads past the end of the array. [2 marks]

  3. Explain how CHAR_TO_CODE and CODE_TO_CHAR can turn 'B' into 'b'. [2 marks]

  4. Explain why numbers read from a text file must be changed before doing arithmetic with them. [2 marks]

Quiz Challenge, round 3: Big Think

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

  2. Write an algorithm that displays every line of the text file results.txt. [4 marks]

This or That: tick the right answer

  1. Which holds values of different types?

    • An array
    • A record
  2. In 'Computing', which index holds the g?

    • 9
    • 8
  3. What is output?

    OUTPUT CODE_TO_CHAR(66)
    • B
    • A
  4. What do you get by concatenating 'Comp' and 'uting'?

    • 'Comp uting'
    • 'Computing'
  5. What happens if a program reads past the end of a file?

    • It starts again from the top
    • An error
  6. The same seed is used twice. What do you get?

    • The same random sequence
    • A different random sequence
  7. What does board[2][0] pick?

    • The first item in row 2
    • Column 2 of row 0
  8. How far apart are the codes for A and a?

    • 26
    • 32

Memory Match: match each operation to its result

OperationResult
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?

StatementArrayRecordText 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

  1. Many values of the same type under one name, found by index One loop can then handle any number of items.
  2. Several related values of different types, each in a named field For example, a student's name, year and average, kept together.
  3. 21 Indexing starts at 0: 14 is at 0, 9 at 1, 21 at 2.
  4. False Its indexes are 0 to 4. LEN is 5, but the last index is LEN - 1.
  5. 1 Row 1 is [0, 1, 0], and position 1 in it is 1.
  6. 3 C is 0, o is 1, m is 2 and p is 3.
  7. Com Indexes 0, 1 and 2, both ends included.
  8. 65 Characters are stored as numbers: A is 65.
  9. The integer 25 It converts the string to a number you can do arithmetic with.
  10. Buffered output may not be written otherwise Output is often held in a buffer until the file is closed.
  11. True Numbers must be converted before arithmetic.
  12. 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 answer

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

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

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

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

    Each 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 answer
    AQA pseudo-code
    results ← OPEN('results.txt')
    line ← results.readLine()
    WHILE NOT results.endOfFile()
        OUTPUT line
        line ← results.readLine()
    ENDWHILE
    results.close()

This or That

  1. A record An array holds many of the same thing. A record holds the different things about one thing.
  2. 8 There are 9 characters, so the indexes run from 0 to 8.
  3. B A is 65, so 66 is B. Lower case b would be 98.
  4. 'Computing' Concatenation joins strings end to end, adding nothing between them.
  5. An error That is why the loop must test for the end of the file.
  6. The same random sequence Pseudo-random numbers are fully determined by the seed.
  7. The first item in row 2 The first index chooses the row, the second the position in it.
  8. 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

  1. Array Every item is found by an index counted from 0 Arrays are indexed from 0.
  2. Text file Read line by line until endOfFile() is true Reading past the end of a file is an error.
  3. Text file Everything read from it arrives as a string Text files hold text, so numbers must be converted.
  4. Record student.name ← 'Amira' Setting the name field of a record.
  5. Array scores[3] ← 11 Changing the item at index 3 of an array.
  6. Record Several fields of different types about one thing That is the definition of a record.
  7. Array Many values of the same type under one name That is the definition of an array.
  8. Array A grid, such as a seating plan A two-dimensional array is an array of arrays.
  9. Text file Keeps data after the program closes Writing to a file makes data persist.
  10. Text file Must be opened before use and closed afterwards Files are opened and closed.
  11. Record A name, a year group and an average, kept together Different types for one student: a record.