Classes, Encapsulation and Representation Invariants · Multiple choice · 1 mark
A class has the representation invariant self.total == sum(self._scores). Which statement about this invariant is correct?
It may be false in the middle of a method, as long as it is true again when the method returns.
It must be true at every moment, including the middle of a method.
It only has to be true right after __init__ finishes.
It only applies to methods that change self._scores.
Answer: (A)
(A). A method that appends a score and then updates the total is momentarily inconsistent between the two statements. What matters is that the invariant holds whenever no method of the object is running: after __init__ and at the end of every method.
Stacks, Queues and Exceptions · Predict the output · 2 marks
Write the output of the code below.
class Overflow(Exception):
pass
def load(items: list[int], limit: int) -> Stack:
s = Stack()
count = 0
for x in items:
if count == limit:
raise Overflow
s.push(x)
count = count + 1
return s
try:
a = load([1, 2, 3], 3)
print('a ok')
b = load([1, 2, 3, 4], 3)
print('b ok')
except Overflow:
print('overflow')
print(a.pop())a ok
overflow
3a is loaded successfully. In the second call the fourth item hits the limit and Overflow is raised before the function returns, so the assignment to b never happens and b ok is not printed. a is still defined, so a.pop() returns 3.
Linked Lists · Short answer · 2 marks
A LinkedList has no size attribute. Let be its number of items. State the worst-case running time of a __len__ method that counts the nodes, and explain in one sentence why Python’s len on a built-in list is faster.
: every node must be visited once. A built-in list stores its length, so len is .
Trees · Multiple choice · 2 marks
Every node of a tree has at most 3 children, and the tree has height 4. What is the largest possible number of items it can contain?
12
27
40
81
Answer: (C)
(C). A full tree has items at depths 0, 1, 2, 3. (D) is , which forgets that the levels must be added.
Recursive Sorting · Complete the code · 2 marks
This version of mergesort sorts the part lst[lo:hi] of a list, and uses the course merge (given). Fill in the four blanks.
def mergesort_range(lst: list, lo: int, hi: int) -> None:
"""Sort lst[lo:hi] in place (hi is excluded); other items are unchanged.
"""
if hi - lo < 2:
return
mid = (lo + hi) // 2
mergesort_range(lst, lo, ________)
mergesort_range(lst, ________, hi)
lst[lo:hi] = merge(lst[lo:________], lst[________:hi]) mergesort_range(lst, lo, mid)
mergesort_range(lst, mid, hi)
lst[lo:hi] = merge(lst[lo:mid], lst[mid:hi])All four blanks are mid. The slice assignment lst[lo:hi] = ... changes lst itself, unlike lst = ..., which would only rebind the local name.
Introduction to Computer Science · University of Toronto (UofT, U of T, UToronto) · also written CSC148, CSC 148
Make a free, printable CSC148H1 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 900 original questions and skips the ones you have already seen.
Start with the past exams and tests your instructor or the UofT library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh CSC148H1 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 Toronto. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.