DOC PREVIEW
UAF CS 311 - Midterm Exam Solutions - CS 311

This preview shows page 1-2 out of 6 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Exam page 2 CS 311 Data Structures and Algorithms, Spring 2009 Midterm Exam Solutions The Midterm Exam was given in class on Wednesday, March 18, 2009. 1. [4 pts] Parameter Passing in C++. In the table below, the names of the three ways to pass parameters in C++ are written in the first row. In the rest of the table, circle “YES” or “NO” to answer the questions for each parameter-passing method. Does this method … BY VALUE BY REFERENCE BY REFERENCE-TO-CONST … make a copy of the object passed? YES / NO YES / NO YES / NO … allow const objects to be passed? YES / NO YES / NO YES / NO … allow for polymorphism/virtual dispatch? YES / NO YES / NO YES / NO … allow implicit type conversions to be performed? YES / NO YES / NO YES / NO 2. [6 pts total] Properties of Algorithms. 2a. [2 pts] What does it mean for an algorithm to be “scalable”? An algorithm is scalable if it works well with large (and ever larger) problems. 2b. [2 pts] What does it mean for an algorithm to be “in-place”? An algorithm is in-place if its space usage, beyond that required for its input, is at most some fixed constant. More concisely, an algorithm is in-place if its additional space usage is O(1). 2c. [2 pts] What does it mean for an algorithm to be “stable”? An algorithm that reorders a list is stable if it never changes the relative order of equivalent items, that is, if it never changes the relative order of items unnecessarily.Exam page 3 3. [6 pts total] Recursion vs. Iteration. 3a. [4 pts] Recursion has a number of disadvantages, as contrasted with iteration. Give at least two of these. Here are three. • Function-call overhead. • Memory management with poor error-recovery ability. • The inherent, and often non-obvious, inefficiency of many (but not all!) recursive algorithms. 3b. [2 pts] Under what circumstances is it very easy to convert recursive code to iterative form? When the code is tail recursive, that is, when the recursive call is the last operation that a function performs. 4. [6 pts total] Linked Lists vs. Arrays. 4a. [3 pts] Give one advantage of a Linked List, as compared to an array. Here are two. • A Linked List has much faster insert and remove (by iterator, or at the beginning) operations than an array. These are generally O(1) for a Linked List, but O(n) for an array. • A Linked List allows for more flexible memory management than an array. It does not require large contiguous memory blocks, and needs no preallocation. 4b. [3 pts] Give one disadvantage of a Linked List, as compared to an array. Here are two. • A Linked List has a much slower look-up by index operation than an array. This operation is O(n) for a Linked List, but O(1) for an array. • A Linked List uses more memory than an array.Exam page 4 5. [6 pts total] The Big Three. 5a. [3 pts] What does the C++ “Law of the Big Three” state? The Law of the Big Three states that, if you declare one of the Big Three (copy constructor, copy assignment operator, destructor) in a class, then you generally should declare all of them. 5b. [3 pts] In what circumstances do we usually need to write “the Big Three”? When we need to write the Big Three for a class, it is generally because an object of the class owns some resource. 6. [6 pts total] Order of Functions. Write the order of each function, using big-O. 6a. [3 pts] void func_a(int arr[], int n) // n is size of arr { cout << "Item 0 = " << arr[0] << endl; cout << "Item 1 = " << arr[1] << endl; cout << "Item 2 = " << arr[2] << endl; } Order (big-O): O(1). 6b. [3 pts] void func_b(int arr[], int n) // n is size of arr { for (int a = 0; a < n; ++a) for (int b = 0; b < a; ++b) for (int c = 0; c < b; ++c) arr[c] += arr[b]; } Order (big-O): O(n3).Exam page 5 7. [7 pts total] Resource Ownership. 7a. [3 pts] In the context of this class, what does it mean to “own” a resource? To own a resource is to be responsible for releasing it. 7b. [4 pts] Give two examples of resources that might be owned, and what it means to release each one. Here are three. • Dynamically allocated memory: freeing the memory. • An open file: flushing and closing the file. • Various synchronization primitives (mutexes, etc.): freeing and returning to the system for reuse. 8. [10 pts total] Order of Operations. In each part, indicate the order of the given operation, using big-O and, based on this, in words. Assume that a good algorithm is used, within the constraints given. Use n to denote the size of the input. 8a. [2 pts] Find an item with a given value in a sorted array. Big-O: O(log n). In Words: Logarithmic time. [Use Binary Search.] 8b. [2 pts] Print the middle item in an array of known size. Big-O: O(1). In Words: Constant time. 8c. [2 pts] Print the middle item in a Linked List of known size. Big-O: O(n). In Words: Linear time. 8d. [2 pts] Sort an array with Quicksort. Big-O: O(n2). In Words: Quadratic time. [Remember: Worst case.] 8e. [2 pts] Sort a Linked List. Big-O: O(n log n). In Words: Log-linear time. [Use Merge Sort.]Exam page 6 9. [6 pts] The Introsort algorithm is, for many purposes, the fastest sort known. However, in certain situations, some other algorithm is faster. List two such situations. For each, say which sorting algorithm would be faster in that situation. Here are three. • When sorting nearly sorted data. Use Insertion Sort. • When a stable sort is required. Use Merge Sort. • When sorting a Linked List. Use Merge Sort. 10. [6 pts] One strategy for dealing with a possible error condition in a function is to signal the client code that an error has occurred. What are two other strategies? Hint: These must not involve signaling the client code. • Prevention: Requiring client code to ensure that an error condition does not occur. • Containment: Handling the error condition entirely inside the function.Exam page 7 11. [12 pts total] Impossible? Your friend Egbert is known for making wild claims. In each part, Egbert claims he has a new algorithm. Indicate whether Egbert claim is possible or impossible (circle one). If it is possible, explain how to do it. If impossible, explain why. 9a. [4 pts] Egbert says, “I have a new algorithm that can sort a list in linear time, assuming that each item’s position is no


View Full Document
Download Midterm Exam Solutions - CS 311
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Midterm Exam Solutions - CS 311 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Midterm Exam Solutions - CS 311 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?