⏱️ Duration: 75 minutes | Total: /60 marks
Show all work. Code-writing answers must be syntactically valid Python. Where space permits, add inline comments to clarify intent. No internet, no AI tools.
K/U
/15
Thinking
/15
Comm.
/15
Applic.
/15
Part A: Knowledge & Understanding [15 marks]
1. [2 marks]
State the value and the data type of each expression.
3 + 4 / 2
"3" * 4
10 // 3
not (5 == 5)
2. [3 marks]
Trace this program. Show the value of n at each iteration and the final printed value.
n = 1
for i in range(5):
n = n * 2 + 1
print(n)
3. [2 marks]
A program reads a user's age. Write the if/elif chain that prints "Child" if age < 13, "Teen" if 13–19, "Adult" if 20–64, otherwise "Senior".
4. [2 marks]
What is the output?
x = 5
y = 2
print(f"{x} divided by {y} is {x/y:.2f}")
5. [3 marks]
A student has written:
while temperature > 0:
print("Still hot")
temperature -= 5
What does this loop do, and what happens if temperature starts at -3? Explain in 2–3 sentences.
6. [3 marks]
Identify the data type that should be used to represent each:
The age of a student
The student's name
Whether the student has paid their fee
The student's grade-point average
A list of the student's courses
The number of letters in the student's last name
Part B: Thinking & Investigation [15 marks]
7. [5 marks]
Find and fix the bugs. The function below is supposed to return the sum of even numbers from 1 to n inclusive. There are three bugs. Rewrite the function correctly.
def sum_evens(n):
total = 1
for i in range(0, n):
if i % 2 = 0:
total + i
return Total
8. [5 marks]
Trace the following nested-loop program. List every line that print outputs, in order.
for i in range(3):
for j in range(3):
if i == j:
print(f"({i},{j}) diagonal")
elif i < j:
print(f"({i},{j}) above")
else:
print(f"({i},{j}) below")
9. [5 marks]
A vending machine accepts only loonies and toonies. Write a function change(amount) that, given an integer dollar amount, returns the minimum number of coins (toonies + loonies) required. Examples:
change(7) → 4 (3 toonies + 1 loonie)
change(2) → 1 (1 toonie)
change(0) → 0
Part C: Communication [15 marks]
10. [5 marks]
Explain the difference between a for loop and a while loop in your own words. Include: when each is most appropriate, what risks each has, and a small example of each (1–3 lines).
11. [5 marks]
A peer's code passes their tests but is hard to read — everything is inside main(), with single-letter variables, no comments, and lines that exceed 100 characters. List five specific style/quality issues you would raise in a code review and explain why each matters for maintainability.
12. [5 marks]
Add high-quality comments and rename variables in this program so a future reader can understand it without running it. Submit your re-commented version.
def f(a, b):
c = 0
for x in range(a, b+1):
if x % 2:
c += x
return c
Part D: Application [15 marks]
13. [5 marks]
Write a complete Python program that:
Asks the user for their hourly wage and the number of hours worked this week.
Calculates pay: regular pay for the first 40 hours, time-and-a-half for hours over 40.
Prints the formatted total to two decimal places.
Include input validation: if hours or wage is negative, print an error and exit.
14. [5 marks]
Write a function count_vowels(s) that takes a string and returns the number of vowels (a, e, i, o, u, case-insensitive). Then show the output of count_vowels("Programming Foundations").
15. [5 marks]
A teacher wants a small program that reads marks one at a time until the user enters -1, then prints (a) the number of marks entered, (b) the average, and (c) the highest mark. Write the program. (Hint: use a sentinel-controlled while loop.)