Professional Documents
Culture Documents
Data Abstraction and Problem Solving With C++ Walls and Mirrors 7th Edition Carrano Test Bank 1
Data Abstraction and Problem Solving With C++ Walls and Mirrors 7th Edition Carrano Test Bank 1
Data Abstraction and Problem Solving with C++ Walls and Mirrors 7th
Edition Carrano
Full download at:
Solution Manual:
https://testbankpack.com/p/solution-manual-for-data-abstraction-and-problem-solving-with-c-
walls-and-mirrors-7th-edition-by-carrano-henry-isbn-0134463978-9780134463971/
Test bank:
https://testbankpack.com/p/test-bank-for-data-abstraction-and-problem-solving-with-c-walls-
and-mirrors-7th-edition-by-carrano-henry-isbn-0134463978-9780134463971/
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
Answer: a.
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
Answer: B.
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());
Answer: D.
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
Answer: C
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
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
Answer: C.
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
Answer: B.
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
Answer: A.
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
Answer: B
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
Answer: C.
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
Answer: D.
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
Answer: A.
True/False Questions
1. A program can use the operations of the ADT stack without knowing how the operations are implemented.
Answer: True.
2. In an array based implementation of a stack, the stack can grow and shrink dynamically.
Answer: False
3. In the link based implementation of the stack, both the method push and the copy constructor allocate new
nodes.
Answer: True
5. A class need not validate data given by client code. According to the author, the client code should do that.
Answer: False
6. In both implementations of the class stack, the pop methods returned a value of type bool.
Answer: True
1. What is the difference between the stack pop and peek operations?
Answer: The pop operation removes the top of the stack. The peek operation retrieves the top of the stack, but
does not remove it.
2. What restriction does the array-based implementation of a stack place on the push operation?
Answer: The array-based implementation of a stack prevents the push operation from adding an item to the stack
when the array is full.
3. What is the purpose of using an assert or throwing an exception in the peek method?
Answer: It enforces the precondition that the stack must contain at least one entry for the peek to be used.
4. In the array implementation of a stack, why should items and top be specified as private?
Answer: You ensure that client code cannot violate the ADT’s walls. Otherwise the client could access entries in
the array directly instead of using the ADT stack operations.
5. Why must both a copy constructor and a virtual destructor be used in implementing a stack with a linked list?
Answer: Memory is allocated dynamically. A shallow copy is insufficient, it would only copy the pointer topPtr.
The copy constructor is needed to make a copy of all the nodes in the linked stack.
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>
Answer: Use an initializer if the value of the data members has no restrictions. Use a set method if the value of the
data member needs validation. Alternatively you could have the constructor perform its own validation.
8. Why did the author decide not to deal with a bad_alloc exception in designing the link based stack?
Answer: Even though both push and the copy constructor allocate new nodes and could fail if the system has no
spare memory available, this would probably not happen in the simple applications we are working with. Also most
computing systems today do not have severely restricted memory size.
9. In an array based implementation of a stack, why is the bottom of the stack the first element of the array?
Answer: There is no need to move or shift entries in the array when adding or removing entries of the stack.
10. In a link based implementation of a stack, why should the first node of the stack contain the top of the stack?
Answer: It provides easiest and fastest addition and removal operations.