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.
Q2.
What does this program print?
Q3.
What does this program print? Pay close attention to what is modified and where.
Q4.
What does this program print? Trace through each iteration.
Q5.
What does this program print?
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.
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.
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.
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.
Describe the bug:
Corrected code:
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.
Describe the bug:
Corrected code: