Expressions, Types and Memory · Predict the output · 2 marks
A race result is stored as a number of seconds. Write the output of the following code fragment. If the code raises an error, write ERROR and stop.
seconds = 7385
hours = seconds // 3600
minutes = seconds % 3600 // 60
secs = seconds % 60
print(hours, minutes, secs, sep=':')
print(round(seconds / 3600, 2), 'hours')2:3:5
2.05 hours7385 % 3600 is 185 and 185 // 60 is 3 (% and // have the same precedence and are applied left to right). 7385 / 3600 is about 2.0514, which rounds to 2.05.
Booleans and Conditionals · Write code · 3 marks
Complete the function body according to its docstring. Do not use min, lists or sorting.
def first_name(name1: str, name2: str, name3: str) -> str:
"""Return whichever of name1, name2 and name3 comes first in
alphabetical order when case is ignored.
Precondition: the three names are different from each other
when case is ignored.
>>> first_name('maya', 'Liam', 'noah')
'Liam'
>>> first_name('Zoe', 'adam', 'Ben')
'adam'
""" a = name1.lower()
b = name2.lower()
c = name3.lower()
if a < b and a < c:
return name1
elif b < c:
return name2
else:
return name3The original name must be returned, not the lowercase copy. Comparing without lower() would make 'Zoe' come before 'adam'.
For and While Loops · Complete the code · 3 marks
Fill in the blanks to complete the function. Do not change any other code. Do not convert n to a string.
def reverse_digits(n: int) -> int:
"""Return the number whose digits are the digits of n in reverse
order.
Precondition: n > 0 and n % 10 != 0
>>> reverse_digits(1234)
4321
>>> reverse_digits(7)
7
"""
result = __________
while ____________________:
result = ______________________________________
n = ______________________
return result result = 0
while n > 0:
result = result * 10 + n % 10
n = n // 10
return resultNested Lists and Loops · Predict the output · 3 marks
Write the output of the code below. If the code raises an error, write ERROR and stop.
teams = [[3, 'Lynx'], [5, 'Owls'], [3, 'Bees'], [5, 'Ants']]
print(max(teams))
teams.sort()
print(teams[0])
print(teams[-1][1])
print(teams.index([5, 'Ants']))
print(min(teams[1]))[5, 'Owls']
[3, 'Bees']
Owls
2
ERRORLists are compared item by item: first by the number, then (on a tie) by the name. The last line tries to compare an int with a str, which raises a TypeError.
Searching, Sorting and Complexity · Multiple choice · 1 mark
For which list does the course bubble_sort make the most swaps?
[1, 2, 3, 4, 5]
[2, 1, 4, 3, 5]
[1, 5, 2, 4, 3]
[5, 4, 3, 2, 1]
Answer: (D)
(D). Every pair of items is out of order, so all comparisons cause a swap. The others make 0, 2 and 4 swaps.
Introduction to Computer Programming · University of Toronto (UofT, U of T, UToronto) · also written CSC180, CSC 180
Make a free, printable CSC180H1 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 CSC180H1 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.