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

Python: Loops, Lists and Functions: Unit Exam B

Reading range(), counting down with while, changing lists, a times table program to write, functions, validation loops and choosing a loop.

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 first number this program prints?1 mark
Python code
for i in range(5):
    print(i)

Mark scheme0 / 1
  • L0
Model answer

range(5) starts at 0.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
2How many numbers does range(2, 8) produce?1 mark

Mark scheme0 / 1
  • L6
Model answer

2, 3, 4, 5, 6 and 7: six numbers, stopping before 8.

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
count = 10
while count > 0:
    count = count - 3
print(count)

Mark scheme0 / 1
  • L-2
Model answer

10, 7, 4, 1, then -2. -2 is not above 0, so the loop stops and prints -2.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
4This program runs first:pets = ["cat", "dog", "fish"] pets.append("rabbit") pets[1] = "hamster"Write what each expression gives now.4 marks

Mark scheme0 / 4
  • Lhamster
  • Lrabbit
  • L4
  • Lcat
Model answer

rabbit is added at index 3, and index 1 is replaced by hamster, giving cat, hamster, fish, rabbit.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
5Which loop prints the numbers 1 to 10?1 mark

Tick one box.

Mark scheme0 / 1
  • LB for i in range(1, 11): print(i)
Model answer

range(1, 11) starts at 1 and stops before 11.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
6Write a program that asks for a number and prints the first five lines of its times table, one result per line. For example, for 3 it prints 3, 6, 9, 12 and 15.3 marks
answer.py

Mark scheme0 / 3
  • LUses a loop
  • LPrints 3, 6, 9, 12 and 15 for 3
  • LWorks for any number: 7, 14, 21, 28 and 35 for 7
A program that scores full marks

Loop i from 1 to 5 with range(1, 6), printing i * n each time.

Python
n = int(input("Number: "))
for i in range(1, 6):
    print(i, "x", n, "=", i * n)
Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
7What does len() give when it is given a list?1 mark

Tick one box.

Mark scheme0 / 1
  • LD The number of items in the list
Model answer

len() counts the items.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
8What does this program print?1 mark
Python code
def greet(name):
    return "Hi " + name

message = greet("Ana")
print(message + "!")

Mark scheme0 / 1
  • LHi Ana!
Model answer

greet returns Hi Ana, and the ! is added before printing.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
9What is the difference between a parameter and a return value?1 mark

Tick one box.

Mark scheme0 / 1
  • LC A parameter is passed into a function; a return value is sent back out of it
Model answer

Values go in through parameters and come out through return.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
10Explain why programmers write functions.2 marks
Explain

Mark scheme0 / 2
  • LCode can be reused without writing it again
  • LPrograms are easier to read, test and fix, because they are split into named parts
Model answer

A function is written once and called whenever it is needed, so code is not repeated. Splitting a program into named functions also makes it easier to read, and each part can be tested and fixed on its own.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
11The user types 9, then 25, then 14. Complete the output, one line per row.3 marks
Python code
age = int(input("Age: "))
while age < 11 or age > 18:
    print("Invalid")
    age = int(input("Age: "))
print("Accepted")
LineOutput
1
2
3

Mark scheme0 / 3
  • LInvalid
  • LInvalid
  • LAccepted
Model answer

9 is below 11, so Invalid. 25 is above 18, so Invalid. 14 is in range, so the loop ends and Accepted is printed.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
12Which validation check makes sure an age is between 11 and 18?1 mark

Tick one box.

Mark scheme0 / 1
  • LA Range check
Model answer

A range check tests that a value lies between limits.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
13Tick one box in each row to show which loop suits each job best.4 marks
for loopwhile loop
Print each item in a shopping list
Keep asking until the user types quit
Repeat exactly 20 times
Keep rolling a dice until it shows a six

Tick one box in each row.

Mark scheme0 / 4
  • LPrint each item in a shopping list: for loop
  • LKeep asking until the user types quit: while loop
  • LRepeat exactly 20 times: for loop
  • LKeep rolling a dice until it shows a six: while loop
Model answer

Use for when the number of repeats is known, and while when you repeat until something happens.

Marked exactly against the mark scheme. Change it only if an examiner would accept your answer.
14What does this program print?1 mark
Python code
nums = [5, 10, 15]
total = 0
for n in nums:
    total = total + n
print(total / len(nums))

Mark scheme0 / 1
  • L10
Model answer

The total is 30 and there are 3 items: 30 / 3 = 10.0.

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.