










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; Professor: Earls; Class: Data Structures; Subject: Computer Science; University: University of Illinois - Urbana-Champaign; Term: Spring 2009;
Typology: Exams
1 / 18
This page cannot be seen from the preview
Don't miss anything!











7p-9p, Tuesday, Feb 24
Consider the following statements:
int *p; int i; int k; i = 37; k = i; p = &i;
After these statements, which of the following will change the value of i to 75?
(a) k = 75; (b) *k = 75; (c) p = 75; (d) *p = 75; (e) Two or more of the answers will change i to 75.
Consider the following statements:
int i = 1; int k = 2; int * p1; int * p2; p1 = &i; p2 = &k; p1 = p2; *p1 = 3; *p2 = 4; cout << i << endl;
Which of the following is printed by the output statement (assume cout works)?
(a) 1 (b) 2 (c) 3 (d) 4 (e) None of these. The code does not compile.
Find the errors in the following code. Select all that apply, if any.
#include
int * myFun(const int & a) {
int * c = new int(a); int b = *c; return &b;
}
int main() {
int n = 8;
cout << *(myFun(n)) << endl;
return 0; }
(a) type mismatch (b) memory leak (c) invalid return value (d) NULL pointer dereference (e) violation of const
Which of the following functions are typically implemented in an iterator class? Check all that apply, if any.
(a) operator* (b) operator() (c) operator++ (d) operator= note: an iterator class MIGHT implement this, but I have never seen the need for it. Iterators for most container classes represent indices (integers) or pointers (addresses). (e) begin() note: begin() is a List class member function that returns an iterator.
class picRosters { private: BMP ** courses; int * enrollments; int numCourses;
// some helper functions
public: // constructors and destructor
// operator= declaration
// lots of public member functions };
The courses structure is a dynamically allocated array of dynamically allocated arrays of BMPs. The enrollments structure is a dynamically allocated array of ints, whose values are the lengths of the elements of the courses structure. In other words, for any i, courses[i] is an array of length enrollments[i].
Both courses and enrollments arrays have numCourses elements. You can assume that numCourses is greater than zero, and that every element of enrollments is greater than 0.
In this question you will help us implement the overloaded assignment operator for the picRosters class.
You may assume that all pointers are valid. That is, they are either NULL or they point to an object of the specified type. Furthermore, you may assume that the BMP class has an appropriately defined “Big Three” (destructor, copy constructor, and assignment operator).
You will write your answers on the following pages. To grade the coding portions of 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. Comments will be worth up to 1/3 of the total points for any part of the problem. Adding comments to our code skeletons can get you partial credit, but it’s not required. problem 2 continued...
(a) (10 points) We have written a partial implementation for a helper member function called copy that makes the current object have the same value as the parameter orig.
-1 pt for incorrect delete or other code
(c) (5 points) We have tried to write the overloaded assignment operator for the picRosters class using the helper functions you wrote in parts (a) and (b). Circle the errors in our function, and write the correct code on the right, very clearly.
const picRoster & picRosters::operator=(const picRosters & rhs) {
if( this != &rhs) { clear(); copy(rhs);
}
return *this; }
Rubric: 1 pt recognizing missing return parameter picRosters const & 0.5 pt minor error in above 1 pt recognizing missing const in input param 1 pt correctly changing *this!=rhs to this!=&rhs 0.5 minor error in above 1 pt correctly switching order of copy and clear 1 pt correctly returning *this
Solution:
(a) (8 points) We have seen 4 different uses for the symbol “:”. For each, give a short example and explain using appropriate C++ vocabulary. i. (::) Accessing a global variable in a class or name-space. std::cout
Defining a function - scope resolution operator <return-value>
class A : public B { } iii. (:) determining the access level
class A { public: // ... private: //... } iv. (:) initializing a variable by a parameter - initializer list void setVar(int param) : var(param) { } Rubric: 1 point for example and 1 point for the explanation. (b) (4 points) We have seen two different uses of the keyword const. For each, give a short (one line) code example and briefly explain the behavior. Solution: One point for example, one point for explanation. i. example 1: const var_type variable This would create a variable of type var type, whose value can not be changed. ii. example 2: Const functions. Call to const functions won’t change the object. <return-value>
In each part of this problem we will be presenting you with a pair of classes and some code snippets and asking you about the result of compiling and running the code. Your task will be to tell us explicitly what happens, and why. If there is an error, you should reason about whether the error occurs at compile time, or run time. If there is output, you should tell us what the output is. If the result depends on some other factors, explain them. Assume that the standard iostream is available (cout and cin work).
(a) (3 points) Class definitions:
class Rect{ class Sq: public Rect { public: public: Rect(int w,int h):wid(w),ht(h){} Sq(int side):Rect(side,side){}
private: int area(){return wid * wid;} int wid, ht; }; };
Code snippet:
Sq d(5); cout << d.area << endl;
Result: Solution: Compiler error: class Sq cannot access Rect’s private members. ( point for an answer of 25) (b) (3 points) Class definitions:
class vehicle{ class bus: public vehicle { public: public: void showCol(){cout << "gold";} void showCol(){cout << "silver";}
}; };
Code snippet:
vehicle * v; int x;
// initialize x
if (x%2 == 0) // x is even v = new vehicle; else
v = new bus;
v->showCol(); delete v;
Result: Solution: Always prints gold. (1 point for conditional response of gold/silver)
(c) (2 points)
Class definitions:
class artifact{ class basket: public artifact {
public: public: virtual void showMed() virtual void showMed() {cout << "gold";} {cout << "clay";}
}; };
Code snippet:
artifact * a; int x;
// initialize x
if (x%2 == 0) // x is even a = new artifact; else a = new basket;
a->showMed(); delete a;
Result: Solution: Prints gold if x is even, silver otherwise. (1 point for assuming x is even)
Suppose you have implemented a List using a singly linked list with sentinels at the head and tail, and a tail pointer as in the figure below. In this problem you will implement some of the member functions for this list class.
Here is a partial List class definition:
template
private: listNode * head; listNode * tail; int size; // the number of data elements in the list
listNode * Find(int k, listNode * curr); void removeAll(listNode * curr);
struct listNode { LIT data; listNode * next; listNode(LIT e): data(e), next(NULL) {} ; listNode(): next(NULL) {} ; }; };
The Find private helper function returns a pointer to the listNode that is k nodes beyond the input listNode *. For example, Find( 2, head) returns a pointer to the node containing the value 8 in the example above. You may use this helper function anywhere you’d like.
(a) (5 points) Write the default (no argument) constructor for the List class. To make this easy for us to grade, place the function prototype on the first line, and then write the rest of your code between the brackets. Our solution required 3 lines of code. (Note, the next pointer for the tail sentinel should be NULL.) template
Rubric: 1 pt - correct templating 1 pt - correct class declaration 1 pt - setting size to 0 1 pt - allocating head sentinel with the correct pointer 1 pt - allocating tail sentinel with the correct pointer
(b) (5 points) We’ve written part of the code for the member function insert below. Your task is to complete the function. insert puts a new listNode containing element e into the list at position loc. For example, in the example on the previous page, insert(3, 2) would place a listnode containing the value 2 into the list between the nodes containing 8 and 3. template
listNode * t = new listNode(e);
listNode * p = Find(loc-1, head);
t->next = p->next; p->next = t size ++; }
Rubric: 1 pt - declaring t a listNode 1 pt - using loc-1 and head (or loc and head if used properly) 1 pt - incrementing the size variable 1 pt - properly setting pointers 2 pts - properly inserting the node at the correct position -1 for each major mistake i.e. deleting nodes (calling delete on pointers)
(c) (5 points) What is the asymptotic, worst case, running time for the best possible imple- mentation of the member function removeLast that removes the last data element from
if (curr != NULL) {
removeAll(curr->next);
delete curr; }
}
Rubric: 2 points for “curr != NULL”, 1 point for recursive call, 2 points for “delete curr”.
ii. (5 points) Now write the destructor for the List class as it would appear in List.cpp. Your solution should call removeAll appropriately. template