112 Practice Materials
Mini Exam
A quiz sized version of a full length exam. Based on F24 course materials and assessments.
Duration: 30 minutes
Total Points: 50
Short Answer
Answer each question in 3 words or less
What is returned when a function has no return statement? (4 points)
What method adds an element to the end of a list? (3 points)
What is selection sort's time complexity? (4 points)
Code Tracing(10 points each)
For each code block, write what will be printed
def ct2(L):
return [L.index(v)**2 for v in L if v%2 == 0]
print(ct2([4, 3, 2, 3, 4]))
class A:
def __init__(self, x): self.x = x
def f(self): return 10*self.x
class B(A):
def f(self): return 1000*self.x
print(B(7).f())
Free Response
Recursive List Processing (25 points)
Write a recursive function flattenSum(L) that takes a nested list of numbers and returns the sum of all numbers in the list. A nested list is a list that can contain both numbers and other lists, which themselves can contain numbers and lists, and so on.
For example:
assert(flattenSum([1, 2, 3]) == 6)
assert(flattenSum([1, [2, 3], 4]) == 10)
assert(flattenSum([]) == 0)
Note:
- You must use recursion to solve this problem
- The list can be nested to any depth
- Empty lists sum to 0
- You may assume the list only contains numbers and other lists