










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Exam; Class: Data Structures; Subject: Computer Science; University: University of Illinois - Urbana-Champaign; Term: Fall 2007;
Typology: Exams
1 / 18
This page cannot be seen from the preview
Don't miss anything!











7p-9p, Thursday, October 4
Consider the following C++ statements:
#include
int main(){ int y; int x = 36; //code to be inserted here (y)++; cout << x << endl; return 0; }
Which of the following choices, if inserted at the line marked, will result in 37 being sent to standard ouput? Answer: b
(a) y = x; (b) y = &x; (c) *y = x; (d) *y = &x; (e) None of these will give the result we want.
We need to write our own destructor (instead of using the default one) when...Answer:c
(a) we have private members that are pointers. (b) we have private members that are arrays. (c) we have allocated dynamic memory in our constructors. (d) two of these three require that we write our own destructor. (e) all of the above require that we write our own destructor.
Consider the following C++ statements:
class Ball { public: //constructors and other member functions ... void setSameRadius(Ball & orig) const; private: int radius; }
void Ball::setSameRadius(Ball & orig) const { radius = orig.radius; }
Consider the Ball class and the setSameRadius function shown above. This function is expected to set the radius of this Ball object to the same value as the radius of the orig Ball object.
This function: Answer: d
(a) compiles and runs without error but does not perform the desired task. (b) compiles and runs without error and performs the desired task. (c) encounters a run-time error. (d) does not compile because of an error involving setSameRadius. (e) compiles and runs without error, performs the desired task, but changes the value of orig.
Suppose we have implemented a queue as a singly linked list with a tail pointer modeled here:
Which of the following best describes the running time of the enqueue and dequeue operations if the rear of the queue must be at the head of the linked memory structure? (n is the number of elements in the queue.) Answer: c
(a) O(1) for both enqueue and dequeue (b) O(n) for both enqueue and dequeue (c) O(1) for enqueue and O(n) for dequeue (d) O(n) for enqueue and O(1) for dequeue (e) None of these is the correct choice.
Suppose myVar is declared as follows: int ** myVar;. Which of the following could describe the variable myVar once it has been initialized?Answer: e
(a) myVar is a dynamic array of integer pointers. (b) myVar is a pointer to a dynamic array of integers. (c) myVar is a dynamic array of dynamic arrays of integers. (d) Two of these are valid descriptions (and one is not). (e) All three are valid descriptions.
Consider the following partial class definitions:
class Sphere { private: double theRadius; public: // Lots of member functions go here. double getArea() const; // computes surface area virtual void displayArea() const; // displayArea() includes the statement "cout << getArea() << endl;" };
class Ball: public Sphere { private: string theName; public: // Lots of member functions. Note: Ball class inherits displayArea(). double getArea() const; // computes cross sectional area };
Now suppose you have the following in your main() function:
Sphere mySphere; Ball myBall; myBall.displayArea();
Which of the following describes the behavior of this code in main()? Answer: a
(a) myBall’s surface area is displayed. (b) myBall’s cross sectional area is displayed. (c) The call to getArea() within displayArea() is ambiguous, so there is a compile error. (d) A bus error occurs at run time. (e) None of these options describes the behavior of this code.
problem 2 continued...
Solution:
Slideshow::Slideshow(const Slideshow & origval) {
slidecount = origval.slidecount; //define slidecount slides = new Image [slidecount]; // allocate space for slides durations = new int[slidecount]; // allocate space for durations for (int j = 0; j < slidecount; j++) { // loop to fill arrays durations[j] = origval.durations[j]; if (origval.slides[j] != NULL) slides[j] = new Image((origval.slides[j])); //copy images else slides[j] = NULL; } }
Grading scheme:
Consider a BackPack class object and two iterators which we have declared in main() using the following statements:
BackPack
Your task is to use the iterators and a single additional integer variable sum3 to compute the sum of the middle three integers in the BackPack. That is, if the BackPack had values:
< 3 2 8 4 5 9 1 6 0 2 4 9 1 >
your code would compute 9 + 1 + 6 = 16. You may assume that the BackPack class has member functions begin() and end(), each of which returns an iterator to the appropriate element of the BackPack. That is, begin() returns an iterator indicating the first element of the BackPack and end() returns an iterator indicating the element past the last element of the BackPack. You may also assume that the operators *, ++, --, ==, and != are overloaded for BackPack iterators. You may ONLY use variables it1, it2, and sum3 in your code. Do not declare any additional variables, and do not assume that the BackPack class provides any other public member functions. You may write your answer on the following page. To grade this problem, we will first read your comments to make sure you intend to do the right thing, and then we’ll check your code to make sure it does what your comments say it should. As a result, be sure your comments are coherent, useful, and reflective of your approach to the problem. You may use the template on the following page for your response, or you may write free-form on the back of this page or the next.
template
(a) (10 points) This class is a simple singly-linked list class. You are to write an iterative reverse() function that reverses a list by rearranging next pointers and changing the head pointer if necessary. You should not need to call any outside functions to write the code for reverse(). To illustrate the action reverse will perform, the following is a LinkedList
The following is that same LinkedList
You may write your answer on the following page. To grade this problem, we will first read your comments to make sure you intend to do the right thing, and then we’ll check your code to make sure it does what your comments say it should. As a result, be sure your comments are coherent, useful, and reflective of your approach to the problem. You may use the template on the following page for your response, or you may write free-form on the back of this page or the next.
Solution
void LinkedList::reverse() { listNode * cur = head; // Traverse list with 2 pointers pointomg listNode * prev = NULL; // to the current and previous list nodes
while (cur !=NULL) // A node still needs it’s pointer reversed { listNode * temp = cur->next; // Save a pointer to the next node
cur->next = prev; // Reverse the current node’s ‘‘next’’ // to point to the previous node
prev = cur; // Move the ‘‘prev’’ pointer to // point to the current node
cur = temp; // Move ‘‘cur’’ to the next node // in the list } // End while loop head = prev; // ‘‘prev’’ points to the end of the list } // Complete the reversal by setting ‘‘head’’ // to point to the end of the list
Grading scheme:
(b) (5 points) Analyze the running time of the code you wrote in part (a). You should state the worst-case time complexity of your code when operating on a list with n nodes and briefly explain why it would take that much time.
The running time of reverse() on an n node list would be O(n). The code traverses the length of the list a single time thereby visiting n nodes and does O( 1 ) work on every listNode it visits.
Grading scheme:
Imagine that you are given a standard Stack class and Queue class, both of which are designed to contain integer data (interface provided on following pages). Your task is to write a function called Rev0 that takes one argument: a reference to a Queue. The function should reverse the order of any numbers appearing between a pair of consecutive 0 ’s, where the pairs can’t overlap (so we reverse the elements between the first 0 and the second one, leave alone the elements between the second and third, reverse the ones between the third and fourth, and so on). If there are an odd number of 0 ’s in the given queue, the elements following the last 0 should also be reversed. For example, given the following queue,
front rear 1 0 5 3 0 3 1 0 2 6 5
we get the following arrangement:
front rear 1 0 3 5 0 3 1 0 5 6 2
We are putting some constraints on your implementation. Specifically, you must write the function using only the variable declarations we provide. We are allowing you four local variables: two ints, one Stack, and one Queue. You may write your answer on the following page. To grade this problem, we will first read your comments to make sure you intend to do the right thing, and then we’ll check your code to make sure it does what your comments say it should. As a result, be sure your comments are coherent, useful, and reflective of your approach to the problem. You may use the template on the following page for your response, or you may write free-form on the back of this page or the next.
Solution:
void Rev0(Queue & queue) { int temp1, temp2; // These names are generic to avoid Stack s; // giving you too many clues about Queue q; // how we think you should use them.
temp2 = 0; // flag to indicate parity of # of 0s.
while (!queue.isEmpty()) { // process each queue value
temp1 = queue.dequeue(); // grab front of queue if ((temp1==0) && (temp2==0)) { // see a 0 and we weren’t reversing temp2 = 1; // start reversing next character q.enqueue(temp1); // put the 0 on queue } else if ((temp1 == 0) && (temp2 == 1)) { // see a 0 and we were reversing temp2 = 0; // flip reversal flag while (!s.isEmpty()) { // complete the reversal by q.enqueue(s.pop()); //putting stack values on queue } q.enqueue(temp1); // put the 0 on the queue } else if ((temp1 != 0) && (temp2 == 0)) // not reversing, just enqueue q.enqueue(temp); else if ((temp1 != 0) && (temp2== 1)) // reversing, push to reverse s.push(temp); } while (!s.isEmpty()) { // empty s into q if we ran out of elements q.enqueue(s.pop()); // in queue while pushing to s } queue = q; // relies on operator= in Queue class. }
Grading scheme:
(scratch paper, page 1)
(scratch paper, page 2)