Expressions, Types and Memory · Short answer · 2 marks
The line print(0.1 + 0.2 == 0.3) prints False.
In one or two sentences, explain why. [1]
Variable total refers to a float. Write a boolean expression that is True when total is within 0.000001 of 0.3 (above or below). [1]
(a) Floats are stored in binary with limited precision, so 0.1, 0.2 and 0.3 are only approximations; 0.1 + 0.2 is actually 0.30000000000000004, which is not exactly equal to the approximation of 0.3.
(b) abs(total - 0.3) < 0.000001 (<= also accepted)
Booleans and Conditionals · Predict the output · 2 marks
Write the output of the following code fragment. Recall that uppercase letters come before lowercase letters when strings are compared.
a = 'kiwi'
b = 'Kiwi'
c = 'kiwis'
print(max(a, b))
print(min(a, c))
if a.upper() == b.upper():
print('same')
if b < a < c:
print('ordered')
else:
print('not ordered')kiwi
kiwi
same
orderedmax and min use the same ordering as <: 'K' < 'k', and a string comes before any longer string that starts with it.
Lists, Mutability and Aliasing · Predict the output · 3 marks
Write the output of the code fragment below.
a = [1, 2, 3]
b = a
for i in range(len(a)):
b[i] = a[i] + a[i - 1]
print(a)
c = [1, 2, 3]
d = c[:]
for i in range(len(c)):
d[i] = c[i] + c[i - 1]
print(c, d)[4, 6, 9]
[1, 2, 3] [4, 3, 5]When i is 0, a[i - 1] is the last item. In the first loop b is an alias of a, so each step reads the value written by the previous step; in the second loop c never changes.
Tuples and Dictionaries · Write code · 2 marks
Complete the function body according to its docstring.
def cart_summary(cart: dict[str, int], prices: dict[str, float]
) -> tuple[int, float]:
"""Return a tuple of the total number of items in cart and their
total cost. cart maps each item to a quantity, and prices maps
each item to its unit price.
Precondition: every key in cart is a key in prices.
>>> cart_summary({'pen': 3, 'pad': 1},
... {'pen': 1.5, 'pad': 4.0, 'ink': 9.0})
(4, 8.5)
""" count = 0
cost = 0.0
for item in cart:
count = count + cart[item]
cost = cost + cart[item] * prices[item]
return (count, cost)The number of items counts quantities (4), not distinct keys (2). Looping over prices instead of cart would include unbought items.
Classes and Objects · Predict the output · 3 marks
Write the output of the code below. If the code raises an error, write ERROR and stop.
class Jar:
"""A jar of coins."""
def __init__(self, label: str, coins: int) -> None:
self.label = label
self.coins = coins
def give(self, other: 'Jar', n: int) -> None:
n = min(n, self.coins)
self.coins = self.coins - n
other.coins = other.coins + n
def __str__(self) -> str:
return self.label + ':' + str(self.coins)
a = Jar('a', 5)
b = Jar('b', 2)
a.give(b, 3)
b.give(a, 10)
print(a, b)
a.give(a, 4)
print(a)
c = b
c.give(a, 1)
print(a, b, c)a:7 b:0
a:7
a:7 b:0 b:0b.give(a, 10) gives only the 5 coins b has. In a.give(a, 4), self and other are the same jar, so it loses 4 and gains 4.
Principles of Computing for Science · University of Waterloo (Waterloo, UWaterloo, UW) · also written CS114
Make a free, printable CS 114 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 Waterloo library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh CS 114 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 University of Waterloo. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.