MTH 4300 Midterm 1 Practice
This practice exam has 5 tracing questions, 5 debugging questions, and 5 programming questions.
Give each question an honest shot. Use an AI tool to check your answers afterwards.
Tracing
For each question, write the exact output. Include spaces and line breaks.
T1. Types and operators
T2. Conditionals and loops
T3. Values, references, and pointers
Track a and b after each call.
T4. Arrays and dynamic allocation
T5. Vectors, algorithms, and recursion
Show the calls and return values for sumFirst(v, 3).
Debugging
Each program or function has two independent bugs. For each bug, identify the faulty expression or statement, explain the failure, and give a correction. Keep the stated behavior and function signature.
D1. Average of positive inputs
meanPositive should return the mean of the positive values in data. Return 0.0 if there are none. The input in main should produce 2.5.
D2. Pointer parameters
addToBoth should add step to each non-null target. The two arguments may point to the same integer. The call shown should leave x equal to 11.
D3. Dynamic arrays
makeSquares(n) should return a new array containing 0², 1², ..., (n-1)². The caller owns the array. Assume n > 0.
D4. Vector search
printFirstAbove should print the first value strictly greater than limit, or none if no such value exists. The sample should print 9.
D5. Recursive search
contains should search the half-open interval [lo, hi) and work for an empty vector. Both sample calls should print 1.
Programming
Write each function from its signature. You may add required standard headers. State any loop invariant or recursive base case you rely on. Follow the limits in each question.
P1. Sum of multiples
Return the sum of all integers in the inclusive interval [first, last] that are divisible by divisor. Assume divisor > 0. If first > last, return 0. Use a loop. Examples: sumMultiples(1, 10, 3) == 18 and sumMultiples(5, 4, 2) == 0.
P2. Update through a pointer
If value is non-null and *value < limit, add amount to *value and return true. Otherwise leave everything unchanged and return false. amount may be negative. Do not allocate memory. Examples: starting at 4, increaseIfBelow(&x, 5, 3) changes x to 7 and returns true; calling it with nullptr returns false.
P3. Reverse into a new array
Assume n >= 0 and source points to at least n integers when n > 0. Return a dynamically allocated array of length n with the elements in reverse order. The caller frees the result with delete[]. Do not modify source and do not use std::vector. For n == 0, returning an array allocated with new int[0] is allowed. Example: {2, 5, 8} becomes {8, 5, 2}.
P4. Count values in a range
Return the number of values in the inclusive interval [low, high]. Use std::count_if and a lambda that captures the bounds. If low > high, return 0. Example: countInRange({1, 3, 3, 7}, 2, 3) == 2.
P5. Recursive count
Return the number of occurrences of target from index through the end of the vector. Assume 0 <= index <= values.size(). Use recursion with no loops or algorithms. Examples: countMatches({2, 1, 2, 2}, 2, 0) == 3 and countMatches({2, 1, 2, 2}, 2, 2) == 2.