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.
Functions and the Design Recipe · Write code · 1 mark
Complete the function body according to its docstring.
def body_mass_index(weight: float, height: float) -> float:
"""Return the body mass index (weight divided by the square of
height) for a person who weighs weight kg and is height metres
tall, rounded to 1 decimal place.
Precondition: height > 0
>>> body_mass_index(70.0, 1.75)
22.9
>>> body_mass_index(50.0, 1.6)
19.5
""" return round(weight / height ** 2, 1)weight / height * height is wrong: it divides and then multiplies by height again.
Strings · Predict the output · 2 marks
Write the output of the code fragment. If the code raises an error, write ERROR and stop.
print('a', 'b', 'c', sep='')
print('a', 'b', sep='-', end='!')
print('c')
print('x' + 'y' * 2, ('x' + 'y') * 2)
print('1' + '2', 1 + 2, sep=' vs ')abc
a-b!c
xyy xyxy
12 vs 3* is done before +, and end='!' replaces the newline, so 'c' is printed on the same line.
For and While Loops · Predict the output · 2 marks
Write the output of the following code. If the code raises an error, write ERROR and stop.
def has_digit(s: str) -> bool:
for ch in s:
if ch.isdigit():
return True
else:
return False
print(has_digit('a1'))
print(has_digit('1a'))
print(has_digit(''))False
True
NoneThe function always returns during the first iteration. For the empty string the loop body never runs, so the function reaches its end and returns None.
Reading and Writing Files · Write code · 2 marks
A cafe records its sales in a file. Each line has the form date,product,amount, for example:
2024-03-01,tea,4.50
2024-03-01,cake,6.25
2024-03-02,tea,3.00Complete the function body according to its docstring.
def total_sales(sales_file: TextIO, product: str) -> float:
"""Return the total amount of all sales of product in sales_file.
Return 0.0 if product does not appear in sales_file.
"""For the example file, total_sales(sales_file, 'tea') returns 7.5.
total = 0.0
for line in sales_file:
fields = line.strip().split(',')
if fields[1] == product:
total = total + float(fields[2])
return totalIntroduction to Programming for Engineers · University of Waterloo (Waterloo, UWaterloo, UW) · also written NE111
Make a free, printable NE 111 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 700 original questions and skips the ones you have already seen.
Start with the past exams and tests your instructor or the Waterloo library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh NE 111 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 Waterloo. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.