Time left25:00 Answered 0 of 16 25 marks
MskProd Year 9 unit exam 25 minutes 25 marks Original paper

Python Programming: Unit Exam A

Functions and procedures, parameters, local and global variables, 2D lists, dictionaries, files, and a function to write.

Instructions

Time allowed
25 minutes
Total marks
25
Calculator
No calculator is permitted.
  • Answer all the questions.
  • The number of marks for each question is shown in brackets. Use it as a guide to how much to write.
  • The clock at the top counts down from the real time allowed. When it reaches zero the paper is locked and marked, as it would be collected in the exam hall.
  • Read every question to the end before answering. Where a question says tick two boxes, tick exactly two: ticking more than asked scores nothing. Type one word and number answers carefully, because they are marked exactly.

Questions

Answer every question, then finish the paper to have it marked. Marking as you go teaches you far less.

25 marks
1What is the difference between a function and a procedure?1 mark

Tick one box.

Mark scheme0 / 1
  • LC A function returns a value; a procedure does not
Model answer

Both are subroutines; only a function returns a value.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
2What does this program print?1 mark
Python code
def add_vat(price):
    return price * 1.2

print(add_vat(50))

Mark scheme0 / 1
  • L60
Model answer

50 x 1.2 = 60.0.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
3(a)What does this program print?1 mark
Python code
x = 5

def change():
    x = 10

change()
print(x)

Mark scheme0 / 1
  • L5
Model answer

The x inside change() is a new local variable. The global x is still 5.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
3(b)Why is x still 5?1 mark

Tick one box.

Mark scheme0 / 1
  • LA The x inside change() is a local variable, separate from the global x
Model answer

Assigning to x inside a function creates a local variable.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
4This 2D list is used for each part:grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]Write what each expression gives.4 marks

Mark scheme0 / 4
  • L3
  • L7
  • L5
  • L3
Model answer

The first index picks the row and the second the column, both counting from 0. There are 3 rows.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
5This program runs first:ages = {"Ali": 13, "Bea": 14} ages["Cy"] = 12Write what each expression gives now.2 marks

Mark scheme0 / 2
  • L14
  • L3
Model answer

Bea's value is 14, and adding Cy makes three entries.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
6Which file mode adds to the end of a file without deleting what is already there?1 mark

Tick one box.

Mark scheme0 / 1
  • LD "a"
Model answer

"a" is append mode.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
7What happens when a file is opened with mode "w"?1 mark

Tick one box.

Mark scheme0 / 1
  • LB Anything already in the file is erased
Model answer

Write mode starts the file again from empty.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
8Complete the function largest so that it returns the largest number in a list. Do not use max().4 marks
answer.py

Mark scheme0 / 4
  • LDefines largest with one parameter
  • LReturns the largest value, for example 9 from [3, 9, 2]
  • LWorks when every number is negative, for example -2 from [-5, -2, -9]
  • LFinds it without max()
A program that scores full marks

Start with the first item as the best so far, then compare it with every other item. Starting from 0 fails when every number is negative.

Python
def largest(numbers):
    best = numbers[0]
    for n in numbers:
        if n > best:
            best = n
    return best
Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
9Explain two advantages of breaking a program into subroutines.2 marks
Explain

Mark scheme0 / 2
  • LA subroutine can be called many times, so code is not repeated
  • LEach part can be tested, fixed or written separately, which makes the program easier to read and maintain
Model answer

A subroutine is written once and called whenever it is needed, so the same code is never repeated. Each subroutine can also be tested and fixed on its own, and different people can write different ones, which makes a large program far easier to manage.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
10What does this program print?1 mark
Python code
def count_over(scores, limit):
    total = 0
    for s in scores:
        if s > limit:
            total = total + 1
    return total

print(count_over([12, 5, 18, 7, 20], 10))

Mark scheme0 / 1
  • L3
Model answer

12, 18 and 20 are over 10: three of them.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
11What does this program print?1 mark
Python code
stock = {"pens": 4, "pads": 0, "rulers": 7}
for item in stock:
    if stock[item] == 0:
        print(item)

Mark scheme0 / 1
  • Lpads
Model answer

Only pads has a value of 0.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
12Which line reads every line of an open file into a list?1 mark

Tick one box.

Mark scheme0 / 1
  • LB file.readlines()
Model answer

readlines() returns a list with one item per line.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
13Explain why global variables should be used sparingly.2 marks
Explain

Mark scheme0 / 2
  • LAny part of the program can change a global variable, so bugs are hard to track down
  • LLocal variables keep subroutines independent, so they can be reused and tested on their own
Model answer

Any part of the program can change a global variable, so when its value is wrong it is hard to work out which part changed it. Local variables keep each subroutine self-contained, so it can be tested and reused without side effects.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
14What does this program print?1 mark
Python code
names = ["Zoe", "Amy", "Max"]
names.sort()
print(names[0])

Mark scheme0 / 1
  • LAmy
Model answer

Sorted alphabetically the list is Amy, Max, Zoe.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
15What is a parameter?1 mark

Tick one box.

Mark scheme0 / 1
  • LD A variable in a subroutine's definition that receives a value when the subroutine is called
Model answer

Parameters receive the values passed in.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
Machine mark 0 / 25
About this paper

This is an original Key Stage 3 assessment. Most questions are closed, so the marking is exact, and the written answers are marked against a mark scheme in the same style as a GCSE paper, so the format is familiar long before it counts.