You are on page 1of 6

EXAM QUESTIONS FOR DATA STRUCTURES FROM CS COLORADO WEBSITE: CHAPTER 1 Short Answers: Multiple Choice: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.

12. 13. 14. 15. 16. 17. 18. 19. B B D TRUE FALSE B B A A D B FALSE FALSE B B E C A E

CHAPTER 2 Short Answers


1. The following: throttle quiz; quiz.shut_off(); cout << quiz.flow() << endl; 2. C, C, -, -, X, X 3. An automatic default constructor is a compiler provided default constructor that is called whenever an object is defined for the first time. This constructor doesnt do much so it is recommended to always implement a constructor yourself that will initialize the data members. 4. An inline member function is a member function that is implemented right after its definition in the header file in the public section. An example is the following: Public: Bool is_on() { return (position > 0);} ..

5. A macro guard in a header file is the #ifndef, #def on the top of the header file, and the #endif on the bottom. It serves to ensure the class definition is only read once and not multiple times thus preventing a possible error from occurring. i.e.: #ifndef point.h #def point.h namespace main_savitch_2A { class throttl { }; .. } #endif 6. Easy 7. A const reference parameter is used when the programmer wants the actual argument to change, instead of having a function/program create a temporary variable which is released later on. i.e.: void change_the_parameter(size_t& size) { size = 5; } 8. Easy 9. Easy 10. Easy 11. Should be easy 12. Foo operator+(const foo& x, const foo& y); 13. Define the function as a friend function 14. When it is necessary for that function to have access to the private data of that class.

Multiple Choice 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. D D** D** C B C A D C E D*

CHAPTER 3

Short Answers: 1. The value_type is defined in the public section of the class definition in the header file. It is better to have such a definition rather than just using an int because if there needed to be a change from int to double throughout the whole implementation, then only the value_type declaration would have to modified and nothing else. 2. Other than the arrays capacity, it would be necessary to keep track of the current index, or the index at which there is an item available to be used. This allows the programmer to keep track of whether or not there is an item in the array at all, or whether it is just empty. And, if there is an item in the array, where it is. 3. In this context, the keyword static defines the CAPACITY as a variable that cannot be changed throughout the class. The keyword static also only needs to be defined and stored once, the same variable CAPACITY is usable for any object that is declared. If static was not used, then along which each object that would be defined, there would also be a copy of CAPACITY defined. Thus, static serves to save memory and guarantee no change in the variable CAPACITY throughout the class. 4. Its not working as intended because, at the end of the the loop when i = 0, it increments down again and becomes a I = -1, however, this is impossible because size_t defiend variables may only be non-negative integers. Therefore, this piece of code will not work. 5. The piece of code is as follows: for(i = 99; i > 50; i--) data[i] = data[i - 1]; data[50] = 42; 6. The piece of code is as follows:

for(i = 50; i < 99; i ++) data[i] = data[i + 1]; Multiple Choice: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. B A A C C B E D E B

11. 12. 13. 14. 15.

D D D A D

CHAPTER 4 Short Answers: 1. The piece of code is as follows: int *p; p = new int[100]; for(size_t i = 0; i < 100; i++) p[i] = i; delete [] p; 2. If new is called but the heap is out of memory, then a bad alloc exception is thrown which prints out an error message and halts the program. 3. Draw picture 4. Draw picture 5. Draw picture 6. What we know about the parameter is the following: The parameter is an array and each item in the array is defined as a double data type, which means it could be an integer or a number with a decimal. Also, data is an automatic pointer to the first index of the array. 7. function foo (const int * p); The restriction that the const keyword
provides within the implementation of foo is that whatever the pointer points to may NOT be modified in any way.

8. The automatic assignment operator that is provided if there is no user defined assignment operator functions by merely copying all member variables from the object on the right side of the equal sign and initializing the object on the left side of the equal sign with exact copies of these member variables. Therefore, it is just a copying technique. However, if we use this automatic assignment operator with the bag class that stores the items in a dynamic array, then it will merely initialize a pointer then data will just be a pointer to the first index to the object it is being defined as a copy of. All in all, there would still be just one array that now both objectss

member variable data will be pointing to, and this is usually not what is wanted. What is usually wanted is for tjhere to be created an exact replica of the pre-defined object that is on theright hand side of the equal sign when this function is called. 9. If the bag is found to be full, it is resized with the statement: reserve(used + 1). A problem with this approach is that if insert is called when the bag is full, it is resized with previously defined statement, however, if it is called repeatedly after this, it must be resized each time again because its size only increases by 1 each time the insert function is called. A better solution would be to increase the size not by 1 but by a larger number. Thus, we can substitute the statement reserve(used + 1) by the statement reserve(used + 10) instead, and save the program from having to inefficiently outsource more memory every single time insert is repeatedly called. 10. The code is as follows: void bag::triple_capacity() { // declare a new array // initialzie this new array with a capacity three times the capacity of data // copy all the items from data into the new array // delete the old data arry // re-initialize the member variables value_type *triple_array; new_capacity = 3*capacity; triple_array = new value_typ[new_capacity]; copy(data, data + used, triple_array); delete [] data; data = triple_array; capacity = new_capacity; } 11. Multiple Choice: 1. D

2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22.

D A D C C C* A B A A A A D B B C A D D B

You might also like