Expressions, Types and Memory · Predict the output · 2 marks
Write the output of the following code fragment. If the code raises an error, write ERROR and stop.
print(int(7.9), int(-7.9))
print(round(7.9), round(-7.5))
print(float(4), str(4.0) + '1')
print(int('42') + int(4.2))7 -7
8 -8
4.0 4.01
46int truncates towards zero (-7.9 becomes -7); round goes to the nearest int and breaks the tie -7.5 towards the even value -8.
Booleans and Conditionals · Predict the output · 2 marks
Write the output of the following code.
def check(n: int) -> str:
if n > 10:
return 'big'
elif n > 5:
print('medium')
print(check(12))
print(check(7))
print(check(2))big
medium
None
Nonecheck(7) prints medium inside the function and then returns None, which the outer print displays. check(2) runs no branch and also returns None.
Strings · Predict the output · 2 marks
Write the output of the code fragment. If the code raises an error, write ERROR and stop.
print('It\'s 5 o\'clock')
print('C:\\temp\\new')
print(len('a\tb'), len('\\'), len('\n'))
print('up\ndown')It's 5 o'clock
C:\temp\new
3 1 1
up
downEach escape sequence is a single character.
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.
rows = ['cat', 'ode', 'wet']
diagonal = ''
for i in range(len(rows)):
diagonal = diagonal + rows[i][i]
print(diagonal)
for row in rows:
print(row[-1], end='')
print()
print(rows[1][-1] + rows[-1][1])cdt
tet
eeend='' keeps the three last letters on one line; the empty print() then ends that line.
Classes and Objects · Design · 4 marks
A museum ticket records the visitor’s age (an int) and whether the visitor is a museum member (a bool). The price is 8.0 dollars for visitors under 12, 10.0 for visitors 65 or older, and 15.0 for everyone else; members pay half of that price. Write class Ticket. Every method needs a header with a type contract and a docstring.
Write the class header, the class docstring and the complete __init__ method. [2]
Write a complete method price that returns the price of the ticket. Include two docstring examples. [2]
class Ticket:
"""A museum entry ticket."""
def __init__(self, age: int, member: bool) -> None:
"""Initialize a ticket for a visitor aged age who is a member
if and only if member is True.
"""
self.age = age
self.member = member
def price(self) -> float:
"""Return the price of this ticket in dollars.
>>> Ticket(70, False).price()
10.0
>>> Ticket(30, True).price()
7.5
"""
if self.age < 12:
result = 8.0
elif self.age >= 65:
result = 10.0
else:
result = 15.0
if self.member:
result = result / 2
return resultBoundary cases: age 12 pays 15.0 and age 65 pays 10.0.
Introduction to Computing I · University of Ottawa (uOttawa, U of O, UOttawa) · also written ITI1120
Make a free, printable ITI 1120 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 uOttawa library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh ITI 1120 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 Ottawa. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.