Expressions, Types and Memory · Multiple choice · 1 mark
What is printed by the following code?
print('A', 'B', sep='', end='')
print('C', 'D', end='!')ABC D!
AB on one line, then C D! on the next line
ABCD!
A B C D!
ABC!D!
Answer: (A)
ABC D!. The first call prints AB with no newline; the second uses the default sep (a space) between C and D, then ends with !.
Booleans and Conditionals · Testing · 3 marks
Consider this function header and docstring.
def rating_word(stars: int) -> str:
"""Return 'poor' for 1 or 2 stars, 'average' for 3 stars,
and 'good' for 4 or 5 stars.
Precondition: 1 <= stars <= 5
"""A student tests their implementation with stars equal to 1, 3 and 5, and all three tests pass.
Write a body for rating_word that passes all three of these tests but is still incorrect. [2]
Which set of test cases would you choose for this function, and why? [1]
(a) For example (wrong for 2 stars):
if stars < 2:
return 'poor'
elif stars < 4:
return 'average'
else:
return 'good'(b) Test all five values 1 to 5: there are only five valid arguments, and 2 and 4 are the boundaries between categories that the student skipped.
For and While Loops · Predict the output · 2 marks
Write the output of each code fragment. If the code raises an error, write ERROR and stop.
result = ''
for ch in 'stressed':
result = ch + result
print(result)[1]
s = 'banana'
result = ''
for i in range(len(s)):
if i % 2 == 0:
result = result + s[i].upper()
else:
result = result + s[i]
print(result)[1]
(a)
desserts(b)
BaNaNaNested 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.
Testing and Debugging · Find the bug · 3 marks
The function below has a bug.
def clean_title(title: str) -> str:
"""Return title with each underscore replaced by a space and with
leading and trailing spaces removed.
>>> clean_title(' war_and_peace ')
'war and peace'
"""
title.replace('_', ' ')
title.strip()
return titleWhat does the docstring example actually return? [1]
Explain the bug in one sentence. [1]
Rewrite the body so that the function is correct. [1]
' war_and_peace ' (the argument, unchanged).
Strings are immutable: replace and strip return new strings, and the code throws those results away.
Body:
title = title.replace('_', ' ')
title = title.strip()
return titleIntroduction to the Foundations of Computation I · University of Alberta (UAlberta, UofA, U of A) · also written CMPUT174
Make a free, printable CMPUT 174 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 UAlberta library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh CMPUT 174 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 Alberta. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.