CSCI 330 Final Exam
Fall 2021
- Please answer each question fully, carefully and thoughtfully.
- No credit will be given for unreadable answers, please write neatly.
- Answer the questions in any order, but please number each answer.
- Make sure that your name is on your test.
- [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.
- [5 points] You wish to store a reference to another object in a class. What must you do to accomplish this? Provide example code.
- Virtual Functions
- [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).
- [5 points] Describe a situation when the answer to part a matters. Give a code example.
- [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.
- [5 points] The
smart_ptr
class eliminates the possibility of a double free error. Explain how.
- Exceptions
- [3 points] Describe the exception handling mechanism in C++.
- [4 points] Give code examples for the components involved in exception handling.
- [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.
- Recursion
- [3 points] What three conditions must be met to successfully write a recursive function.
- [2 points] Name two potential disadvantages of recursive functions.
- [5 points] Write a recursive function to print the elements of a singly linked list in reverse order.