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.

Edexcel GCSE CS · 6.2

Lists, Strings and Files in Python

Section A: 10 multiple choice questions, 10 marks. Section B: 4 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. 1A list called names holds 8 items. What is the index of the last one?1
    1. A0
    2. B7
    3. C8
    4. D9
  2. 2What does scores[-1] return?1
    1. AAn error
    2. BThe first item
    3. CThe last item in the list
    4. DThe length of the list
  3. 3What does 'Computing'[0:3] give?1
    1. A'C'
    2. B'omp'
    3. C'Comp'
    4. D'Com'
  4. 4What does 'Amira,15'.split(',') return?1
    1. A['Amira', '15']
    2. B'Amira 15'
    3. C['Amira,15']
    4. D('Amira', 15)
  5. 5Why is strip() usually needed on a line read from a text file?1
    1. AIt splits the line into parts
    2. BIt converts the line to an integer
    3. CIt closes the file
    4. DIt removes the newline character at the end of the line
  6. 6What happens if a file is opened with mode 'w' when it already exists?1
    1. AAn error is raised
    2. BIts existing contents are deleted before anything is written
    3. CThe new data is added to the end
    4. DThe file is opened read only
  7. 7In a two dimensional list called grid, what does grid[2][0] refer to?1
    1. AAn error, since two indexes are not allowed
    2. BThe third item of the first row
    3. CThe first item of the third row
    4. DThe second row, first column, counting from one
  8. 8Which method adds an item to the end of a list?1
    1. Aappend
    2. Bextend
    3. Cadd
    4. Dinsert
  9. 9Why must a number read from a text file be converted before it is used in a calculation?1
    1. AThe file must be closed first
    2. BFiles store numbers in hexadecimal
    3. CEverything read from a text file is a string
    4. DPython cannot add two numbers from a file
  10. 10What does ord('A') return, given that 'A' has the code 65?1
    1. A65
    2. B97
    3. C1
    4. D'A'

Section B

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

  1. 1WriteA list called marks holds 30 integers. Write a Python program that outputs the highest mark.4
  2. 2ExplainExplain why a program that reads a name from a file and compares it with a name typed by the user might never find a match, even when the names look identical.3
  3. 3WriteA text file called results.txt has one record per line in the form name,score. Write a Python program that reads the file and outputs the name of every student who scored more than 50.5
  4. 4ExplainExplain the difference between opening a file in write mode and in append mode, and give one situation where each is appropriate.4

Mark scheme

Section A

1B2C3D4A5D6B7C8A9C10A

Section B

1 A list called marks holds 30 integers. Write a Python program that outputs the highest mark. 4

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

  • Sets an initial highest value from the list, such as marks[0]
  • Loops through the list
  • Compares each item with the current highest
  • Updates the highest and outputs it after the loop

Model answer. A correct program is: highest = marks[0], then for mark in marks: if mark > highest: highest = mark, then print("The highest mark is", highest). Starting from marks[0] rather than from zero matters, because a list containing only negative values would otherwise report zero as the highest. The print statement must be outside the loop, otherwise it outputs a running maximum on every pass rather than the final answer. Python's built in max(marks) would also be accepted.

2 Explain why a program that reads a name from a file and compares it with a name typed by the user might never find a match, even when the names look identical. 3

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

  • A line read from a file includes the newline character at the end
  • The typed name does not include that character, so the two strings differ
  • Using strip() removes it so the comparison works

Model answer. When a line is read from a text file it includes the newline character that ended the line in the file. The name typed by the user does not include that character, so although the two look identical when printed, one is a character longer than the other and the comparison is False. The fix is to remove the whitespace at the ends of the line read from the file, using name = line.strip(), after which the two strings genuinely are equal and the comparison succeeds. The same problem causes numbers read from files to fail if they are compared before being cast.

3 A text file called results.txt has one record per line in the form name,score. Write a Python program that reads the file and outputs the name of every student who scored more than 50. 5

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

  • Opens the file for reading
  • Loops through each line of the file
  • Splits each line on the comma
  • Casts the score to an integer
  • Compares with 50 and outputs the name for those above it

Model answer. A correct program is: with open("results.txt", "r") as file: then for line in file: then parts = line.strip().split(",") then name = parts[0] then score = int(parts[1]) then if score > 50: print(name). Three details carry the marks. The line must be stripped before splitting, or the score picks up a trailing newline. The score must be cast with int(), because everything read from a text file is a string and comparing a string with 50 would fail. Using with open means the file is closed automatically even if an error occurs part way through.

4 Explain the difference between opening a file in write mode and in append mode, and give one situation where each is appropriate. 4

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

  • Write mode empties the existing file before writing
  • Append mode adds new data to the end, keeping what is there
  • Gives a suitable use of write mode, such as creating a fresh report
  • Gives a suitable use of append mode, such as adding to a log of results

Model answer. Opening a file in write mode, using "w", deletes everything already in the file before a single character is written, so the file always ends up containing only what this run of the program put there. Append mode, using "a", leaves the existing contents alone and adds the new data to the end. Write mode is appropriate when the file is being regenerated in full each time, for example a report that should reflect only the current data. Append mode is appropriate when each run adds to a growing record, for example a log file that records the result of every game played, where opening in write mode would destroy the entire history every time the program ran.