Chapter 7 Questions
Multiple Choice Questions
1. When is an array based implementation of the ADT stack not a better choice?
a) the stack can be large, but often is not
b) the stack does not exceed the fixed size of the array
c) the stack is most always right around 80 characters
d) the stack represents the number of students in a typical class
2. When should a constructor use an initializer?
a) when the value of the data member needs validation
b) when the stack is full
c) when the value of the data member has no restrictions
d) when the stack is empty
3. What is accomplished by hiding implementations of a stack in a class?
a) client code can access any entries of the stack
b) only the top of the stack can be accessed by client code
c) you can also treat the ADT as a queue
d) the code runs faster
4. In a link-based implementation of a stack you must write both a copy constructor and
a) a virtual constructor
b) a virtual destructor
c) a sort() method
d) a virtual instructor
5. What command is used to enforce a precondition that the peek() method cannot work on an empty stack.
a) result = false;
b) if (top < MAX_STACK – 1)
c) return top < 0;
d) assert (!isEmpty());
6. Given the following code:
template < class ItemType>
ArrayStack<ItemType>::ArrayStack() : top(–1)
{
}
What method is this?
a) the default destructor
b) the virtual destructor
c) the default constructor
d) the copy constructor
7. Given the following code:
template < class ItemType>
bool ArrayStack<ItemType>::doSomething()
{
bool result = false ;
if (!isEmpty())
Chapter 7 Questions
{
top–;
result = true ;
}
return result;
}
What method is this?
a) pop()
b) peek()
c) default constructor
d) copy constructor
8. Given the following code:
template < class ItemType>
ItemType ArrayStack<ItemType>::doSomething() const
{
assert (!isEmpty());
return items[top];
}
What method is this?
a) pop()
b) peek()
c) default constructor
d) copy constructor
9. If you implement a stack using an array, where should the bottom element of the stack be placed?
a) at the end of the array
b) mid array for a binomial search
c) at the first element of the array
d) it doesn’t matter
10. If you implement a stack using a chain of linked nodes that has a head pointer, where should the top of the stack
be placed for easiest and fastest access?
a) at the first node of the chain
b) at the top of the tree structure
c) at the last node of the chain
d) it doesn’t matter
11. Why would a stack be a poor choice for implementing an ADT bag?
a) it might fill up
b) you couldn’t account for duplicates
c) you need to be able to look through all the elements in the bag, not just the top of the stack
d) actually it would be a good choice
12. What kind of method might be most useful during program debugging?
a) return the bottom element of the stack
b) display the contents of the whole stack
c) sort the items in the stack
d) reverse the order of the items in the stack
Chapter 7 Questions
13. Given the following code:
template < class ItemType>
LinkedStack<ItemType>::~doSomething()
{
while (!isEmpty())
pop();
}
What does this method do?
a) it is the destructor
b) it is the constructor
c) it displays the contents of the whole stack
d) if the stack is not empty, it returns the top of the stack
14. The return type of the peek() operation in the array-based and pointer-based implementations of a stack is
a) ItemType
b) bool
c) int
d) assert
15. In the array based implementation of a stack class, what does the constructor initialize the private variable top
to? a) 0
b) -1
c) 1
d) MAX_STACK
16. In the linked based implementation of a stack class, what does the default constructor initialize the private
variable topPtr to?
a) OrigChainPtr
b) newChainPtr
c) nullPtr
d) 0
17. In the linked based implementation of a stack class, what method besides push() allocates a new node?
a) pop()
b) display()
c) default constructor
d) copy constructor
18. In designing a link based implementation of the stack, what standard exception did the author decide to ignore?
a) bad_alloc
b) logic_error
c) PrecondViolatedExcep
d) nullPtrError
19. In the array based implementation of the stack is an if statement
if (top < MAX_STACK – 1) … // What is this checking for?
a) a null pointer error
b) does the stack have room for a new entry
Chapter 7 Questions
c) is the array empty
d) where should we put the new entry
20. Given this graphic of an array based stack.
What would be returned by a call to the method peek()
a) 2
b) 10
c) 20
d) 30
Chapter 7 Questions
True/False Questions
1. A program can use the operations of the ADT stack without knowing how the operations are implemented.
2. In an array based implementation of a stack, the stack can grow and shrink dynamically.
3. In the link based implementation of the stack, both the method push and the copy constructor allocate new
nodes.
4. Private data members are hidden from the client.
5. A class need not validate data given by client code. According to the author, the client code should do that.
6. In both implementations of the class stack, the pop methods returned a value of type bool.
Chapter 7 Questions
Short Answer Questions
1. What is the difference between the stack pop and peek operations?
2. What restriction does the array-based implementation of a stack place on the push operation?
3. What is the purpose of using an assert or throwing an exception in the peek method?
4. In the array implementation of a stack, why should items and top be specified as private?
5. Why must both a copy constructor and a virtual destructor be used in implementing a stack with a linked list?
6. Give an example of when a link based implementation of a stack is a better choice.
7. When writing a constructor how do you decide whether to use an initializer versus the set method to give initial
values to the values of a class’s data members>
8. Why did the author decide not to deal with a bad_alloc exception in designing the link based stack?
9. In an array based implementation of a stack, why is the bottom of the stack the first element of the array?
10. In a link based implementation of a stack, why should the first node of the stack contain the top of the stack?