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.

GCSE Computer Science · 2.2.3

Additional Programming Techniques

Section A: 10 multiple choice questions, 10 marks. Section B: 5 written questions, 19 marks. Total 29 marks.

Name

Class

Date

Mark  / 29

Section A

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

  1. 1What does "Programming"[0:4] produce in Python?1
    1. Agram
    2. BProgr
    3. CProg
    4. Drogr
  2. 2An array data has 8 elements. What is the index of the last element?1
    1. A0
    2. B7
    3. C8
    4. D9
  3. 3Which file mode adds data to the end of a file without deleting what is already there?1
    1. AAppend
    2. BRead
    3. CWrite
    4. DOverwrite
  4. 4Which SQL query returns only the names of students in Year 11?1
    1. ASELECT Name WHERE Year = 11 FROM Students
    2. BSELECT Students FROM Name WHERE Year = 11
    3. CSELECT * FROM Students WHERE Year = 11
    4. DSELECT Name FROM Students WHERE Year = 11
  5. 5What is the difference between a parameter and an argument?1
    1. AThey are the same thing
    2. BA parameter is in the definition, an argument is the value passed in when called
    3. CAn argument is in the definition, a parameter is passed in
    4. DParameters are only used in procedures
  6. 6In the 2D array grid, how would you access the item in row 2, column 0?1
    1. Agrid[2,0][0]
    2. Bgrid(2,0)
    3. Cgrid[0][2]
    4. Dgrid[2][0]
  7. 7Why are local variables generally preferred to global variables?1
    1. AThey are automatically saved to a file
    2. BThey are faster to access than any other variable
    3. CThey cannot be accidentally changed by other parts of the program
    4. DThey can hold more data
  8. 8What does random.randint(1, 6) return?1
    1. AThe number 6 every time
    2. BA whole number from 1 to 5
    3. CA whole number from 1 to 6 inclusive
    4. DA decimal between 1 and 6
  9. 9Which is a benefit of using subroutines?1
    1. ACode is written once and can be called many times, so it is easier to maintain
    2. BPrograms always run faster
    3. CVariables become global automatically
    4. DFiles do not need to be closed
  10. 10A program needs to find the largest value in an array that may contain negative numbers. How should the variable holding the maximum be initialised?1
    1. ATo the first element of the array
    2. BTo 0
    3. CTo the length of the array
    4. DTo 1

Section B

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

  1. 1WriteWrite an SQL query to return the Name and Grade of all students in the table Students who have a Grade of 7 or higher.3
  2. 2ExplainExplain two benefits of using subroutines in a program.4
  3. 3ExplainA program stores high scores in a text file. Explain why the file should be opened in append mode rather than write mode when a new score is added.3
  4. 4WriteWrite a program that reads a list of names from a file called names.txt and outputs only those names that begin with the letter A.5
  5. 5ExplainExplain the difference between a local variable and a global variable, and state one reason why local variables are usually preferred.4

Mark scheme

Section A

1C2B3A4D5B6D7C8C9A10A

Section B

1 Write an SQL query to return the Name and Grade of all students in the table Students who have a Grade of 7 or higher. 3

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

  • Uses SELECT with the correct columns Name and Grade
  • Uses FROM with the correct table name
  • Uses WHERE with the correct condition

Model answer. SELECT Name, Grade FROM Students WHERE Grade >= 7 The SELECT clause names the two columns required rather than using an asterisk, FROM names the table being queried, and WHERE restricts the rows returned to those where the Grade column holds a value of 7 or above. No quotation marks are used around 7 because it is a number, not text.

2 Explain two benefits of using subroutines in a program. 4

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

  • Code that is needed in several places is written only once
  • If a change is needed it is made in one place, reducing errors
  • Each subroutine can be tested independently
  • Programs are easier to read and understand, and subroutines support decomposition

Model answer. The first benefit is the removal of duplication. Code that is needed in several parts of a program can be written once as a subroutine and then called wherever it is needed, which makes the program shorter. More importantly, if that code ever needs correcting or updating, the change only has to be made in one place, rather than finding and editing every copy, which greatly reduces the risk of leaving an old version behind and introducing a bug. The second benefit is that subroutines make a program much easier to develop and test. Each subroutine handles one clearly defined task, so it can be tested on its own with a range of inputs until it is known to work correctly, and different programmers can develop different subroutines at the same time. This also makes the main program far easier to read, because a well named call such as calculateTotalCost explains what is happening without the reader needing to see the detail.

3 A program stores high scores in a text file. Explain why the file should be opened in append mode rather than write mode when a new score is added. 3

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

  • Write mode erases the existing contents of the file
  • All previous high scores would therefore be lost
  • Append mode adds the new score to the end while keeping existing data

Model answer. Opening a file in write mode causes the existing contents of that file to be erased before anything new is written, so every high score already stored would be permanently lost as soon as a single new score was saved. Append mode instead opens the file so that anything written is added to the end, leaving all the existing content untouched. Since the purpose of a high score table is to accumulate results over time, append mode is the correct choice.

4 Write a program that reads a list of names from a file called names.txt and outputs only those names that begin with the letter A. 5

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

  • Opens the file for reading
  • Uses a loop to process each line
  • Removes the newline character or otherwise cleans the line
  • Tests whether the first character is A
  • Outputs matching names and closes the file

Model answer. file = open("names.txt", "r") for line in file: name = line.strip() if name[0].upper() == "A": print(name) file.close() The file is opened in read mode and each line is processed in turn. The strip method removes the newline character from the end of each line so that the name is clean. The first character of the name is converted to uppercase and compared with A, so that names beginning with either a capital or a lowercase a are matched, and matching names are printed. The file is closed once every line has been processed.

5 Explain the difference between a local variable and a global variable, and state one reason why local variables are usually preferred. 4

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

  • A local variable only exists inside the subroutine where it is declared
  • A global variable is accessible throughout the whole program
  • Local variables cannot be changed accidentally by other parts of the program
  • Memory used by a local variable is freed when the subroutine ends

Model answer. A local variable is declared inside a subroutine and only exists while that subroutine is running, so it cannot be seen or changed by any code outside it. A global variable is declared outside all subroutines and is accessible from anywhere in the program, so any part of the code can read or modify it. Local variables are usually preferred because their limited scope prevents them being altered accidentally by unrelated parts of the program, which removes a very common and hard to trace source of bugs, and it also means two subroutines can safely use the same variable name without interfering with each other. In addition, the memory a local variable occupies is released as soon as the subroutine finishes, whereas a global variable takes up memory for the entire time the program runs.