Command Palette

Search for a command to run...

MTH 4300

MTH 4300 Diagnostic Exam

This quiz assesses your readiness based on the Python programming course you completed. It covers core concepts that will be essential as we transition to C++. Answer all questions to the best of your ability — this is not graded, but it will help your instructor understand where to focus early instruction.


Section 1: Code Tracing

For each question, predict the exact output the program would print.


Q1.

What does this program print? Write the output exactly as it would appear.

x = 10

def mystery(n):
    if n <= 1:
        return 1
    return n * mystery(n - 1)

print(mystery(5))
print(x)

Q2.

What does this program print?

nums = [1, 2, 3, 4, 5]
result = []
for i in range(len(nums)):
    if nums[i] % 2 == 0:
        result.append(nums[i] * 2)
    else:
        result.append(nums[i])
print(result)

Q3.

What does this program print? Pay close attention to what is modified and where.

def add_item(lst, item):
    lst.append(item)
    item = 99

my_list = [1, 2, 3]
my_num = 7
add_item(my_list, my_num)
print(my_list)
print(my_num)

Q4.

What does this program print? Trace through each iteration.

total = 0
for i in range(1, 6):
    for j in range(i):
        total += 1
print(total)

Q5.

What does this program print?

a = 5
b = 2
print(a // b)
print(a % b)
print(a / b)

Section 2: Short Written Answer

Answer in your own words. Complete sentences are encouraged.


Q6.

Consider the two functions below. Both are called with x = [1, 2, 3]. After calling each function, does the original list change? Explain why or why not for each case.

def func_a(lst):
    lst.append(4)

def func_b(lst):
    lst = [10, 20, 30]

Q7.

In your own words, what is a class? What is the difference between a class and an instance of a class? Give a real-world analogy to support your answer.


Q8.

What does the self parameter represent in a Python method? Why does every instance method need it as its first parameter?


Section 3: Write a Function

Write working Python code. You may use any built-in data types, but the restrictions noted in each question apply.


Q9.

Write a function second_largest(nums) that takes a list of integers and returns the second largest value. Do not use Python's built-in sorted() or max(). Assume the list has at least two distinct values.

def second_largest(nums):
    # your code here

Q10.

Write a function count_words(sentence) that takes a string and returns a dictionary mapping each word to the number of times it appears. Treat the input as all lowercase with no punctuation.

count_words("the cat sat on the mat")
# → {"the": 2, "cat": 1, "sat": 1, "on": 1, "mat": 1}
def count_words(sentence):
    # your code here

Section 4: Debugging

Each function below contains a bug. Identify the bug and write a corrected version.


Q11.

The function below is supposed to return the sum of all numbers from 1 to n (inclusive), but it has a bug. Identify the bug and write the corrected version.

def sum_to_n(n):
    total = 0
    i = 1
    while i < n:
        total += i
        i += 1
    return total

Describe the bug:

Corrected code:

def sum_to_n(n):
    # your fix here

Q12.

The function below is supposed to remove all negative numbers from a list and return the result. It has a bug. Identify it and write the corrected version.

def remove_negatives(nums):
    for n in nums:
        if n < 0:
            nums.remove(n)
    return nums

Describe the bug:

Corrected code:

def remove_negatives(nums):
    # your fix here