Sample Data Structures Questions
Chapter 6
Stacks

Data Structures and Other Objects Using Java (Third Edition)
by Michael Main
ISBN 0-321-37525-4


The Purpose of These Questions

These are typical exam questions from Chapter 6 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

    Short Answers
    Sections 6.1 - 6.2
    Stacks and Their
    Applications
  1. Explain what modifications would be needed to make the parenthesis matching algorithm check expressions with more kinds of parentheses such as <>.

  2. Complete the body of this method. You do not need to check the precondition. You may use the CharStack class.
    
      public static boolean balanced(String p)
      // Precondition: Each character of p is '(', ')', '{' or '}'.
      // Postcondition: The method returns true if the characters form a
      // sequence of correctly balanced parentheses with each '(' matching
      // a ')' and each '{' matching a '}'. Note that a sequence such as
      // ( { ) } is NOT balanced because when we draw lines to match the
      // parentheses to their partners, the lines cross each other. On the
      // other hand, ( { } ) and { ( ) } are both balanced.
    

    Short Answers
    Section 6.3
    Implementations of
    the Stack ADT

  3. I am going to execute this code with THREE pushes and ONE pop:
    
       IntStack s = new IntStack( );
       s.push(1);
       s.push(2);
       s.push(3);
       System.out.println(s.pop( ));
    
    Suppose that s is represented by a partially filled array. Draw the state of the private instance variables of s after the above code:
    
               _______            __________________________________
     manyItems|       |      data|      |      |      |      |      |
              |_______|          |______|______|______|______|______|...
                                   [0]    [1]    [2]    [3]    [4]
    

  4. I am going to execute this code with THREE pushes and ONE pop:
    
       IntStack s = new IntStack( );
       s.push(1);
       s.push(2);
       s.push(3);
       System.out.println(s.pop( ));
    
    Suppose that s is represented by a linked list. Draw the state of the private member variables of s after the above code:
    
                   _______      
          head|       |  
                  |_______|     
    

    Short Answers
    Section 6.4
    More Complex
    Stack Applications

  5. Implement the following method. You may use the IntStack class and the Stack operations of push, pop, peek, isEmpty, and size. The parameter, in, is an EasyReader from Appendix B of the text and it is already attached to some kind of input. You may use the methods:
    • in.isEOLN() -- returns true when the end of line is reached.
    • in.peek() -- returns the next input character without actually reading it.
    • in.ignore() -- reads and throws away the next input character.
    • in.intInput() -- reads and returns an integer value from the EasyReader. This should be used only if you know that the next input characters form a valid integer value.
    The method specification is:
    
       public static int evaluatePostfix(EasyReader in)
       // Precondition (Which is not checked): The next input line of in is a
       // properly formed postfix expression consisting of integers, 
       // the binary operations + and -, and spaces. 
       // Postcondition: The method has read the next input line (including
       // the newline) and returned the value of the postfix expression.
    

  6. 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:
    
                  |       |  
                  |   +   |  
                  |   (   |  
           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

    Multiple Choice
    Sections 6.1-6.2
    Stacks and Their
    Applications
  1. Entries in a stack are "ordered". What is the meaning of this statement?
    • A. A collection of Stacks can be sorted.
    • B. Stack entries may be compared with the '<' operation.
    • C. The entries must be stored in a linked list.
    • 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:
    • A. add
    • B. append
    • C. insert
    • D. push

  3. The operation for removing an entry from a stack is traditionally called:
    • A. delete
    • B. peek
    • C. pop
    • D. remove

  4. Which of the following stack operations could result in stack underflow?
    • A. is_empty
    • B. pop
    • C. push
    • D. Two or more of the above answers

  5. Which of the following applications may use a stack?
    • A. A parentheses balancing program.
    • B. Keeping track of local variables at run time.
    • C. Syntax analyzer for a compiler.
    • D. All of the above.

  6. Consider the following pseudocode:
    
       declare a stack of characters
       while ( there are more characters in the word to read )
       {
          read a character
          push the character on the stack
       }
       while ( the stack is not empty )
       {
          pop a character off the stack
          write the character to the screen
       }
    
    What is written to the screen for the input "carpets"?
    • A. serc
    • B. carpets
    • C. steprac
    • D. ccaarrppeettss

  7. Here is an INCORRECT pseudocode for the algorithm which is supposed to determine whether a sequence of parentheses is balanced:
    
       declare a character stack 
       while ( more input is available)
       {
          read a character
          if ( the character is a '(' ) 
             push it on the stack
          else if ( the character is a ')' and the stack is not empty )
             pop a character off the stack
          else
             print "unbalanced" and exit
        }
        print "balanced"
    
    Which of these unbalanced sequences does the above code think is balanced?
    • A. ((())
    • B. ())(()
    • C. (()()))
    • D. (()))()

  8. 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: (()(())(()))?
    • A. 1
    • B. 2
    • C. 3
    • D. 4
    • E. 5 or more

  9. 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 during the computation?
    • A. 1
    • B. 2
    • C. 3
    • D. 4
    • E. 5 or more

    Multiple Choice
    Section 6.3
    Implementations of
    the Stack ADT

  10. 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 method place the new entry in the array?
    • A. data[0]
    • B. data[1]
    • C. data[9]
    • D. data[10]

  11. 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 the Stack at the last used position of the array?
    • A. Both peek and pop would require linear time.
    • B. Both push and pop would require linear time.
    • C. The Stack could not be used to check balanced parentheses.
    • D. The Stack could not be used to evaluate postfix expressions.

  12. In the linked list implementation of the stack class, where does the push method place the new entry on the linked list?
    • A. At the head
    • B. At the tail
    • C. After all other entries that are greater than the new entry.
    • D. After all other entries that are smaller than the new entry.

  13. In the array version of the Stack class, which operations require linear time for their worst-case behavior?
    • A. is_empty
    • B. peek
    • C. pop
    • D. push when the stack is below capacity
    • E. None of these operations require linear time.

  14. In the linked-list version of the Stack class, which operations require linear time for their worst-case behavior?
    • A. is_empty
    • B. peek
    • C. pop
    • D. push
    • E. None of these operations require linear time.

    Multiple Choice
    Section 6.4
    More Complex
    Stack Applications

  15. What is the value of the postfix expression 6 3 2 4 + - *:
    • A. Something between -15 and -100
    • B. Something between -5 and -15
    • C. Something between 5 and -5
    • D. Something between 5 and 15
    • E. Something between 15 and 100

  16. 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 TIME during the conversion of this expression?
    • A. 1
    • B. 2
    • C. 3
    • D. 4
    • E. 5



Michael Main (main@colorado.edu)