Welcome to Foundations of Business Programming


Download Spring 2026 Practice Final

Spring 2026 Final Exam Practice - Solution Key

1) Multiple Choice

  1. 1.1 # 2. 3
  2. 1.2 # 1. [n**2 for n in nums]
  3. 1.3 # 1. It automatically closes the file when the block is done
  4. 1.4 # 2. Python runs the matching except block
  5. 1.5 # 2. import math
  6. 1.6 # 2. Skips the rest of the current iteration and moves to the next one
  7. 1.7 # 4. 5
  8. 1.8 # 1. x > 10 and x < 20

2) True/False

  1. 2.1 True
  2. 2.2 True
  3. 2.3 True
  4. 2.4 True
  5. 2.5 True
  6. 2.6 True
  7. 2.7 False
  8. 2.8 False

3) Code Tracing

  1. 3.1
    [4, 8]
  2. 3.2
    2
  3. 3.3
    15
  4. 3.4
    60

4) Open Ended

4.1

def positiveSquares(L):
    return [x**2 for x in L if x > 0]

4.2

def countLines(filename):
    count = 0
    with open(filename) as f:
        for line in f:
            count += 1
    return count

4.3

def safeInt(s):
    try:
        return int(s)
    except ValueError:
        return None

Download Spring 2026 Midterm

Spring 2026 Midterm — Solution Key

1) Multiple Choice

  1. 1.1 # 2. 4
  2. 1.2 # 1. ababab
  3. 1.3 # 2. [1,2,3]
  4. 1.4 # 2. ==
  5. 1.5 # 2. Exits the loop
  6. 1.6 # 2. 0,1,2
  7. 1.7 # 3. Index-value pairs
  8. 1.8 # 3. <class 'tuple'>

2) True/False

  1. 2.1 True
  2. 2.2 True
  3. 2.3 False
  4. 2.4 True
  5. 2.5 False
  6. 2.6 False
  7. 2.7 True
  8. 2.8 True

3) Code Tracing

  1. 3.1
    The while loop prints x while x < 4, increasing by 1 each time.
    1
    2
    3
  2. 3.2
    The loop uses indices 0, 1, and 2 to print each element of L.
    1
    3
    5
  3. 3.3
    g(2) is 6 and g(1) is 3, so the total is 9.
    9
  4. 3.4
    Only even numbers are added: 4 + 6 = 10.
    10

4) Open-Ended

4.1 Write a function countGreater that counts how many numbers in a list are greater than a given value k.

def countGreater(L, k):
    count = 0
    for x in L:
        if x > k:
            count += 1
    return count

4.2 Write a function sumList that returns the sum of all numbers in a list.

def sumList(L):
    total = 0
    for x in L:
        total += x
    return total

4.3 Write a function reverseList that returns a new list with the elements reversed.

def reverseList(L):
    out = []
    for i in range(len(L) - 1, -1, -1):
        out.append(L[i])
    return out
return L[::-1] would also be a correct shorter answer.

Download Spring 2026 Practice Midterm

Spring 2026 Practice Midterm — Solution Key

1) Multiple Choice

  1. 1.1 # 3. 5
  2. 1.2 # 2. (1,2,3)
  3. 1.3 # 2. A B C
  4. 1.4 # 3. Checks whether x appears as an element of L
  5. 1.5 # 3. while
  6. 1.6 # 2. 4
  7. 1.7 # 2. {1:2,3:4}
  8. 1.8 # 3. Skips to the next iteration

2) True/False

  1. 2.1 True
  2. 2.2 False
  3. 2.3 True
  4. 2.4 True
  5. 2.5 True
  6. 2.6 True
  7. 2.7 True
  8. 2.8 True

3) Code Tracing

  1. 3.1
    The loop prints each value in L divided by 2 using integer division.
    1
    2
    3
  2. 3.2
    This adds 1 + 2 + 3 + 4, so the final value is 10.
    10
  3. 3.3
    The function returns the sum of its two inputs.
    7
    7
  4. 3.4
    The loop skips index 1, so it prints the values at indices 0 and 2.
    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).

Download Fall 25 midterm

Fall 25 Midterm — Solution Key

1) Multiple Choice

  1. 1.1 # 1. set(L)
  2. 1.2 # 3. Returns s reversed
  3. 1.3 # 1. [w[0].upper() for w in words if len(w) >= 4]
  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.
  5. 1.5 # 3. "a"
  6. 1.6 # 2. continue starts the next loop condition check
  7. 1.7 # 1 and 3
    Those three print exactly a,b,c.

2) True/False

  1. 2.1 False
  2. 2.2 True
  3. 2.3 False
  4. 2.4 True
  5. 2.5 False
  6. 2.6 True

3) Code Problem Identification & Short Traces

  1. 3.1 # 1. 1
    Loop prints the value for key "a" then breaks when it sees "b".
  2. 3.2 # 2. [1, 2, 3, [4, 5]]
  3. 3.3 # 3. 5
    Computed as b + kw["x"] + len(args) = 1 + 2 + 2 = 5.
  4. 3.4 # 3. 4
    The loop adds 1 and 3 to s, skipping 2; final s is 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