Expressions, Types and Memory · Memory model · 2 marks
Consider the following code.
a = 'red'
b = a
a = a + 'dish'
c = bWhich statements are true after the code runs? Circle ALL that apply.
b and c refer to the same object.
a and b refer to the same object.
The object that a referred to on line 1 now has the value 'reddish'.
Exactly two different str objects are referred to by a, b and c.
id(a) == id(c) is True.
c == 'red' is True.
Answer: A, D, F
The first, fourth and sixth statements. Line 3 creates a new string object for a; b (and then c) still refer to the original 'red' object, which cannot be changed.
Booleans and Conditionals · Multiple choice · 2 marks
Suppose s = '15'. Which of the following expressions evaluates to True without raising an error?
s > 9
s > '9'
float(s) == '15.0'
s == 15
int(s) > 9
Answer: (E)
(E). (A) raises a TypeError because it compares a str with an int. (B) compares strings character by character, and '1' < '9', so it is False. (C) and (D) compare a number with a string, which is always False.
For and While Loops · Complete the code · 2 marks
Fill in the blanks to complete the function. Do not change any other code.
def first_word(sentence: str) -> str:
"""Return the characters of sentence up to but not including its
first space. If sentence has no space, return all of sentence.
>>> first_word('library books are due')
'library'
>>> first_word('overdue')
'overdue'
"""
i = 0
while ____________________________________________:
i = i + 1
return ________________________ while i < len(sentence) and sentence[i] != ' ':
i = i + 1
return sentence[:i]The bound check i < len(sentence) must come first; with the operands swapped, first_word('overdue') raises an IndexError.
Nested Lists and Loops · Write code · 3 marks
A transit agency counts how many people board each bus trip at each stop: riders[t][s] is the number of people who boarded trip t at stop s. Complete the function body according to its docstring.
def busiest_stop(riders: list[list[int]]) -> int:
"""Return the index of the stop with the most boardings over all
trips. If several stops tie, return the smallest such index.
Precondition: riders is not empty, all of its rows have the same
non-zero length, and every item is >= 0.
>>> busiest_stop([[4, 10, 2], [6, 1, 9], [5, 0, 7]])
2
>>> busiest_stop([[1, 2], [2, 1]])
0
""" best_stop = 0
best_total = -1
for s in range(len(riders[0])):
total = 0
for trip in riders:
total = total + trip[s]
if total > best_total:
best_stop = s
best_total = total
return best_stopCommon mistake: summing rows (trips) instead of columns (stops), or using >= so that ties return the largest index.
Searching, Sorting and Complexity · Short answer · 2 marks
For each situation, say whether you would use linear search or binary search, and give a one-sentence reason.
A list of 80,000 library book IDs is kept sorted. The checkout desk searches it thousands of times every day. [1]
A list of the 300 card transactions made today, in the order they happened, is searched once at the end of the day for a particular amount. [1]
(a) Binary search: the list is already sorted and each search takes about 17 iterations instead of up to 80,000.
(b) Linear search: the list is not sorted, and sorting it just to do one search costs more than one linear pass. Binary search on the unsorted list would give wrong answers.
Introduction to Computer Programming · University of Toronto (UofT, U of T, UToronto) · also written ESC180, ESC 180
Make a free, printable ESC180H1 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 ESC180H1 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.