Expressions, Types and Memory · Predict the output · 2 marks
A worker’s shift is stored in minutes. Write the output of the following code fragment. If the code raises an error, write ERROR and stop.
minutes = 135
print(minutes / 60, minutes // 60, int(minutes / 60))
print(round(minutes / 60), round(minutes / 60, 1))
print(minutes % 60 / 60)2.25 2 2
2 2.2
0.25round(2.25, 1) is a tie between 2.2 and 2.3 and goes to the even last digit, 2.2. % is done before / because they have equal precedence and group left to right.
Booleans and Conditionals · Write code · 3 marks
Complete the function body according to its docstring.
def quadrant(x: float, y: float) -> int:
"""Return the quadrant (1, 2, 3 or 4) that the point (x, y)
lies in, or 0 if the point is on the x-axis or the y-axis.
Quadrant 1 has x > 0 and y > 0, quadrant 2 has x < 0 and
y > 0, quadrant 3 has x < 0 and y < 0, and quadrant 4 has
x > 0 and y < 0.
>>> quadrant(3.0, -1.5)
4
>>> quadrant(-2.0, 5.0)
2
>>> quadrant(0.0, 2.0)
0
""" if x == 0 or y == 0:
return 0
elif x > 0:
if y > 0:
return 1
else:
return 4
elif y > 0:
return 2
else:
return 3Handling the axes first means the later conditions only need > and <. A flat if/elif chain with four compound conditions is also fine.
For and While Loops · Write code · 3 marks
Complete the function body according to its docstring. Do not use str.split.
def get_initials(full_name: str) -> str:
"""Return the first character of each word in full_name.
Precondition: full_name is not empty, does not start or end
with a space, and its words are separated by single spaces.
>>> get_initials('Ada Lovelace')
'AL'
>>> get_initials('grace brewster hopper')
'gbh'
""" result = full_name[0]
for i in range(1, len(full_name)):
if full_name[i - 1] == ' ':
result = result + full_name[i]
return resultAlso accepted: start with result = '', loop over all indices, and add the character when i == 0 or the previous character is a space.
Reading and Writing Files · Predict the output · 2 marks
The file prices.txt contains the three lines below; every line ends with a newline.
$4.50
$12.00
$0.75Write the output of this code. If the code raises an error, write ERROR and stop.
f = open('prices.txt')
total = 0.0
for line in f:
total = total + float(line.strip().lstrip('$'))
print(line.strip('$\n'), end=' ')
print()
print(total)
f.close()4.50 12.00 0.75
17.25strip('$\n') removes dollar signs and newlines from both ends, and the printed text keeps its trailing zeros because it is still a string.
Classes and Objects · Write code · 2 marks
Complete the body of method buy according to its docstring.
class CoffeeCard:
"""A coffee shop loyalty card: every 10th drink is free."""
def __init__(self, owner: str) -> None:
"""Initialize a card for owner with no drinks bought."""
self.owner = owner
self.drinks = 0
def buy(self, price: float) -> float:
"""Record one drink bought with this card, and return the amount
to pay for it: 0.0 if it is the card's 10th, 20th, 30th, ...
drink, and price otherwise.
>>> card = CoffeeCard('Mei')
>>> card.buy(4.5)
4.5
""" self.drinks = self.drinks + 1
if self.drinks % 10 == 0:
return 0.0
return priceCommon mistake: testing self.drinks % 10 == 0 before adding 1, which makes the first drink free instead of the 10th.
Computer Programming for Physical Sciences and Engineering · McGill University (McGill, McGill U) · also written COMP208
Make a free, printable COMP 208 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,000 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 208 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.