Expressions, Types and Memory · Trace the code · 3 marks
Assume x = 7, y = 2 and z = 3.0 have been run. Give the value of each expression, showing float values with a decimal point.
| Expression | Value |
x // y | |
x % y | |
x // z | |
x % z | |
y ** z | |
(x + y) // z |
Answer: 3; 1; 2.0; 1.0; 8.0; 3.0
See the completed table above. Whenever one operand is a float, //, % and ** also give a float.
Booleans and Conditionals · Predict the output · 2 marks
Write the output of the following code.
def check(n: int) -> str:
if n > 10:
return 'big'
elif n > 5:
print('medium')
print(check(12))
print(check(7))
print(check(2))big
medium
None
Nonecheck(7) prints medium inside the function and then returns None, which the outer print displays. check(2) runs no branch and also returns None.
Lists, Mutability and Aliasing · Find the bug · 4 marks
The function below does not work as its docstring says.
def sorted_copy(values: list[int]) -> list[int]:
"""Return a new list containing the items of values in
non-decreasing order. values is not changed.
>>> nums = [4, 1, 3]
>>> sorted_copy(nums)
[1, 3, 4]
>>> nums
[4, 1, 3]
"""
result = values
result = result.sort()
return resultSuppose nums = [4, 1, 3]. What does sorted_copy(nums) return, and what is the value of nums after the call? [2]
Rewrite lines 11–13 so that the function is correct. [2]
It returns None (sort returns None), and nums becomes [1, 3, 4] because result is an alias of values, so the list passed in is sorted.
result = values[:]
result.sort()
return resultTuples and Dictionaries · Complete the code · 2 marks
Consider this function:
def mystery(d: dict[str, int]) -> list[str]:
result = []
for k in d:
if d[k] > len(k):
result.append(k)
return resultComplete the docstring example by filling in the return value. [1]
>>> mystery({'sun': 4, 'a': 1, 'rain': 9, 'fog': 2})Give a dictionary with exactly three keys for which mystery returns ['ab']. [1]
['sun', 'rain']
Any dictionary with keys 'ab' and two others, where only 'ab' maps to a number greater than its length, e.g. {'ab': 5, 'c': 0, 'xyz': 3}. ('a': 1 does not count: 1 is not greater than 1.)
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 Box:
"""A labelled storage box."""
def __init__(self, label: str) -> None:
self.label = label
def relabel(box: Box, label: str) -> None:
box.label = label
def replace(box: Box, label: str) -> None:
box = Box(label)
box.label = box.label.upper()
b1 = Box('books')
b2 = b1
relabel(b2, 'toys')
print(b1.label)
replace(b1, 'tools')
print(b1.label, b2.label)
b2 = Box('maps')
print(b1.label, b2.label, b1 is b2)toys
toys toys
toys maps Falserelabel mutates the object both variables refer to. In replace, box = Box(label) only rebinds the local variable, so the caller’s box is unchanged.
Introduction to Computer Programming · University of Toronto (UofT, U of T, UToronto) · also written CSC108, CSC 108
Make a free, printable CSC108H1 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,200 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 CSC108H1 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.