You are on page 1of 6

EXAM QUESTIONS FOR DATA STRUCTURES FROM CS COLORADO WEBSITE:

CHAPTER 1
Short Answers:
Multiple Choice:
1. B
2. B
3. D
4. TRUE
5. FALSE
6. B
7. B
8. A
9. A
10.D
11.B
12.FALSE
13.FALSE
14.B
15.B
16.E
17.C
18.A
19.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.:

8.
9.
10.
11.
12.
13.
14.

void change_the_parameter(size_t& size)


{
size = 5;
}
Easy
Easy
Easy
Should be easy
Foo operator+(const foo& x, const foo& y);
Define the function as a friend function
When it is necessary for that function to have access to the private data of that class.

Multiple Choice
1. D
2. D**
3. D**
4. C
5. B
6. C
7. A
8. D
9. C
10.E
11.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. B
2. A
3. A
4. C
5. C
6. B
7. E
8. D
9. E
10.B
11.D
12.D
13.D
14.A
15.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.
2.
3.
4.
5.
6.
7.
8.

D
D
A
D
C
C
C*
A

9. B
10.A
11.A
12.A
13.A
14.D
15.B
16.B
17.C
18.A
19.D
20.D
21.B
22.

You might also like