About the Final Exam

CS 149, 05-Dec-2025

JMU Department of Computer Science Python Powered

Format and Length

  • Written potion: on paper
    • Turn off monitor
  • Coding potion: on computer
    • You must use Thonny
    • Submit to gradescope
    • Log out when finished
  • About as long as 3 quizzes
    • So, about 90 minutes long
    • And you have 120 minutes
  • Attachments:
    • Program to Read / Test
    • JSON Data File Snippet
    • Python Reference Sheet

You've done this 12 times!

  • See practice quizzes on CS-149-F25.
  • See actual quizzes on Gradescope.
  • Practice solving similar problems!

Tentative Outline


Written Portion (60 pts)

Q# Topic Points
1 Evaluate Expressions 12 pts
2 Trace Nested If-Else 10 pts
3 Trace For/While Loops 12 pts
4 Assert Statements 10 pts
5 List/String Methods 8 pts
6 Recursive Functions 8 pts

Coding Portion (40 pts)

Q# Topic Points
7 Topics from Quiz 1-2 10 pts
8 Topics from Quiz 3-4 15 pts
9 Topics from Quiz 5-6 15 pts

Q1. Evaluate Expressions

  • Determine the type and value
  • Arithmetic, logic, and containers

Expression Type Value
13 % 5
b**2 - 4*a*c
ready or not ready
data["name"][2]

Q2. Trace Nested If-Else

def can_vote(age, registered):
    if registered:
        if age <= 0:
            message = "Input Error: Age is invalid!"
        elif age < 18:
            message = "You have to be at least 18 to vote!"
        else:
            message = "You are eligible to vote!"
    else:
        message = "You are not registered to vote!"
    return message
  • What is the message? can_vote(15, True)
  • What is the message? can_vote(18, False)

Q3. Trace For/While Loops

picks = (44, -17, 253)
for i, x in enumerate(picks):
    if x > 0:
        print(i, x, end=" ")
  • How many times the loop runs?
  • What is printed on the screen?
sci_fi = ("Interstellar",
    "Coherence", "Blade Runner")

a = 0
z = len(sci_fi) - 1
while a <= z:
    print(sci_fi[a])
    a += 3
print(a)

Q4. Assert Statements

Test every branch of the can_vote() function from Q2.

Format:
assert function_call(args) == expected_value

Q5. List/String Methods

s = "  Let's go Python!  "
w = s.split()[1].upper()
z = len(s.strip())
  • What is the type/value of w?
  • What is the type/value of z?
nums = list(range(4))
nums.insert(2, "Hi")
nums.pop()
  • What are the contents of nums?
  • What is nums[-2:]?

Q6. Recursive Functions

def power(base, exp):
    if exp == 0:
        return 1
    else:
        return base * power(base, exp - 1)

power(2, 4)
  • How many times is the power() function called?
  • What is the return value of each function call?
  • Give an example that causes a RecursionError.

Q7. Topics from Quiz 1-2

Statements, Expressions, Branches, Functions

Example: Write shipping_cost(weight) that takes the weight of a package and returns the shipping cost as a float.

  • Packages up to 2 pounds cost $5
  • Packages over 2 pounds and up to 6 pounds cost $10
  • Packages over 6 pounds and up to 10 pounds cost $15
  • Packages over 10 pounds cost $20

Q8. Topics from Quiz 3-4

Containers, While Loops, For Loops, Testing

Example: Write odd_words(words) that takes a list of strings and returns a set containing only the strings with an odd number of characters.

Q9. Topics from Quiz 5-6

Sequences, File I/O, Nested Data, Recursion

Example: Write word_lengths(path):

  • Read a text file and count how many words of each length appear in the file.
  • Return a dictionary where the keys are word lengths and the values are the number of words with that length.

File contents:

Hello world
Python is fun

Example test:

>>> word_lengths("sample.txt")
{5: 2, 6: 1, 2: 1, 3: 1}

You got this! 💪 🧠 🎓

Good luck on all your final exams