CSC148 20221-Exam
CSC148 20221-Exam
CSC148 20221-Exam
Given Name(s):
Family Name(s):
• Turn off and place all cell phones, smart watches, electronic devices, and unau-
thorized study materials in your bag under your desk. If it is left in your pocket,
Marking Guide
it may be an academic offence.
• This final examination consists of 11 questions on 18 pages (including this one), No 1: /13
printed on both sides of the paper. When you receive the signal to start, please
make sure that your copy of the examination is complete. No 2: / 4
• Answer each question directly on the examination paper, in the space provided. No 3: / 5
There are several blank pages at the end for rough work. No 4: / 4
• Comments are not required in your code, however they may help us give you part No 5: / 5
marks if your answer is not completely correct.
No 6: /10
• There is an aid sheet on a separate piece of paper. Nothing you write on the
aid sheet will be marked. No 7: / 6
• Remember that, in order to pass the course, you must achieve a grade of at least No 8: / 5
40% on this final examination. No 9: / 8
• As a student, you help create a fair and inclusive writing environment. If you No 10: / 5
possess an unauthorized aid during an exam, you may be charged with an
academic offence. No 11: /11
TOTAL: /76
It’s been a real pleasure teaching you this term. Good luck!
3. Write a line of code to print the model of the first bus in ttc (the one at index 0).
4. What is the technical term for the relationship between our two classes?
Part (c) [2 marks] Suppose I can run this nonsense code without error:
thing1 = Froogle()
thing2 = Froogle()
thing1.absorb([1, 2, 3])
n = int(input(’Enter a number: ’)) # Read an int from the user.
if n in thing1:
thing2.absorb([n, n + 1, n + 2])
if thing1 == thing2:
print(f’{thing1} and {thing2} are samesies!’)
What method(s) must be defined in class Froogle so that the code runs? (It need not do anything sensible.)
April 2022 Examinations
CSC 148 H1S
Duration: 3 hours
What is the value of each of the following expressions? If it raises an error, explain why the error occurs.
pokey._first == pokey._rest
small._rest._first is None
type(small._rest)
type(linky._first._rest)
linky._rest._rest._rest._first
>>> b = BinarySearchTree(8)
>>> b.insert(20)
>>> b.insert(17)
>>> b.insert(2)
What is the value of each of the following expressions? If it raises an error, explain why the error occurs.
b._right._left._root
b._right._right is None
Question 2. [4 marks]
Part (a) [2 marks]
For this question, fill in actual numbers, not formulas.
A binary search tree with 14 nodes has height at least and at most .
4663
8
it 5 Iq
0
It 3 8 44
d
2
The post-order traversal of this tree is:
234 Yg 2
1
70
68 3
If this tree is stored using our implementation of the BinarySearchTree class (see the Aid Sheet for reference),
how many instances of BinarySearchTree are used?
April 2022 Examinations
CSC 148 H1S
Duration: 3 hours
Question 3. [5 marks]
Part (a) [4 marks]
Complete the memory model diagram below to reflect the state of this code immediately before it is done
executing. Show the whole history of changes. For any values that change, cross out the old value and write the
updated value beside it. For popped stack frames, neatly cross out the stack frame by drawing an X over it.
if __name__ == ‘__main__’:
original_lst = [[1, 2, 3], [4, 5, 6]]
lst_copy = [original_lst[0], original_lst[1]]
do_something(lst_copy)
print(original_lst)
print(lst_copy)
April 2022 Examinations
CSC 148 H1S
Duration: 3 hours
Question 4. [4 marks]
Consider the following module:
if __name__ == ‘__main__’:
d = {3: 8, 5: 32, 0: 2}
answer = bumbly(d)
print(answer)
2. For each of the following, circle YES or NO to indicate whether or not it has a stack frame on the call
stack at the moment when the error occurs.
bumbly YES NO
mia YES NO
chirly YES NO
__main__ YES NO
3. What is a key-value pair that could be added to the dictionary d above (before calling the bumbly function
in the main code) to prevent any KeyError from occuring in this code?
key: value:
Question 5. [5 marks]
Consider the following Stack implementation, which uses an internal Queue (as defined on the Aid Sheet) to
store its data. Complete the push and pop methods. Do not add code anywhere else.
Hint: for one or both methods below, you may need to create a temporary second Stack or Queue.
class Stack:
"""A last-in-first-out (LIFO) stack of items.
"""
# === Private attributes ===
# _items: The items stored in this stack. The FRONT of the _items Queue
# represents the top of this stack.
_items: Queue
2. What is the Big-Oh runtime of your function, given a lst of length n where every value is v:
3. Complete the following LinkedList method. You must not make any method or function calls. For
example, your implementation must not call the LinkedList.remove method.
def remove_all(self, v: Any) -> None:
"""Remove all occurences of <v> from this linked list.
>>> lst = LinkedList([2, 1, 3, 1, 4, 1])
>>> lst.remove_all(1)
>>> print(lst)
[2 -> 3 -> 4]
"""
4. What is the Big-Oh runtime of your method on a LinkedList of length n, in the worst case:
April 2022 Examinations
CSC 148 H1S
Duration: 3 hours
Question 7. [6 marks]
Part (a) [2 marks]
Suppose we have a stack of s items, implemented as a linked list with the bottom of the stack at the front of the
linked list and the top of the stack at the end. For each operation, give its big-oh time-complexity, in terms of s.
push
pop
new = []
for item in lst:
new.insert(0, item)
new = []
for i in range(len(lst) // 2):
new.append(lst[i])
Question 8. [5 marks]
Consider the mystery function below. Complete the four given doctest examples, and write a good, plain English
docstring description for the function explaining its purpose.
You may assume that obj is a nested list, following the definition we have seen in this course. That is, it is
either a single integer or a list of other nested lists. Note that obj has arbitrary depth, and if it is simply an
integer, its depth is 0.
"""
if isinstance(obj, int):
return obj == n and d >= 0
else:
for element in obj:
if mystery(element, n, d - 1):
return True
return False
April 2022 Examinations
CSC 148 H1S
Duration: 3 hours
Question 9. [8 marks]
Assume that a Tree class has been defined as described on the Aid Sheet. The following recursive method is to
be added to the class. Complete the method according to its docstring.
A string s occurs along a path in a tree iff s[0] is the value at the root, s[1] is the value
at one of the root’s children, s[2] is the value at one of that node’s children, and so on,
and the last character in s is a leaf. No string occurs in an empty tree.
Which pair of lists would require fewer comparisons to merge (using the merge algorithm from mergesort)?
Part (b) [1 mark] Consider the problem of constructing a binary search tree of height h > 0, such that it
contains the numbers 1, 2, . . . , 2h − 1. Here are a couple of examples of such binary search trees:
h=2 h=3
Given h, provide a mathematical expression for the value that must go at the root of the tree:
April 2022 Examinations
CSC 148 H1S
Duration: 3 hours
Part (c) [6 marks] Complete the following function according to its docstring, without calling insert.
Note that ** is the exponentiation operator in Python. E.g., 2 ** h is 2h .
Hint 1: How is each value in the right subtree related to the the corresponding value in the left subtree?
Hint 2: You should find the method increase_all_by_x helpful. You may use it even if you didn’t complete it.
Hint 3: When creating a new BinarySearchTree, the initializer lets you optionally specify the root’s children.
Precondition: h > 0.
>>> c = construct_tree(1)
>>> print(c)
1
<BLANKLINE>
>>> print(construct_tree(2))
2
1
3
<BLANKLINE>
"""
April 2022 Examinations
CSC 148 H1S
Duration: 3 hours