Expressions, Types and Memory · Predict the output · 2 marks
Write the output of the following code fragment. If the code raises an error, write ERROR and stop.
total = 10
total += 5
total *= 2
print(total, end=' ')
total //= 4
print(total, end=' ')
total -= total % 3
print(total)30 7 6x op= y means x = x op y: 15, then 30, then 30 // 4 is 7, then 7 - 7 % 3 is 6.
Booleans and Conditionals · Complete the code · 3 marks
Consider the following function.
def mystery(n: int) -> str:
if n % 2 == 0 and n > 10:
return 'A'
elif n % 2 == 0:
return 'B'
elif n > 10 or n < 0:
return 'C'
elif n > 20:
return 'D'
else:
return 'E'For each return value, give an argument for which mystery returns that value. If no argument does, write IMPOSSIBLE.
'A' | 'B' | 'C' | 'D' | 'E' |
Answer: 12; 4; 11; IMPOSSIBLE; 3
'A': any even number above 10. 'B': any even number up to 10 (including negative ones). 'C': any odd number above 10 or below 0. 'D': IMPOSSIBLE, since any n > 20 is odd or even and is caught by an earlier branch. 'E': 1, 3, 5, 7 or 9.
Lists, Mutability and Aliasing · Memory model · 3 marks
Consider the code below.
def reset(data: list[int]) -> None:
data = []
def clear(data: list[int]) -> None:
while len(data) > 0:
data.pop()
x = [1, 2]
y = x
reset(x)
print(x, y)
clear(y)
print(x, y, x is y)Write the output of the code. [2]
Using the memory model, explain in one or two sentences why the call reset(x) does not empty the list that x refers to. [1]
[1, 2] [1, 2]
[] [] Truedata = [] only makes the local variable data in the reset frame refer to a new empty list. x in the main frame still refers to the original list, which is never mutated.
Reading and Writing Files · Write code · 2 marks
Complete the function body according to its docstring.
def write_countdown(filename: str, n: int) -> None:
"""Write the integers from n down to 1 to the file called
filename, one per line, followed by a final line Liftoff!
Any existing contents of the file are replaced.
Precondition: n >= 1
"""For example, after write_countdown('launch.txt', 3) the file launch.txt contains:
3
2
1
Liftoff! f = open(filename, 'w')
for i in range(n, 0, -1):
f.write(str(i) + '\n')
f.write('Liftoff!\n')
f.close()Common mistakes: f.write(i) (write needs a str) and forgetting the '\n'.
Classes and Objects · Multiple choice · 1 mark
Inside class Parcel, which header for __eq__ follows the course conventions and lets p1 == p2 compare two Parcel objects?
def __eq__(other: 'Parcel') -> bool:
def __eq__(self, other: 'Parcel') -> 'Parcel':
def __eq__(self, other: 'Parcel') -> bool:
def __eq__(self, p1: 'Parcel', p2: 'Parcel') -> bool:
Answer: (C)
(C). p1 == p2 calls p1.__eq__(p2): self is p1, other is p2, and the result is a bool.
Foundations of Programming · McGill University (McGill, McGill U) · also written COMP202
Make a free, printable COMP 202 practice exam: choose the topics, the length and the difficulty above, and get an exam-style PDF with a full answer key. Each paper draws on 1,100 original questions and skips the ones you have already seen.
Start with the past exams and tests your instructor or the McGill library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh COMP 202 practice exams here on the same topics, with full answer keys, as many as you need. The questions here are original, not copies of past exams.
questionbank.dev is independent and not affiliated with McGill University. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.