EECS 281 MIDTERM QUESTIONS WITH CORRECT
ANSWERS
What is memory ownership for a container? - ans-
xz xz xz xz xz xz xz xzxz
When a container owns a value, only that container can modify the value. A drawback of thi
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
s is it takes a long time to copy containers like this. When a container has pointers, either the
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
container or other objects pointing to the same thing can modify, which can be unsafe. This
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
can be used for shared data. When a container owns a reference to a value, it doesn't own t
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
he value. You cannot delete by reference. The value must be initialized but cannot be assig
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
ned to. xz xz
student answer: xz xz
Memory ownership of a container refers to how controlled the interface is to the data held wi
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
thin the container. There are different degrees of ownership ranging from no control (as in t
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
he case of references) to a lot of control (which is the default from within the container). A co
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
ntainer class can control how much access objects outside the class can have over the ele
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
ments of the container (hence the class has a public interface for other objects).
xz xz xz xz xz xz xz xz xz xz xz xz xz xz
-
A container composed of values can completely restrict or give complete access to the ele
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
ments (the former being impractical and the latter being close to what vector does) but in eit
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
her situation the container class mutates the data.
xz xz xz xz xz xz xz
-
A container composed of pointers however is a different story. Because they contain pointe
xz xz xz xz xz xz xz xz xz xz xz xz xz xz
rs, the container only protects the location data (the pointer data) but it cannot protect or con
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
trol the interface to the data stores at the addresses. Thus, the container does not have total
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
control over the relevant data. Hypothetically, an object outside the container can make edi
xz xz xz xz xz xz xz xz xz xz xz xz xz xz
ts to the data.xz xz xz
What are some disadvantages of arrays? - ans-
xz xz xz xz xz xz xzxz
when many insertions are needed, we need to move everything over EVERY time we insert
xz xz xz xz xz xz xz xz xz xz xz xz xz xz x
a number.
z xz xz
One disadvantage of an array is that there are no bound checks so you increase the change
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
of illegally accessing memory (causing a seg fault) or you need O(1) complexity overhead li
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
ke a size variable to maintain bound checks.
xz xz xz xz xz xz xz
Why do you need a const and non-const version of some operators?
xz xz xz xz xz xz xz xz xz xz xz
What should non=const op[ ] return ? - ans-xz xz xz xz xz xz xz xzxz
Read and write. In some cases, the compiler knows it can speed some things up if it knows i
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
t never has to make any changes. Non const operator returns the object, which can be modi
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
fied. xz
, You would need both const and non-
xz xz xz xz xz xz
const version of operations because you would want to be as safe as possible. One good ru
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
le for C++ programming from the book "Effective C+
xz xz xz xz xz xz xz xz
+" highlights that you should make every variable you don't manipulate a const variable to p
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
rotect any data you can. Having methods return const makes your operation more resilient t
xz xz xz xz xz xz xz xz xz xz xz xz xz xz
o potential bugs (like accidently mutating data in a container when you didn't want to).
xz xz xz xz xz xz xz xz xz xz xz xz xz xz
how many destructor calls (min, max) can be invoked by
xz xz xz xz xz xz xz xz xz xz
operator delete xz xz
operator delete[ ] - ans- xz xz xz xzxz
A destructor should have only one call to delete for each new allocation to the heap and one
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
call to delete[] for each new[] allocation to the heap.
xz xz xz xz xz xz xz xz xz
Why would you use a pointer-based copying algorithm? - ans-Pointer-
xz xz xz xz xz xz xz xz xzxz
based algorithm works with both random access and sequential access (ex: will work with b
xz xz xz xz xz xz xz xz xz xz xz xz xz xz
oth a linked list and a vector).Random access would not work with a linked list, just a vector.
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
You would use a pointer- xz xz xz xz
based copying algorithm when the objects are very large and it would be costly to copy or w
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
hen you don't necessarily know the end of a container so you can use a nullptr as the null ter
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
minator. This is the idea behind vector.end() iterator which is an iterator that points one past
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz x
the end of a vector.
z xz xz xz xz
Are C++ strings null-terminated? - ans-no
xz xz xz xz xzxz
Give two examples of off-by-one bugs - ans-
xz xz xz xz xz xz xzxz
accessing one off the end of the array when comparing i <= SIZE. Assigning one off the end
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
of the array with arr[i] = arr[i + 1] when i = size - 1.
xz xz xz xz xz xz xz xz xz xz xz xz xz xz
How do I set up a two-dim array class? - ans-setup an array of int pointers:
xz xz xz xz xz xz xz xz xz xzxz xz xz xz xz xz xz
const int ** arr = new int * [R]; xz xz xz xz xz xz xz xz xz
The array is a collection of pointers, which is currently not looking at anything
xz xz xz xz xz xz xz xz xz xz xz xz xz
Perform an amortized complexity analysis of an automatically resizable container with a do
xz xz xz xz xz xz xz xz xz xz xz xz
ubling policy - ans- xz xz xzxz
When a call to add an element places the element one passed the end, the container will re
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
size to twice it's size. In amortized complexity analysis, we find the cost per operation over a
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz x
sequence of operations. In the worst case of the container reaching it's limit, the complexity
z xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
of the nth addition would be O(n). However, the next n-
xz xz xz xz xz xz xz xz xz xz
1 calls to add an element will have a O(1) complexity. So we have n + n(1) = 2n => O(n) com
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
plexity. O(n) / n = O(1) amortized complexity. xz xz xz xz xz xz xz
Discuss pros and cons of pointers and references when implementing container classes. -
xz xz xz xz xz xz xz xz xz xz xz xz
ans-Pointers vs references in container classes.
xzxz xz xz xz xz xz
ANSWERS
What is memory ownership for a container? - ans-
xz xz xz xz xz xz xz xzxz
When a container owns a value, only that container can modify the value. A drawback of thi
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
s is it takes a long time to copy containers like this. When a container has pointers, either the
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
container or other objects pointing to the same thing can modify, which can be unsafe. This
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
can be used for shared data. When a container owns a reference to a value, it doesn't own t
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
he value. You cannot delete by reference. The value must be initialized but cannot be assig
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
ned to. xz xz
student answer: xz xz
Memory ownership of a container refers to how controlled the interface is to the data held wi
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
thin the container. There are different degrees of ownership ranging from no control (as in t
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
he case of references) to a lot of control (which is the default from within the container). A co
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
ntainer class can control how much access objects outside the class can have over the ele
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
ments of the container (hence the class has a public interface for other objects).
xz xz xz xz xz xz xz xz xz xz xz xz xz xz
-
A container composed of values can completely restrict or give complete access to the ele
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
ments (the former being impractical and the latter being close to what vector does) but in eit
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
her situation the container class mutates the data.
xz xz xz xz xz xz xz
-
A container composed of pointers however is a different story. Because they contain pointe
xz xz xz xz xz xz xz xz xz xz xz xz xz xz
rs, the container only protects the location data (the pointer data) but it cannot protect or con
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
trol the interface to the data stores at the addresses. Thus, the container does not have total
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
control over the relevant data. Hypothetically, an object outside the container can make edi
xz xz xz xz xz xz xz xz xz xz xz xz xz xz
ts to the data.xz xz xz
What are some disadvantages of arrays? - ans-
xz xz xz xz xz xz xzxz
when many insertions are needed, we need to move everything over EVERY time we insert
xz xz xz xz xz xz xz xz xz xz xz xz xz xz x
a number.
z xz xz
One disadvantage of an array is that there are no bound checks so you increase the change
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
of illegally accessing memory (causing a seg fault) or you need O(1) complexity overhead li
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
ke a size variable to maintain bound checks.
xz xz xz xz xz xz xz
Why do you need a const and non-const version of some operators?
xz xz xz xz xz xz xz xz xz xz xz
What should non=const op[ ] return ? - ans-xz xz xz xz xz xz xz xzxz
Read and write. In some cases, the compiler knows it can speed some things up if it knows i
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
t never has to make any changes. Non const operator returns the object, which can be modi
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
fied. xz
, You would need both const and non-
xz xz xz xz xz xz
const version of operations because you would want to be as safe as possible. One good ru
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
le for C++ programming from the book "Effective C+
xz xz xz xz xz xz xz xz
+" highlights that you should make every variable you don't manipulate a const variable to p
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
rotect any data you can. Having methods return const makes your operation more resilient t
xz xz xz xz xz xz xz xz xz xz xz xz xz xz
o potential bugs (like accidently mutating data in a container when you didn't want to).
xz xz xz xz xz xz xz xz xz xz xz xz xz xz
how many destructor calls (min, max) can be invoked by
xz xz xz xz xz xz xz xz xz xz
operator delete xz xz
operator delete[ ] - ans- xz xz xz xzxz
A destructor should have only one call to delete for each new allocation to the heap and one
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
call to delete[] for each new[] allocation to the heap.
xz xz xz xz xz xz xz xz xz
Why would you use a pointer-based copying algorithm? - ans-Pointer-
xz xz xz xz xz xz xz xz xzxz
based algorithm works with both random access and sequential access (ex: will work with b
xz xz xz xz xz xz xz xz xz xz xz xz xz xz
oth a linked list and a vector).Random access would not work with a linked list, just a vector.
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
You would use a pointer- xz xz xz xz
based copying algorithm when the objects are very large and it would be costly to copy or w
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
hen you don't necessarily know the end of a container so you can use a nullptr as the null ter
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
minator. This is the idea behind vector.end() iterator which is an iterator that points one past
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz x
the end of a vector.
z xz xz xz xz
Are C++ strings null-terminated? - ans-no
xz xz xz xz xzxz
Give two examples of off-by-one bugs - ans-
xz xz xz xz xz xz xzxz
accessing one off the end of the array when comparing i <= SIZE. Assigning one off the end
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
of the array with arr[i] = arr[i + 1] when i = size - 1.
xz xz xz xz xz xz xz xz xz xz xz xz xz xz
How do I set up a two-dim array class? - ans-setup an array of int pointers:
xz xz xz xz xz xz xz xz xz xzxz xz xz xz xz xz xz
const int ** arr = new int * [R]; xz xz xz xz xz xz xz xz xz
The array is a collection of pointers, which is currently not looking at anything
xz xz xz xz xz xz xz xz xz xz xz xz xz
Perform an amortized complexity analysis of an automatically resizable container with a do
xz xz xz xz xz xz xz xz xz xz xz xz
ubling policy - ans- xz xz xzxz
When a call to add an element places the element one passed the end, the container will re
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
size to twice it's size. In amortized complexity analysis, we find the cost per operation over a
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz x
sequence of operations. In the worst case of the container reaching it's limit, the complexity
z xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
of the nth addition would be O(n). However, the next n-
xz xz xz xz xz xz xz xz xz xz
1 calls to add an element will have a O(1) complexity. So we have n + n(1) = 2n => O(n) com
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
plexity. O(n) / n = O(1) amortized complexity. xz xz xz xz xz xz xz
Discuss pros and cons of pointers and references when implementing container classes. -
xz xz xz xz xz xz xz xz xz xz xz xz
ans-Pointers vs references in container classes.
xzxz xz xz xz xz xz