Welcome to Foundations of Business Programming
Download Spring 2026 Practice Midterm
Spring 2026 Practice Midterm — Solution Key
1) Multiple Choice
- 1.1 # 3.
5 - 1.2 # 2.
(1,2,3) - 1.3 # 2.
A B C - 1.4 # 3. Checks whether
xappears as an element ofL - 1.5 # 3.
while - 1.6 # 2.
4 - 1.7 # 2.
{1:2,3:4} - 1.8 # 3. Skips to the next iteration
2) True/False
- 2.1 True
- 2.2 False
- 2.3 True
- 2.4 True
- 2.5 True
- 2.6 True
- 2.7 True
- 2.8 True
3) Code Tracing
-
3.1
The loop prints each value in
Ldivided by 2 using integer division.1 2 3 -
3.2
This adds
1 + 2 + 3 + 4, so the final value is10.10 -
3.3
The function returns the sum of its two inputs.
7 7 -
3.4
The loop skips index
1, so it prints the values at indices0and2.10 30
4) Open-Ended
4.1 Write a function countOdd that returns the number of odd integers in a list.
def countOdd(L):
count = 0
for x in L:
if x % 2 == 1:
count += 1
return count
4.2 Write a function maxDiff that returns the difference between the largest and smallest value in a list.
def maxDiff(L):
biggest = L[0]
smallest = L[0]
for x in L:
if x > biggest:
biggest = x
if x < smallest:
smallest = x
return biggest - smallest
4.3 Write a function sumIndices that returns the sum of the values at even indices.
def sumIndices(L):
total = 0
for i in range(len(L)):
if i % 2 == 0:
total += L[i]
return total
Another correct approach is to loop with a step of 2, for example
for i in range(0, len(L), 2).DownloadFall 25 midterm
Fall 25 Midterm — Solution Key
1) Multiple Choice
-
1.1
# 1.
set(L) -
1.2
# 3. Returns
sreversed -
1.3
# 1.
[w[0].upper() for w in words if len(w) >= 4] -
1.4 *this was extra credit. any answer correc. any answer correct, bonus for stating:
enumerate returns pairs of (index,value) pairs. Index first, value second.
-
1.5
# 3.
"a" -
1.6
# 2.
continuestarts the next loop condition check -
1.7
# 1 and 3
Those three print exactly
a,b,c.
2) True/False
- 2.1 False
- 2.2 True
- 2.3 False
- 2.4 True
- 2.5 False
- 2.6 True
3) Code Problem Identification & Short Traces
-
3.1
# 1.
1Loop prints the value for key"a"then breaks when it sees"b". -
3.2
# 2.
[1, 2, 3, [4, 5]] -
3.3
# 3.
5Computed asb + kw["x"] + len(args) = 1 + 2 + 2 = 5. -
3.4
# 3.
4The loop adds 1 and 3 tos, skipping 2; finalsis 4.
4) Open-Ended
4.1 Write a function that returns a new list containing the elements at odd indices of a given list L.
def elements_at_odd_indices(L):
"""Return a new list with elements from odd indices of L."""
odd_idx_list = []
for i in range(len(L)):
if i%2 == 1: # check if *index* is odd
odd_idx_list.append(L[i]) # add *value* at that index
return odd_idx_list
or
def elements_at_odd_indices(L):
"""Return a new list with elements from odd indices of L."""
return [L[i] for i in range(1, len(L), 2)]
or
def elements_at_odd_indices(L):
return L[1::2]
4.2 Program that opens "nums.txt" and computes the average of the integers (one per line).
total = 0
with open("nums.txt") as f:
lines = f.readlines() # returns list of strings, each string one line
for line in lines:
total += int(line)
avg = total / len(lines)
print(avg)
4.3 One-line list comprehension for cubes of nums = [3, 6, 9, 12, 15].
cubes_even = [n**3 for n in nums]
or>
cubes_even = [n*n*n for n [3,6,9,12,15]]
Course Schedule
| Week | Topic | Items Due |
|---|---|---|
| 1 | Intro to terminal/python | |
| 2 | Built-in types: String, Integer, Float, list, tuple |
|
| 3 | Built-in types: dict, Functions: def, return, *args, **kwargs |
|
| 4 | Conditionals: if, elif, else |
HW1 released |
| 5 | Loops: for and while loops |
HW1 due |
| 6 | Nested loops, break, continue, enumerate |
|
| 7 | Comprehensions: List, dictionary, and set comprehensions | HW2 released |
| 8 | File I/O: open and with, Exceptions: try/except |
HW2 due |
| 9 | Command-line Arguments: sys.argv, argparse |
Midterm |
| 10 | Scripts vs Modules & Packages: import, from, as,if __name__=='__main__',__init__.py, __main__.py |
HW3 released |
| 11 | General Programming: language model back-end | HW3 due, HW4 released |
| 12 | General Programming: language model front-end / Object Oriented Programming | HW4 due, HW4.5 released |
| 13 | General Programming: program 2 (TBD) | HW4.5 due, HW5 released |
| 14 | General Programming: program 2 (TBD) | |
| 15 | Review | HW5 due |
| 16 | Final exams / Final projects |