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.

Python · GCSE 2

Arrays, Records and Files

Section A: 10 multiple choice questions, 10 marks. Section B: 5 written questions, 16 marks. Total 26 marks.

Name

Class

Date

Mark  / 26

Section A

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

  1. 1What is the index of the first item in an array?1
    1. A1
    2. B-1
    3. C0
    4. DIt depends on the size of the array
  2. 2names = ["Ann", "Bo", "Cy"]. What happens when names[3] runs?1
    1. AAn IndexError
    2. BIt returns "Cy"
    3. CIt adds a fourth item
    4. DIt returns None
  3. 3Which line builds a 3 row by 4 column grid of zeros with three separate rows?1
    1. A[[0] * 4] * 3
    2. B[[0] * 4 for row in range(3)]
    3. C[[0 for col in range(3)] for row in range(4)]
    4. D[[0 for col in range(4)] * 3]
  4. 4In OCR reference language grid[1,2] is row 1, column 2. How is it written in Python?1
    1. Agrid[1][2]
    2. Bgrid[2][1]
    3. Cgrid[1, 2]
    4. Dgrid(1, 2)
  5. 5Two parallel arrays hold names and marks. What makes them work?1
    1. AThey must be sorted
    2. BOne must hold only text and the other only numbers
    3. CThe same index in each array refers to the same person
    4. DThey must have different lengths
  6. 6What is a record?1
    1. AA set of related fields about one thing, which can be different data types
    2. BA numbered list of values that must all be the same data type
    3. CA stored file of text that a program reads one line at a time
    4. DA value that is stored in one variable for the whole program
  7. 7Which file mode erases the existing contents of the file?1
    1. A"a"
    2. B"r"
    3. C"w"
    4. DNone of them
  8. 8A line read from a file is "Asha,56\n". Which method removes the newline?1
    1. Asplit()
    2. Bupper()
    3. Cclose()
    4. Dstrip()
  9. 9students is a list of dictionaries. Which expression is the mark of the third student?1
    1. Astudents[3]["mark"]
    2. Bstudents[2]["mark"]
    3. Cstudents.mark[2]
    4. Dstudents["mark"][2]
  10. 10What is the main advantage of using with open(...) as file?1
    1. AThe file is made read only so it cannot be changed by accident
    2. BThe file can hold more data than one opened the normal way
    3. CThe file opens faster because Python skips the usual checks
    4. DThe file is closed automatically when the block ends

Section B

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

  1. 1GiveGive two differences between an array and a record.2
  2. 2WriteA two dimensional array named marks stores the results of 5 students in 3 tests, one student on each row. Write a program that outputs the total of each student's three marks.4
  3. 3WriteA text file called scores.txt holds one whole number on each line. Write a program that reads the file and outputs the highest number in it.4
  4. 4DescribeA program stores student names in one array and their marks in a second array. Describe one problem with parallel arrays and explain how using records solves it.3
  5. 5DescribeDescribe the difference between opening a file in write mode and in append mode.3

Mark scheme

Section A

1C2A3B4A5C6A7C8D9B10D

Section B

1 Give two differences between an array and a record. 2

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

  • An array holds items that are all the same data type, a record can hold different data types
  • An array is accessed by a numeric index, a record by the name of a field
  • An array holds many values of one kind, a record holds the facts about one item

Model answer. An array stores a number of items that must all be the same data type and each is found by its numeric position. A record stores a set of related fields about one thing, and the fields can be different data types, such as a string for a name and an integer for an age, each found by the name of the field.

2 A two dimensional array named marks stores the results of 5 students in 3 tests, one student on each row. Write a program that outputs the total of each student's three marks. 4

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

  • Outer loop that repeats once for each of the 5 students
  • Sets the total to 0 inside the outer loop, before the inner loop
  • Inner loop over the 3 tests adding marks[student][test] to the total
  • Outputs the total after the inner loop, once per student

Model answer.

The outer loop picks a student, the inner loop adds up that student's three tests, and the total is printed once the inner loop has finished.

Python
marks = [[56, 61, 70], [72, 68, 75], [38, 45, 40], [90, 85, 94], [60, 60, 62]]   # given in the question

for student in range(5):
    total = 0
    for test in range(3):
        total = total + marks[student][test]
    print(total)

3 A text file called scores.txt holds one whole number on each line. Write a program that reads the file and outputs the highest number in it. 4

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

  • Opens the file for reading and closes it
  • Reads each line using a loop until the end of the file
  • Converts each line to an integer
  • Compares with the highest so far, updates it, and outputs it after the loop

Model answer.

Everything read from a file is text, so each line is cast to an integer before it is compared. The highest value is output once the whole file has been read.

Python
file = open("scores.txt", "w")        # set up the file the question describes
file.write("56\n71\n38\n90\n64\n")
file.close()

file = open("scores.txt", "r")
highest = int(file.readline())
for line in file:
    number = int(line)
    if number > highest:
        highest = number
file.close()
print(highest)

4 A program stores student names in one array and their marks in a second array. Describe one problem with parallel arrays and explain how using records solves it. 3

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

  • The arrays are only linked by position
  • If one array is changed or sorted and the other is not, the data no longer matches
  • A record keeps all the fields for one student together so they cannot separate

Model answer. In parallel arrays the only thing connecting a name to a mark is that they sit at the same index. If the names are sorted or one item is deleted but the marks array is not changed in the same way, the data goes out of step and a mark is attached to the wrong student. With a record, the name and the mark are fields of one record, so they are always kept together and move as one unit.

5 Describe the difference between opening a file in write mode and in append mode. 3

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

  • Write mode replaces any existing contents of the file
  • Append mode keeps the existing contents
  • Append mode adds new data to the end of the file

Model answer. Opening a file in write mode removes everything that was already stored in it and starts again from an empty file, so only the newly written data remains. Opening it in append mode keeps everything that is already there, and any new data is added after it, at the end of the file.