MTH 4300 Midterm 2 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
-
Consider the following program.
Boxperforms a deep copy of itsintvalue and prints a message from each special member function.Write the exact output produced by this program, in order. Pay close attention to when copy construction vs. copy assignment is called, and to the order in which local objects are destroyed.
-
Consider the following program.
Shape,Circle, andSquareeach print from their constructor and destructor.name()is virtual inShapebutSquaredoes not override it.Write the exact output produced by this program, in order. Be careful about constructor and destructor chaining, and about which
name()is dispatched for each object.
Debugging
-
The following class is intended to be a minimal string wrapper backed by a heap-allocated
charbuffer. It contains 3 bugs. For each one:- Name the member function that contains the bug.
- Explain in one or two sentences what goes wrong.
- Write the corrected line or lines of code.
-
The following function is intended to return the
kmost frequently occurring words in a list, sorted from most to least frequent. It contains 3 bugs. For each one:- Identify the line or lines where the bug occurs.
- Explain in one or two sentences why the result is incorrect.
- Write the corrected line or lines of code.
Implementation
-
Given the following node definition:
Implement the following function:
Reverses the singly linked list in-place and returns the new head. Do not allocate any new nodes.
-
Implement the following function template:
Returns the second largest distinct value in
v. You may assumevcontains at least two distinct elements. Nostd::sortallowed. -
Given the following function signature:
Returns the index of the first character that appears exactly once, or
-1if no such character exists. Use anstd::unordered_map. O(n) expected. -
Given the following function signature:
Returns
trueif the bracket sequence insis valid (every opener is closed in the correct order). Only'(',')','[',']','{','}'appear ins. Use a stack.