You are on page 1of 6

Chapter 7 Questions

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/

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
Answer: a.

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
Answer: C.

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
Answer: B

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.

© 2017 Pearson Education, Inc., Hoboken, NJ. All rights reserved.


Chapter 7 Questions

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
Answer: C.

7. Given the following code:


template < class ItemType>
bool ArrayStack<ItemType>::doSomething()
{
bool result = false ;
if (!isEmpty())
{
top--;
result = true ;
}
return result;
}
What method is this?
a) pop()
b) peek()
c) default constructor
d) copy constructor
Answer: A

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
Answer: B.

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

© 2017 Pearson Education, Inc., Hoboken, NJ. All rights reserved.


Chapter 7 Questions

c) at the last node of the chain


d) it doesn’t matter
Answer: A.

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.

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
Answer: A.

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?

© 2017 Pearson Education, Inc., Hoboken, NJ. All rights reserved.


Chapter 7 Questions

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.

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
c) is the array empty
d) where should we put the new entry
Answer: B.

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
Answer: D

© 2017 Pearson Education, Inc., Hoboken, NJ. All rights reserved.


Chapter 7 Questions

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

4. Private data members are hidden from the client.


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

© 2017 Pearson Education, Inc., Hoboken, NJ. All rights reserved.


Chapter 7 Questions

Short Answer Questions

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.

6. Give an example of when a link based implementation of a stack is a better choice.


Answer: A link based implementation would be required if a stack is occasionally large, but most of the time is not.
Otherwise, memory is wasted in a large array that is rarely required.

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.

© 2017 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

You might also like