Java Stacks Quiz

Home

Question: 0 1 2 3 4 5 6 7 8 9 10

Question Zero

What best describes the stack paradigm?


Which method inserts an element?


Which method looks at the top element??


Which method removes the top element?


Which method checks if there are elements in the stack?


top next

Question One

  public class StackTest{
    public static void main(String[] args){
      Stack<Integer> stack = new Stack<Integer>();
      
      for(int i = 0; i < 10; i++)
        stack.push(i);

      stack.pop();
      stack.pop();
      stack.pop();
    }
  }
  

What is the value returned when stack.peek() is called?

stack.peek()=


back top next

Question Two

  public class StackTest{
    public static void main(String[] args){
      Stack<Integer> stackOne = new Stack<Integer>();
      Stack<Integer> stackTwo = new Stack<Integer>();
      
      for(int i =0; i<5; i++)
        stackOne.push(i);
      
      stackTwo.push(stackOne.pop());
      stackTwo.push(stackOne.pop());
      stackTwo.push(stackOne.pop());
    }
  }
  

What are the values returned by peeking on each stack?

stackOne.peek()=


stackTwo.peek()=


back top next

Question Three

  public class Test{
    public static void main(String[] args){
      Stack<Integer> stack = new Stack<Integer>();
      int n = 12
      while (n > 0){
        stack.push(n%2);
        n = n/2;
      }
      String result = "";
      while (!stack.isEmpty())
        results += stack.pop();
      System.out.println(result)
    }
  }
  

What is printed out?

result=


back top next


Question Four

  public class Test{
    public static void main(String[] args){
      Stack<Integer> stack = new Stack<Integer>();
      Scanner scanner = new Scanner(System.in)
      while (true){
        char ch = scanner.next();
        if(ch == '(')
          stack.push(1);
        else if (ch == ')' && !stack.isEmpty())
          stack.pop();
        else
          System.println("unbalanced");
      }
    }
  }
  

Which unbalanced input does this program consider correct?

(()()))
(()))()
((())
())(()


back top next


Question Five

  public class Test{
    public static void main(String[] args){
      Stack<Integer> stackOne = new Stack<Integer>();
      Stack<Integer> stackTwo = new Stack<Integer>();
      for(int i = 0; i < 100; i++)
        stackOne.push(i);
      for(int i = 0; i < 100; i++)
        if(i%4==0)
          stackTwo.push(stackOne.peek());
      }
  }
  

How many elements are in each Stack?

stackOne size:


stackTwo size:


back top next

Question Six

Which is something you would not use a stack for?

Reversing a word
Breadth First Search
Depth-First Search
backtracking (ie. edit->undo)


back top next


Question Seven

How many calls to pop() is need to determine the size of a stack? Answer in terms of n where n is the number of elements in the stack.

n (linear)
nlog(n) (logarithmic)
n^x (polynomial)
x^n (exponential)


back top next


Question Eight

Which data structures can easily represent a stack?
Choose all that apply.

Binary Tree
Linked List
Array
Queue


back top next


Question Nine (Challenging - not tested)

Implement a stack with two queues

back top next


Question Ten

Which Stack is more helpful?

https://stackexchange.com/
https://stackoverflow.com/


back top

Home