CSCI 330 Final Exam

Fall 2021

  1. [5 points] Provide the code for a templated function that is passed three parameters of the same type, returns the median or "middle" value. For example, if the input is 'b', 'a', 'c', the function would return 'b'. If the input is "cow", "zebra", "dog", the function would return "dog". You may assume that the comparison operators are available on the data type passed to your function.

  2. [5 points] You wish to store a reference to another object in a class. What must you do to accomplish this? Provide example code.

  3. Virtual Functions
    1. [5 points] Describe in detail the difference between virtual and non-virtual functions when encountered by the compiler. (The keyword virtual is NOT an answer to this question).
    2. [5 points] Describe a situation when the answer to part a matters. Give a code example.
    3. [5 points] Why should a destructor be declared virtual in a class that is part of a hierarchy? Given an example where a problem will arise if this is guidance is not followed.

  4. [5 points] The smart_ptr class eliminates the possibility of a double free error. Explain how.

  5. Exceptions
    1. [3 points] Describe the exception handling mechanism in C++.
    2. [4 points] Give code examples for the components involved in exception handling.
    3. [4 points] Describe what happens when an exception is thrown. Include a description of what happens to functions that were called and classes that were allocated.

  6. Recursion
    1. [3 points] What three conditions must be met to successfully write a recursive function.
    2. [2 points] Name two potential disadvantages of recursive functions.
    3. [5 points] Write a recursive function to print the elements of a singly linked list in reverse order.