Midterm Exam
April 10, 2023
Instructions. The duration of this exam is 100 minutes.
This exam is individual and closed book. No communication or use of external notes or
resources is allowed.
I know, writing code with pencil-and-paper is hard. You may make up to 3 small errors
(syntax errors, forgotten colons, typos, etc.) without any penalty. After the first 3, you will be
penalized at 0.5 marks per error.
Name
Student number
1
1. Circle the correct answer. No justification is needed. (1 mark each)
(a) Which of the following will print out each element of x, right-aligned with 3 digits
after the decimal point?
i. ’%3f’ % x[i]
ii. f’{x[i]:.3f}’
iii. f’{x[i]:3f}’
iv. ’%.f’ % x[i]
(b) If x is an int and y is a float, which is return type of a function that does return x,
y?
i. Tuple
ii. List
iii. Boolean
iv. IntFloat
(c) What is the output of the following program?
1 def f(x, y):
2 y = 2 * y + x
3 x += 2
4 return y
5 x = 1
6 y = 3
7 while x < 10:
8 x = f(x, y)
9 y += 1
10 print(y)
i. 4
ii. 1
iii. 10
iv. 5
v. 11
(d) Which function has the best design?
(e) Another tracing question not involving functions.
2
2. Write a function which counts how many negative odd numbers there are in a list. The
function must include documentation. For example, if given the list [5, 7.5, -3,
-2, -4, 9, -1], the function should return 2 (because both -3 and -1 are negative odd
numbers). (6 marks)
3
3. This question is in two parts. It deals with a hypothetical word game in which players
score points for making words. Documentation is not required for either part of this
question.
(a) Write a function which determines how many points a word is worth. Your function
will accept 1 string as a parameter, and return an int. You may assume that all
letters in the word are lowercase (a to z). The letters z, x and q are worth 3 points.
The letters j, k and v are worth 2 points. All other letters are worth 1 point. For
example, vixen is worth 8 points. (5 marks)
(b) Write a function which takes a list of words, and returns the highest scoring word
from that list. Your functions will accept 1 list of strings as a parameter, and return
a string. You may assume the list is not empty. You should use your function from
part (a) to write this function. (5 marks)
4
4. Write a complete Python program which reads in two integers from the user, and prints
out the factorial of the larger one minus the factorial of the smaller one. E.g., if the user
enters in 3 and 5, your program will print out 114 (because 5! = 120 and 3! = 6 and
120 − 6 = 114). It is not necessary to write a function for this question. If you do choose
to write a function, documentation is not required. (7 marks)
5
5. The following Python program never stops. It has an infinite loop. It should print 1, 0.1,
0.001, 0.0001, 0.00001 and then stop, but it keeps printing numbers forever. Identify the
problem and change only 1 line of code to fix the problem. (2 marks)
1 x = 1
2 while x != 0.000001:
3 print(x)
4 x /= 10
5 print(’All done!’)
6. The following Python program crashes with an error. The error is as follows:
TypeError: ’<’ not supported between instances of ’int’ and ’str’
Identify the problem and change at most 2 lines of code to fix the problem. (2 marks)
1 def foo(x, y):
2 if x < y:
3 return x + ’0’
4 else:
5 return y + ’0’
6
7 def bar(x, y, z):
8 return foo(x, foo(y, z))
9
10 print(bar(3, 4, 5))
When fixed, it should print out 300, which is the smallest number with two ’0’s on the
end.
6
7. Write a function which accepts 2 parameters, both integers, and returns a list of all
multiples of 5 between them. You may assume that the first number is smaller than the
second number. For example, if the numbers are 3 and 45, you will return the list [5, 10,
15, 20, 25, 30, 35, 40]. Documentation is not required for this function. (5 marks)
7
8. Another question not involving a function.
Empty page.
8
Empty page.