Time left25:00 Answered 0 of 15 25 marks
MskProd Year 9 unit exam 25 minutes 25 marks Unit exam

Python Programming: Unit Exam B

Instructions

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

Questions

25 marks
1What does this program print?1 mark
Python code
def area(r):
    return 3 * r * r

print(area(2) + area(1))

Mark scheme0 / 1
  • L15
Model answer

area(2) is 12 and area(1) is 3: 15.

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

2Which of these is a procedure?1 mark

Tick one box.

Mark scheme0 / 1
  • LC def show_menu(): print("1. Play")
Model answer

show_menu does a job and returns nothing, so it is a procedure.

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

3This 2D list is used for each part:seats = [["A1", "A2"], ["B1", "B2"], ["C1", "C2"]]Write what each expression gives.3 marks

Mark scheme0 / 3
  • LB1
  • LC2
  • L2
Model answer

Row 1 is the B row and row 2 the C row. Each row has 2 seats.

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:capitals = {"France": "Paris", "Spain": "Madrid"} capitals["Italy"] = "Rome"Write what each expression gives now.2 marks

Mark scheme0 / 2
  • LMadrid
  • L3
Model answer

The value for Spain is Madrid, and adding Italy makes three entries.

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

5The program stores test scores in a dictionary. Complete it so that it prints the name of every student who scored 50 or more, one name per line.4 marks
answer.py

Mark scheme0 / 4
  • LUses a loop over the dictionary
  • LPrints Ana and Cara
  • LDoes not print Ben or Dev
  • LUses the condition 50 or more, so Cara's score of exactly 50 is included
A program that scores full marks

Loop over the keys and print each name whose score is at least 50. Using > 50 would wrongly leave out Cara.

Python
scores = {"Ana": 72, "Ben": 45, "Cara": 50, "Dev": 38}
for name in scores:
    if scores[name] >= 50:
        print(name)
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

6Why would a game append to its high score file rather than write to it?1 mark

Tick one box.

Mark scheme0 / 1
  • LD Writing would erase the scores already saved
Model answer

Write mode empties the file first; append keeps what is there.

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

7How many lines does log.txt contain after this program runs?1 mark
Python code
file = open("log.txt", "w")
file.write("one\n")
file.close()
file = open("log.txt", "a")
file.write("two\n")
file.close()

Mark scheme0 / 1
  • L2
Model answer

Write mode creates the file with one line, then append mode adds a second.

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

8Which error does this program produce?1 mark
Python code
def set_score():
    score = 10

set_score()
print(score)

Mark scheme0 / 1
  • LNameError
Model answer

score only exists inside set_score(), so print(score) raises a NameError.

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

9Explain the difference between a local variable and a global variable.2 marks
Explain

Mark scheme0 / 2
  • LA local variable only exists inside the subroutine where it is created
  • LA global variable can be used anywhere in the program
Model answer

A local variable is created inside a subroutine and only exists while it runs, so nothing outside can use it. A global variable is created in the main program and can be used anywhere in it.

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

10Tick one box in each row to show the best data structure for each job.3 marks
List2D listDictionary
Store each player's name with their score, to look up a score by name
Store the squares of a noughts and crosses board in rows and columns
Store a shopping list in order

Tick one box in each row.

Mark scheme0 / 3
  • LStore each player's name with their score, to look up a score by name: Dictionary
  • LStore the squares of a noughts and crosses board in rows and columns: 2D list
  • LStore a shopping list in order: List
Model answer

A dictionary looks values up by key, a 2D list holds a grid, and a list holds items in order.

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

11Which line opens data.txt so that it can be read?1 mark

Tick one box.

Mark scheme0 / 1
  • LA open("data.txt", "r")
Model answer

"r" is read mode.

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

12A program reads names from a file, but it crashes if the file does not exist. Describe how it could be made more robust.2 marks
Describe

Mark scheme0 / 2
  • LCheck the file exists first, or catch the error with try and except
  • LShow a helpful message, or create an empty file, instead of crashing
Model answer

Put the open() inside try and except FileNotFoundError. If the file is missing, show the user a clear message or create an empty file, so the program carries on instead of crashing.

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

13What does this program print?1 mark
Python code
total = 0
for row in [[1, 2], [3, 4], [5, 6]]:
    total = total + row[1]
print(total)

Mark scheme0 / 1
  • L12
Model answer

row[1] is the second item of each row: 2 + 4 + 6 = 12.

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 a function with no return statement give back?1 mark

Tick one box.

Mark scheme0 / 1
  • LB None
Model answer

Python returns None when there is no 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

15What does this program print?1 mark
Python code
word = "Python"
print(word[0] + word[-1])

Mark scheme0 / 1
  • LPn
Model answer

The first letter is P and the last is 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

Machine mark 0 / 25