Stack quiz

Answer each question to see whether you got it right and why. Your answers are saved in this browser.

0 correct · 0 / 51 answered
  1. Q1Stack basics

    A stack is which kind of structure?

  2. Q2Stack basics

    In a stack, all insertions and deletions are done at one end, called the top.

  3. Q3Stack basics

    You can read the third element from the top of a stack directly, without popping anything.

  4. Q4Stack basics

    Empty stack, then push(4), push(9), pop(), push(2), push(7), pop(). Type the two values returned by the pops, in order, separated by a space.

  5. Q5Stack basics

    Same sequence: push(4), push(9), pop(), push(2), push(7), pop(). Type the final stack from top to bottom, separated by spaces.

  6. Q6Stack basics

    Which of these is not a typical use of a stack?

  7. Q7Stack basics

    You push the letters of "ABC" one by one, then pop until empty. What is printed?

  8. Q8Stack ADT

    What does stackTop(el) do?

  9. Q9Stack ADT

    In the slides' ADT, pop returns the removed element through a reference parameter (pop(Type &)).

  10. Q10Stack ADT

    Popping from an empty stack is called:

  11. Q11Stack ADT

    Both the array stack and the linked stack in the slides provide stackIsFull().

  12. Q12Array stack

    In the array-based stack, what value of top means the stack is empty?

  13. Q13Array stack

    A Stackt was created with capacity MaxSize. When is it full?

  14. Q14Array stack

    What capacity does Stackt<int> s; get?

  15. Q15Array stack

    Which line correctly pushes v onto the array stack (after the full check)?

  16. Q16Array stack

    What does this print?

    Stackt<int> s(5);
    int x, y;
    s.push(10);
    s.push(20);
    s.push(30);
    s.pop(x);
    s.stackTop(y);
    cout << x << " " << y;
  17. Q17Array stack

    pop in the array stack erases the value from the array slot.

  18. Q18Array stack

    Why does Stackt define its own copy constructor?

  19. Q19Array stack

    Why does Stackt.h end with #include "Stackt.cpp"?

  20. Q20Array stack

    What is wrong with this push from the slides?

    template <class T> void Stack<T>::push(T k) {
        if (isFull()) {
            cout << "Stack is full\n";
        }
        cout << "Inserted element " << k << endl;
        top = top + 1;
        st[top] = k;
    }
  21. Q21Linked stack

    In the linked stack, top points to:

  22. Q22Linked stack

    Which two lines push the new node pnew (data already set), in the right order?

  23. Q23Linked stack

    Which is a correct order for linked-stack pop?

  24. Q24Linked stack

    A linked stack needs more memory per element than an array stack, but it is more flexible in size.

  25. Q25Linked stack

    Pushing onto an empty linked stack needs a special case.

  26. Q26Linked stack

    What should ~StackL() do?

  27. Q27Balanced symbols

    While checking brackets, you read a right symbol and the stack is empty. What does it mean?

  28. Q28Balanced symbols

    If every closer matched but the stack is not empty at the end of the input, the text is still balanced.

  29. Q29Balanced symbols

    { [ ( a + b ) * ( c - d ) ] } is balanced. If the final } is replaced with ], why does the check fail?

  30. Q30Balanced symbols

    Check { [ ( ) ] } with a stack. Type the stack after the third bracket, bottom to top, with no spaces. (Practice sheet Q18)

  31. Q31Balanced symbols

    Check { [ ( ] ) }. At which position (starting at 1, spaces don't count) is the first error? (Practice sheet Q18)

  32. Q32Postfix

    What is the postfix form of (A+B) * (C-(D+E))?

  33. Q33Postfix

    Why does postfix notation not need parentheses?

  34. Q34Postfix

    Evaluating 8 2 - with a stack: you pop 2 first, then 8. What is pushed?

  35. Q35Postfix

    Evaluate the postfix expression 2 3 + 2 4 1 + - *.

  36. Q36Postfix

    After the last token, a valid postfix expression leaves exactly one value on the stack.

  37. Q37Postfix

    Evaluate 18 6 2 / - 3 4 + * with a stack. Type the stack, bottom to top, right after the / token. (Practice sheet Q19)

  38. Q38Postfix

    Same expression 18 6 2 / - 3 4 + *: what is on the stack after the - token? (Practice sheet Q19)

  39. Q39Postfix

    Same expression 18 6 2 / - 3 4 + *: what is the final result? (Practice sheet Q19)

  40. Q40Postfix

    Using the stack algorithm (self-study), what is A + B * C - D in postfix?

  41. Q41Applications

    Converting 300 to hexadecimal by pushing n % 16 and dividing, then popping everything. What is printed?

  42. Q42Applications

    When searching a maze with a stack, what do you do at a dead end?

  43. Q43Applications

    The slides say the maze search will later be done recursively with the system stack. What is that algorithm called?

  44. Q44Applications

    Each function call pushes a frame on the run-time stack, and returning from the function pops it.

  45. Q45Array stack

    Array stack [3, 7] (7 on top), out = 0. Run peek(out). Type the return value and out, separated by a space. (Practice sheet Q16)

    bool pop(int& out) {
        if (empty()) return false;
        out = data[topIndex--];
        return true;
    }
    bool peek(int& out) const {
        if (empty()) return false;
        out = data[topIndex];
        return true;
    }
  46. Q46Array stack

    Using the Q16 code above, after the peek, run pop(out) three times. Type the value of out after each pop, separated by spaces. (Practice sheet Q16)

  47. Q47Array stack

    In the Q16 code above, a failed pop on an empty stack sets out back to 0. (Practice sheet Q16)

  48. Q48Linked stack

    Linked stack with 11 on top and 4 below it. Pop three times with the code below. What is (return, out, topNode) after the third pop? (Practice sheet Q17)

    bool pop(int& out) {
        if (!topNode) return false;
        Node* old = topNode;
        out = old->value;
        topNode = old->next;
        delete old;
        return true;
    }
  49. Q49Linked stack

    In the Q17 code above, why is old needed? (Practice sheet Q17)

  50. Q50Applications

    Maze (S at (1,1), G at (4,5)); try right, down, left, up; mark cells visited when pushed. Type the two cells that get popped, in order, as (r,c) (r,c). (Practice sheet Q20)

          1 2 3 4 5
      1   S . . # #
      2   . # # # #
      3   . . . # #
      4   # # . . G
  51. Q51Applications

    Using the Q20 maze above, what is the final path from S to G? (Practice sheet Q20)