MTH 4300 Midterm 1 Practice Questions
Exams in MTH 4300 revolve around 3 classes of questions:
- Tracing - evaluating the output of a code snippet
- Debugging - given a code snippet, diagnose and fix the errors
- Implementation - write code from scratch to solve a problem
Clarity is the only way you'll get points. If you don't even understand what it is you're trying to say, then how can I?
Tracing
-
What is the output of the following program? Trace through each statement, tracking
x,y, andzafter every change — including what happens insidetransform. -
What is the output of the following program? For each recursive call to
compress, show the value oflo,hi,mid, and the return value. Also show the final state ofnums. -
What is the output of the following program? Trace through each statement, tracking the values of
a,b,c, andptr— especially whatptr->next = &cand the subsequent pointer operations do. -
What is the output of the following program? Trace through the sort, the
find_if, thecount_if, and theaccumulatecalls step by step.
Debugging
-
The program below is supposed to create an array filled with a given value, reverse it in-place, print it, and free the memory. It contains 4 bugs. For each one, state the line, explain the problem, and write the fix.
-
The program below is supposed to count how many words in a list are palindromes. It contains 3 bugs. For each one, state the line, explain the problem, and write the fix.
Implementation
-
Given the following function signature:
Returns the sum of all decimal digits in
n. Assumen >= 0. No loops allowed. -
Given the following function signature:
Both
aandbare already sorted ascending. Returns a new sorted vector containing all elements from both. Nostd::sortorstd::mergeallowed. -
Given the following function signature:
Returns
trueif any value appears more than once. No sorting. Nostd::set,std::unordered_set, or similar containers. O(n²) is acceptable.