Expressions, Types and Memory · Multiple choice · 1 mark
What is printed by the following line?
print(max(3, 7.0, 5), min(2.0, 2))7 2
7.0 2
7 2.0
ERROR, because int and float values cannot be compared.
7.0 2.0
Answer: (E)
(E). max and min return one of their arguments unchanged, so the largest is the float 7.0. For min(2.0, 2) the two values are equal, and the first one, 2.0, is returned.
Booleans and Conditionals · Multiple choice · 1 mark
Assume a, b and c refer to Boolean values. Python evaluates the expression not a or b and c in the same way as which of the following?
(not a) or (b and c)
not (a or b) and c
((not a) or b) and c
not (a or (b and c))
Answer: (A)
(A). Among the Boolean operators, not binds most tightly, then and, then or.
For and While Loops · Predict the output · 3 marks
Write the output of each code fragment. If the code raises an error, write ERROR and stop.
n = 3
for i in range(n):
n = n + 1
print(i, n)
print(n)[2]
for i in range(3):
print(i, end=' ')
i = i * 10
print(i)[1]
(a)
0 4
1 5
2 6
6(b)
0 0
1 10
2 20range(n) is evaluated once, before the first iteration, and each iteration rebinds i to the next value from the range, whatever the body did to n or i.
Nested Lists and Loops · Predict the output · 2 marks
Write the output of the code below. If the code raises an error, write ERROR and stop.
for i in range(1, 5):
line = ''
for j in range(i, 5):
line = line + str(j)
print(line)
print(i, j)1234
234
34
4
4 4The inner loop starts at i, so each line is one digit shorter. After the loops, i and j keep their last values.
Testing and Debugging · Multiple choice · 1 mark
def smallest(nums: list[int]) -> int:
"""Return the smallest item in nums.
Precondition: len(nums) > 0
"""The test suite already has the arguments [5], [1, 8, 3] and [6, 2, 9]. Which one additional argument adds the most value to the suite?
[4]
[]
[9, 7, 3]
[2, 8, 3]
Answer: (C)
(C). The suite has the smallest item first and in the middle, but never last. [] violates the precondition, and (A) and (D) repeat categories already tested.
Introduction to Programming · McMaster University (McMaster, Mac, MacU) · also written COMPSCI1MD3
Make a free, printable COMPSCI 1MD3 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,000 original questions and skips the ones you have already seen.
Start with the past exams and tests your instructor or the McMaster library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh COMPSCI 1MD3 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 McMaster University. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.