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. You have 45 minutes.


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? Trace through each iteration.

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

Q3.

What does this program print?

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

Q4.

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)

Section 2: Short Written Answer

Answer in your own words. Complete sentences are encouraged.


Q5.

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. Also: what does the self parameter represent in a Python method?


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.


Q6.

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

Section 4: Debugging

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


Q7.

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