coms1022a_test2 (1)
coms1022a_test2 (1)
(COMS1022A)
Class Test 1
10 April 2024
Question 1
Question 2
Question 3
Question 4
Total
Instructions
• Answer all questions in pen. Do not write in pencil.
• This test consists of 8 pages. Ensure that you are not missing any pages.
• This is a closed-book test: you may not consult any written material or notes.
1
COMS1022A Introduction to Algorithms and Programming 10 April 2024
[0.5 each]
2. What is the difference between a dynamically typed language and a statically typed
language? Provide an example that demonstrates Python’s dynamic typing abili-
ties. [2]
Dynamic: the type of a variable can change over time; static: variable types are fixed
forever
e.g.
= is assignment
Any two:
eases maintenance, easy to read, easy to distribute labour, easy to reuse, etc
[1 mk each]
Page 2 of 8
COMS1022A Introduction to Algorithms and Programming 10 April 2024
5. Alice wrote some Python code, but when she tried to execute it, the interpreter dis-
played the error: TypeError: unsupported operand type(s) for +:
’int’ and ’str’. Give one potential cause for the error. [1]
Alice attempted to add a string to an integer e.g. "The answer is " + 4 [1 mk]
6. Mary has written code that stores all the first names of students in her class in
a container. However, after adding everyone’s name to the container, when she
computes its length she finds that there are fewer names than people in the class.
What container has she likely used? Provide a reason for your answer. [2]
A set. If there are peopel sharing names, then the set will drop duplicates, meaning
0.5 each
Page 3 of 8
COMS1022A Introduction to Algorithms and Programming 10 April 2024
Question 2 [5 Marks]
1. Consider the function below:
What does the function print to screen when we invoke f as follows: [2]
2. How many times does line 2 execute in the code below? [1]
10
3. How many times does line 10 execute in the code below? [2]
1 i = 2
2 while i < 5:
3 i = i + 1
4 j = 1
5 while j < i:
6 if j % i != 0:
7 j = j + 1
8 else:
9 j = j + 2
10 print(j)
9 2mk
Page 4 of 8
COMS1022A Introduction to Algorithms and Programming 10 April 2024
Question 3 [4 Marks]
1. Bob has written some code to determine if the user has entered a number that
is positive, negative or zero, but there are a number of errors that will prevent it
from executing successfully. Identify and suggest a way to fix each error. [2]
1 number = int(input())
2 if number == 0:
3 print("You entered zero")
4 else if number > 0:
5 print("You entered a positive number")
6 else number < 0:
7 print("You entered a negative number")
XXXXXXXXXXX
Line 2: need ==
[1mk each]
2. Eve has written a function that accepts a list of names and returns a dictionary that
associates each name with how many times it appears in the list. However, there
are two errors in her code that will prevent it from executing successfully. Identify
and suggest a way to fix each error. [2]
1 def count(names):
2 counts = dict()
3 N = len(names)
4 for i in range(N + 1):
5 name = names[i]
6 if name not in counts:
7 counts[name] = counts[name] + 1
8 else:
9 counts[name] = 1
10 return counts
Line 6: the not in is the wrong way round, it should be if name in counts
[1 mk each]
Page 5 of 8
COMS1022A Introduction to Algorithms and Programming 10 April 2024
Question 4 [4 Marks]
1. Bob and Alice are attempting to solve the following problem: given an unordered
list of integers (that we assume to be all unique) and a target integer value, find
all pairs of integers that sum up to the target value. For example, the list [4, 1,
6, 3] with target value 7 has two such pairs: [1, 6] and [4, 3]. Note that the order
of the pairs is not important.
Alice has written the following function to solve the problem:
(a) Assuming that there are N elements in the list nums, how many times is
the if-statement (line 6) in the function find pairs alice executed? The
following identity may be helpful in your calculation: [2]
N
X N (N + 1)
i=
2
i=1
When i is 0, inner loop runs (N-1) times. When i=1, inner loop runs (N-2)
times, ... when i=N-1, loop runs 0 times. So total times inner loop (and hence
Page 6 of 8
COMS1022A Introduction to Algorithms and Programming 10 April 2024
(b) As the size of the list nums grows larger and larger, whose function do you
think will compute an answer faster? Motivate your answer, making reference
to your solution from (a). [2]
statement will run N times. If we plot these two functions, one is quadratic
and one is linear. And as N grows, Alice's function wil be much larger
than Bob's, which means more if statements are being executed, more
Page 7 of 8
COMS1022A Introduction to Algorithms and Programming 10 April 2024
Page 8 of 8