Classes, Encapsulation and Representation Invariants · Short answer · 2 marks
The method below omits the isinstance test.
class Point:
def __init__(self, x: int, y: int) -> None:
self.x = x
self.y = y
def __eq__(self, other: object) -> bool:
return self.x == other.x and self.y == other.yGive an expression that shows why the test is needed, and say what happens when it runs.
For example Point(1, 2) == 5 raises an AttributeError, because an int has no attribute x. With the isinstance test the comparison would simply evaluate to False.
Inheritance, Abstract Classes and Polymorphism · Short answer · 2 marks
For each pair of classes, write inheritance if the second should be a subclass of the first, or composition if an object of the first should hold an object of the second as an attribute.
Account and SavingsAccount | |
Car and Wheel | |
Course and Instructor | |
Animal and Penguin |
Answer: inheritance; composition; composition; inheritance
A savings account is an account and a penguin is an animal. A car has wheels and a course has an instructor.
Running Time and Big-Oh · Predict the output · 2 marks
Write the output of the following code. If the code raises an error, write ERROR and stop.
def steps(n: int) -> int:
count = 0
for i in range(n):
for j in range(i, n):
count = count + 1
return count
print(steps(1))
print(steps(4))
print(steps(10))1
10
55The inner loop runs times, so the total is .
Linked Lists · Predict the output · 2 marks
Write the output of the code below. “print(lst)” displays a LinkedList like [1 -> 2 -> 3].
lst = LinkedList([5, 4, 3, 2, 1])
curr = lst._first
while curr is not None and curr.next is not None:
curr.item = curr.item * 10
curr = curr.next.next
print(lst)
print(curr.item)[50 -> 4 -> 30 -> 2 -> 1]
1curr visits the 1st, 3rd and 5th nodes. At the 5th node curr.next is None, so the loop ends without changing it, and curr still refers to that node.
Recursive Sorting · Multiple choice · 1 mark
Using the course mergesort (halves of sizes len(lst) // 2 and the rest), how many calls to merge are made by mergesort([9, 4, 7, 1, 8, 2])?
3
5
6
11
Answer: (B)
(B). Every call on a list with 2 or more items makes exactly one call to merge. The tree of calls has 6 leaves (one per item) and therefore 5 internal nodes. (The whole tree has 11 calls to mergesort, option D.)
Introduction to the Foundations of Computation II · University of Alberta (UAlberta, UofA, U of A) · also written CMPUT175
Make a free, printable CMPUT 175 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 700 original questions and skips the ones you have already seen.
Start with the past exams and tests your instructor or the UAlberta library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh CMPUT 175 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 Alberta. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.