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

Python: Loops, Lists and Functions: Unit Exam B

Instructions

Time allowed
25 minutes
Total marks
25
Calculator
No calculator is permitted.
  • Answer all the questions.

Questions

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.

Think this answer or mark scheme is wrong? Report it

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.

Think this answer or mark scheme is wrong? Report it

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.

Think this answer or mark scheme is wrong? Report it

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.

Think this answer or mark scheme is wrong? Report it

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.

Think this answer or mark scheme is wrong? Report it

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.

Think this answer or mark scheme is wrong? Report it

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.

Think this answer or mark scheme is wrong? Report it

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.

Think this answer or mark scheme is wrong? Report it

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.

Think this answer or mark scheme is wrong? Report it

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.

Think this answer or mark scheme is wrong? Report it

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.

Think this answer or mark scheme is wrong? Report it

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.

Think this answer or mark scheme is wrong? Report it

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.

Think this answer or mark scheme is wrong? Report it

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.

Think this answer or mark scheme is wrong? Report it

Machine mark 0 / 25