Expressions, Types and Memory · Multiple choice · 2 marks
Variables x and y refer to two different ints. Which code fragments swap their values? Circle ALL that apply.
x = y then y = x
temp = x then x = y then y = temp
x, y = y, x
temp = y then y = x then x = temp
temp = x then y = x then x = temp
x = x + y then y = x - y then x = x - y
Answer: (B, C, D, F)
The second, third, fourth and sixth fragments. The first sets both to the old y; the fifth sets both to the old x. The last one works for numbers: after line 1 x holds the sum, so x - y is the old x and the new x - y is the old y.
Booleans and Conditionals · Write code · 2 marks
Complete the function body according to its docstring.
def triangle_type(a: int, b: int, c: int) -> str:
"""Return 'equilateral' if the side lengths a, b and c are all
equal, 'isosceles' if exactly two of them are equal, and
'scalene' if no two of them are equal.
Precondition: a, b and c are the sides of a valid triangle.
>>> triangle_type(3, 3, 3)
'equilateral'
>>> triangle_type(5, 3, 5)
'isosceles'
>>> triangle_type(3, 4, 5)
'scalene'
""" if a == b == c:
return 'equilateral'
elif a == b or b == c or a == c:
return 'isosceles'
else:
return 'scalene'Checking the equilateral case first matters: an equilateral triangle also satisfies the isosceles condition. Forgetting the a == c comparison is a common mistake.
Lists, Mutability and Aliasing · Write code · 3 marks
Complete the function body according to its docstring.
def above_average(marks: list[int]) -> list[int]:
"""Return a new list of the items in marks that are strictly
greater than the average of marks, in their original order.
Precondition: len(marks) > 0
>>> above_average([65, 80, 72, 91])
[80, 91]
>>> above_average([50, 50])
[]
""" total = 0
for mark in marks:
total = total + mark
average = total / len(marks)
result = []
for mark in marks:
if mark > average:
result.append(mark)
return resultThe average must be computed in a first loop, before any item is compared.
Tuples and Dictionaries · Write code · 3 marks
Complete the function body according to its docstring. You may not use sum.
def student_averages(grades: dict[str, dict[str, int]]
) -> dict[str, float]:
"""Return a new dictionary that maps each student in grades to
the average of that student's marks.
grades maps each student to a dictionary that maps course codes
to the student's mark in that course.
Precondition: every inner dictionary is non-empty.
>>> marks = {'Ana': {'CSC108': 80, 'MAT137': 70},
... 'Raj': {'CSC108': 91}}
>>> student_averages(marks) == {'Ana': 75.0, 'Raj': 91.0}
True
""" result = {}
for student in grades:
total = 0
for course in grades[student]:
total = total + grades[student][course]
result[student] = total / len(grades[student])
return resultCommon mistake: initializing total = 0 outside the outer loop, so marks carry over from one student to the next.
Classes and Objects · Multiple choice · 1 mark
Class Thermometer has a method to_fahrenheit whose only parameter is self, and t refers to a Thermometer object. Which of these expressions correctly call the method on t? Circle ALL that apply.
t.to_fahrenheit()
Thermometer.to_fahrenheit(t)
to_fahrenheit(t)
t.to_fahrenheit(t)
Thermometer.to_fahrenheit()
Answer: (A, B)
t.to_fahrenheit() is shorthand for Thermometer.to_fahrenheit(t). The others raise errors: the method is not a global function, and self must be given exactly once.
Fundamentals of Computer Programming · University of Toronto (UofT, U of T, UToronto) · also written APS106, APS 106
Make a free, printable APS106H1 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,100 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 APS106H1 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.