Fall 2019 Midterm Solution
Fall 2019 Midterm Solution
Circle the answer that best describes what is printed when the (A) <class ‘list’>
following code is run: (B) <class ‘int’>
(C) <class ‘str’>
s = ‘Oct’
s = s + ‘31’ (D) <class ‘float’>
print(type(s[4])) (E) <class ‘NoneType’>
(F) Nothing is printed because an error occurs
(G) No error occurs, and something else is printed
Circle all of the code snippets that would print rat if (A) print(s[1:3])
(B) print(s[-2] + s[2] + s[1])
s = ‘stars’
(C) print(s[3:0:-1])
(D) print(s[-2:-5:1])
(E) print(s[::-1])
(F) None of the above will print rat
Circle all of the answers that correctly describe what is printed (A) The first number printed is 4
when the following code is run: (B) The first number printed is 9
(C) There are four numbers printed in total
i = 4
(D) There are sixteen numbers printed in total
while i < 20:
(E) The number 15 is printed
print(i) (F) None of the above correctly describe what is printed
i = i + 5
Circle all of the expressions that would evaluate to True: (A) ‘o’ in [‘Welcome’, ‘to’, ‘CSC108’]
(B) len(‘what\’s’) in [2, 4, 6, 8]
(C) [1, 2, 3, 4, 5] in len(‘hello’)
(D) ‘g’ in (‘abc’ + ‘defg’)
(E) ‘aabbcc’ == ‘abc’ * 2
(F) None of the expressions evaluate to True
Circle all of the code snippets that would result in variable f (A) f = 24 (C) f = 12
referring to the int value 12 and variable g referring to the g = 9 g = 3
int value 2. g = f // g if g >= f:
f = f // g f = g
if g < f:
g = g - 1
(B) f = 24
g = 12
g = f / g (D) f = 12 // 2 + 2
f = f / g g = 16
g = g // f
Solution:
1. D 2. C 3. B, C 4. A, C 5. B, D 6. A, C
Question 2. [3 marks]
Part (a) [2 marks]
Consider the problem of writing function are_consecutive_nums that has the following docstring description:
"""Return True if and only if i, j and k are consecutive numbers in ascending order.
>>> are_consecutive_nums(4, 5, 6)
True
>>> are_consecutive_nums(3, 2, 1)
False
>>> are_consecutive_nums(3, 3, 4)
False
"""
Several solution attempts are given below. Some are correct and some are incorrect.
Circle the letter in front of each solution attempt that correctly implements the function.
Solution: A and D.
Part (b) [1 mark]
Fill in the box below to correctly implement function are_consecutive_nums using a single return statement.
Sample solution:
def are_consecutive_nums(i: int, j: int, k: int) -> bool:
return
i == j - 1 and j == k - 1 # Another solution: i + 1 == j and j + 1 == k
Question 3. [4 marks]
Complete the following function according to its docstring. You must use the constant VOWELS in your solution.
VOWELS = ‘aeiou’
>>> get_lowercase_vowels(‘aardvark’)
‘aaa’
>>> get_lowercase_vowels(‘TWO camels!’)
‘ae’
>>> get_lowercase_vowels(‘123xyzABC’)
‘’
"""
Sample solutions:
# Possible solution:
vowels = ‘’
for ch in s:
if ch in VOWELS:
vowels = vowels + ch # Alternative: vowels += ch
return vowels
for i in range(len(s)):
if s[i] in VOWELS:
vowels = vowels + s[i] # Alternative: vowels += s[i]
return vowels
Question 4. [4 marks]
Fill in the boxes below to correctly complete the body of function get_sum_matches according to its docstring.
Do not change the code outside of the boxes.
Sample solutions:
# Possible solution
matches = []
for i in range(1, len(lst) - 1): # don’t loop over first and last indexes
if lst[i - 1] + lst[i + 1] == lst[i]:
matches.append(lst[i]) # matches.extend([lst[i]])
# matches = matches + [lst[i]]
return matches
return matches
Question 5. [4 marks]
Part (a) [2 marks]
Fill in the box with the while loop condition required for the function to work as described in the docstring. Do not
change the code outside of the box.
>>> find_index_of_last_digit(‘csc108’)
5
>>> find_index_of_last_digit(‘801csc’)
2
>>> find_index_of_last_digit(‘Comp Sci’)
-1
"""
i = len(s) - 1
>>> find_index_of_first_digit(‘csc108’)
3
>>> find_index_of_first_digit(‘801csc’)
0
>>> find_index_of_first_digit(‘Comp Sci’)
8
"""
Question 6. [4 marks]
Complete the following function according to its docstring. You must use the constant PUNCTUATION in your
solution.
PUNCTUATION = ‘?!.,’
>>> is_punctuated_tweet_word("hello_world!")
True
>>> is_punctuated_tweet_word("WeTheNorth!")
True
>>> is_punctuated_tweet_word("how?are?you?")
False
>>> is_punctuated_tweet_word("Yes!!!")
False
"""
Sample solution:
if not (s[-1] in PUNCTUATION):
return False
Use the space on this “blank” page for scratch work, or for any solution that did not fit elsewhere.
Clearly label each such solution with the appropriate question and part number.
Total
Gedownload door Camila Marks
Calderon Cruz=([email protected])
25