stack ques
DESCRIPTION
16TRANSCRIPT
7/16/2019 Stack Ques
http://slidepdf.com/reader/full/stack-ques 1/6
http://www.cs.colorado.edu/~main/supplements/questio
n.html
Sample Data Structures Questions
Chapter 7
Stacks
Data Structures and Other Objects Using C++
by Michael Main and Walter Savitch
Second Edition ISBN 0-201-70297-5, Softcover, 816 pages, 2000
The Purpose of These Questions
These are typical exam questions from Chapter 7 of the textbook. These exact questions
might not be on your exam, but if you research and find the right answers to these
questions, that should be good preparation for a real exam. (It's also possible that some of this material was not covered in your class.) There are 6 short answer questions and 16
multiple choice questions in this file.
Short Answers
1. Explain what modifications would be needed to make the parenthesis matching algorithm check expressions withdifferent kinds of parentheses such as (), [] and {}'s.
2. Complete the body of this function. You do not need to check the precondition.
You may use the stack template class.3.4. bool balanced(const char p[ ], size_t n)5. // Precondition: p[0]...p[n-1] contains n characters, each of
which6. // is '(', ')', '{' or '}'.7. // Postcondition: The function returns true if the characters
form a8. // sequence of correctly balanced parentheses with each '('matching
9. // a ')' and each '{' matching a '}'. Note that a sequence suchas
10. // ( { ) } is NOT balanced because when we draw lines to matchthe
11. // parentheses to their partners, the lines cross each other.On the
12. // other hand, ( { } ) amd { ( ) } are both balanced.
Short AnswersSections 7.1 - 7.2Stacks and Their
Applications
7/16/2019 Stack Ques
http://slidepdf.com/reader/full/stack-ques 2/6
13. I am going to execute this code with THREE pushes and
ONE pop:
14.15. stack<int> s;16. s.push(1);
17. s.push(2);18. s.push(3);19. s.pop( );
Suppose that s is represented by a partially filled array. Draw the state of the private member variables of s after the above code:
_______ __________________________________used| | data| | | | | |
|_______| |______|______|______|______|______|[0] [1] [2] [3] [4]
20. I am going to execute this code with THREE pushes and ONE pop:21.22. stack<int> s;23. s.push(1);24. s.push(2);25. s.push(3);26. cout << s.pop( );
Suppose that s is represented by a linked list. Draw the state of the privatemember variables of s after the above code:
_______
head_ptr| |
|_______|
27. Implement the following function. You may use the stack template class and the
stack operations of push, pop, peek, is_empty, and size. You may also use
cin.peek( ) and use "cin>>i" to read an integer.28.29. int evaluate_postfix_from_cin( )
30. // Precondition (Which is not checked): The next input lineof cin is a31. // properly formed postfix expression consisting of integers,32. // the binary operations + and -, and spaces.33. // Postcondition: The function has read the next input line
(including34. // the newline) and returned the value of the postfix
expression.35. {36. int i;
Short AnswersSection 7.3
Implementations of
the stack ADT
Short Answers
Section 7.4More Complex
Stack Applications
7/16/2019 Stack Ques
http://slidepdf.com/reader/full/stack-ques 3/6
37. stack<int> s;
38. Consider the usual algorithm to convert an infix expression to a postfix
expression. Suppose that you have read 10 input characters during a conversion
and that the stack now contains these symbols:39.40. | |
41. | + |42. | ( |43. bottom |___*___|
Now, suppose that you read and process the 11th symbol of the input. Draw the
stack for the case where the 11th symbol is:
A. A number:
B. A left parenthesis:
C. A right parenthesis:
D. A minus sign:
E. A division sign:
Multiple Choice
1. Entries in a stack are "ordered". What is the meaning of this statement?
o A. A collection of stacks can be sorted.
o B. Stack entries may be compared with the '<' operation.
o C. The entries must be stored in a linked list.
o D. There is a first entry, a second entry, and so on.
2. The operation for adding an entry to a stack is traditionally called:
o A. add
o B. append
o C. insert
o D. push
3. The operation for removing an entry from a stack is traditionally called:
o A. delete
o B. peek
o C. pop
o D. remove
4. Which of the following stack operations could result in stack underflow?
o A. is_empty
Multiple ChoiceSections 7.1-7.2
Stacks and Their
Applications
7/16/2019 Stack Ques
http://slidepdf.com/reader/full/stack-ques 4/6
o B. pop
o C. push
o D. Two or more of the above answers
5. Which of the following applications may use a stack?
o A. A parentheses balancing program.
o B. Keeping track of local variables at run time.o C. Syntax analyzer for a compiler.
o D. All of the above.
6. Consider the following pseudocode:7.8. declare a stack of characters9. while ( there are more characters in the word to read )10. {11. read a character12. push the character on the stack13. }14. while ( the stack is not empty )15. {
16. write the stack's top character to the screen17. pop a character off the stack18. }
What is written to the screen for the input "carpets"?
o A. serc
o B. carpets
o C. steprac
o D. ccaarrppeettss
19. Here is an INCORRECT pseudocode for the algorithm which is supposed to
determine whether a sequence of parentheses is balanced:20.21. declare a character stack22. while ( more input is available)23. {24. read a character25. if ( the character is a '(' )26. push it on the stack27. else if ( the character is a ')' and the stack is not
empty )28. pop a character off the stack29. else30. print "unbalanced" and exit31. }
32. print "balanced"
Which of these unbalanced sequences does the above code think is balanced?
o A. ((())
o B. ())(()
o C. (()()))
o D. (()))()
7/16/2019 Stack Ques
http://slidepdf.com/reader/full/stack-ques 5/6
33. Consider the usual algorithm for determining whether a sequence of parentheses
is balanced. What is the maximum number of parentheses that will appear on the
stack AT ANY ONE TIME when the algorithm analyzes: (()(())(()))?
o A. 1
o B. 2
o C. 3o D. 4
o E. 5 or more
34. Consider the usual algorithm for determining whether a sequence of parentheses
is balanced. Suppose that you run the algorithm on a sequence that contains 2 left
parentheses and 3 right parentheses (in some order). What is the maximum
number of parentheses that will ever appear on the stack AT ONE TIME duringthe computation?
o A. 1
o B. 2
o C. 3
o D. 4
o E. 5 or more
35. Suppose we have an array implementation of the stack class, with ten items in the
stack stored at data[0] through data[9]. The CAPACITY is 42. Where does the
push member function place the new entry in the array?
o A. data[0]
o B. data[1]o C. data[9]
o D. data[10]
36. Consider the implementation of the stack using a partially-filled array. What goes
wrong if we try to store the top of the stack at location [0] and the bottom of thestack at the last used position of the array?
o A. Both peek and pop would require linear time.
o B. Both push and pop would require linear time.
o C. The stack could not be used to check balanced parentheses.
o D. The stack could not be used to evaluate postfix expressions.
37. In the linked list implementation of the stack class, where does the push member
function place the new entry on the linked list?o A. At the head
o B. At the tail
o C. After all other entries that are greater than the new entry.
o D. After all other entries that are smaller than the new entry.
38. In the array version of the stack class (with a fixed-sized array), which operations
require linear time for their worst-case behavior?
o A. is_empty
Multiple Choice
Section 7.3
Implementations of the stack ADT
7/16/2019 Stack Ques
http://slidepdf.com/reader/full/stack-ques 6/6
o B. peek
o C. pop
o D. push
o E. None of these operations require linear time.
39. In the linked-list version of the stack class, which operations require linear time
for their worst-case behavior?o A. is_empty
o B. peek
o C. pop
o D. push
o E. None of these operations require linear time.
40. What is the value of the postfix expression 6 3 2 4 + - *:o A. Something between -15 and -100
o B. Something between -5 and -15
o C. Something between 5 and -5
o D. Something between 5 and 15
o E. Something between 15 and 100
41. Here is an infix expression: 4+3*(6*3-12). Suppose that we are using the usual
stack algorithm to convert the expression from infix to postfix notation. What is
the maximum number of symbols that will appear on the stack AT ONE TIMEduring the conversion of this expression?
o A. 1
o B. 2o C. 3
o D. 4
o E. 5
Data Structures and Other Objects Using C++
Michael Main ( [email protected] )
and
Walter Savitch ( [email protected] )
Thank you for visiting http://www.cs.colorado.edu/~main/questions/chap07q.html
Copyright © 2000 Addison-Wesley Computer and Engineering Publishing Group
Multiple Choice
Section 7.4More Complex
Stack Applications