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

Python: Loops, Lists and Functions: Unit Exam A

for and while loops, lists and indexes, append and len, functions with parameters and return values, and validation.

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
1How many times does this program print Hi?1 mark
Python code
for i in range(3):
    print("Hi")

Mark scheme0 / 1
  • L3
Model answer

range(3) gives 0, 1 and 2: three passes.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
2What is the last number this program prints?1 mark
Python code
for i in range(1, 5):
    print(i)

Mark scheme0 / 1
  • L4
Model answer

range(1, 5) stops before 5, so the last value is 4.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
3What does this program print?1 mark
Python code
total = 0
for n in range(1, 6):
    total = total + n
print(total)

Mark scheme0 / 1
  • L15
Model answer

1 + 2 + 3 + 4 + 5 = 15.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
4This list is used for each part:colours = ["red", "green", "blue", "yellow"]Write what each expression gives.4 marks

Mark scheme0 / 4
  • Lred
  • Lblue
  • Lyellow
  • L4
Model answer

Indexes start at 0, so colours[0] is red and colours[2] is blue. -1 is the last item, yellow. There are 4 items.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
5Which line adds "purple" to the end of the list colours?1 mark

Tick one box.

Mark scheme0 / 1
  • LA colours.append("purple")
Model answer

append() adds to the end. colours[4] would be an error, because the list has no index 4 yet.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
6What is an infinite loop?1 mark

Tick one box.

Mark scheme0 / 1
  • LC A loop whose condition never becomes false, so it never stops
Model answer

If nothing inside the loop ever makes the condition false, it runs for ever.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
7What does this program print?1 mark
Python code
x = 1
while x < 50:
    x = x * 2
print(x)

Mark scheme0 / 1
  • L64
Model answer

x doubles: 2, 4, 8, 16, 32, 64. 64 is not below 50, so the loop stops and 64 is printed.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
8Which is best when you do not know in advance how many times to repeat, such as asking until a valid password is typed?1 mark

Tick one box.

Mark scheme0 / 1
  • LB A while loop
Model answer

A while loop repeats until its condition changes.

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

print(double(4) + double(3))

Mark scheme0 / 1
  • L14
Model answer

double(4) returns 8 and double(3) returns 6: 8 + 6 = 14.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
10This program is used for each row:def double(n): return n * 2result = double(4)Tick one box in each row.3 marks
ParameterFunction callReturn value
n in def double(n):
double(4)
The value worked out by n * 2 and sent back

Tick one box in each row.

Mark scheme0 / 3
  • Ln in def double(n):: Parameter
  • Ldouble(4): Function call
  • LThe value worked out by n * 2 and sent back: Return value
Model answer

n is the parameter, double(4) calls the function, and n * 2 is sent back as the return value.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
11Complete the function area so that it returns the area of a rectangle with the given width and height.3 marks
answer.py

Mark scheme0 / 3
  • LDefines a function called area with two parameters
  • LReturns the correct area, for example 12 for a width of 3 and a height of 4
  • LReturns the value rather than printing it
A program that scores full marks

Multiply the width by the height and return the result.

Python
def area(width, height):
    return width * height
Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
12Explain what validation is, and give one example of a validation check.2 marks
Explain

Mark scheme0 / 2
  • LChecking that input is sensible or follows the rules before it is used
  • LA named check, such as a range, length, presence or type check
Model answer

Validation is the program checking that input is sensible, and follows the rules, before using it. A range check, for example, rejects an age outside 11 to 18.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
13Complete the table to show what this program prints, one line per row.3 marks
Python code
for i in range(2, 11, 4):
    print(i)
LinePrinted
1
2
3

Mark scheme0 / 3
  • L2
  • L6
  • L10
Model answer

range(2, 11, 4) starts at 2 and goes up in 4s, stopping before 11: 2, 6, 10.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
14What does this program print?nums = [4, 8, 15] print(nums[1])1 mark

Tick one box.

Mark scheme0 / 1
  • LD 8
Model answer

Index 1 is the second item, 8.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
15What does this program print?1 mark
Python code
scores = [3, 7, 2]
scores.append(5)
print(len(scores))

Mark scheme0 / 1
  • L4
Model answer

Appending 5 makes four items.

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.