Worksheets › Class 12 › Stack

Class 12 · Computer Science worksheet

Stack

CBSE Computer Science. LIFO, push and pop on a list, underflow, matching brackets, infix to postfix conversion and evaluating postfix expressions with a stack. Every sheet has 20 questions in three sections and an answer key, and the questions change every time.

Open a printable sample sheet

Sample questions

Real questions from this chapter, as the app sets them. Tap Answer to see the answer and working.

Section A · multiple choice

  1. 1.

    A stack works on the principle of:

    1. random access
    2. smallest first
    3. First In, First Out (FIFO)
    4. Last In, First Out (LIFO)
    Answer

    (d) Last In, First Out (LIFO)

  2. 2.

    What is the last line this program prints? def push(s, x):     s.append(x) def pop(s):     if len(s) == 0:         return 'Underflow'     return s.pop() st = [] push(st, 86) push(st, 89) push(st, 81) print(pop(st)) push(st, 63) print(pop(st)) push(st, 83) print(st)

    1. [86, 89, 83]
    2. [89, 86, 83]
    3. [89, 83]
    4. [86, 83, 89]
    Answer

    (a) [86, 89, 83]

    the last item pushed is the first popped (LIFO)

  3. 3.

    What is the first line this program prints? def push(s, x):     s.append(x) def pop(s):     if len(s) == 0:         return 'Underflow'     return s.pop() st = [] push(st, 55) push(st, 58) push(st, 75) print(pop(st)) print(pop(st)) push(st, 91) print(pop(st)) push(st, 19) print(st)

    1. 75
    2. 19
    3. None
    4. 55
    Answer

    (a) 75

    pop() returns the item on top: the one pushed most recently

Section B · short answer

  1. 4.

    What is a stack? Give one example from daily life.

    Answer

    A linear data structure in which items are added and removed only at one end, the top (LIFO); e.g. a pile of plates.

  2. 5.

    What does this program print? Write each line in order, separated by commas. def push(s, x):     s.append(x) def pop(s):     if len(s) == 0:         return 'Underflow'     return s.pop() st = [] push(st, 10) push(st, 50) push(st, 95) push(st, 84) print(pop(st)) print(pop(st)) push(st, 83) print(st)

    Answer

    84, 95, [10, 50, 83]

    LIFO: each pop takes the most recent push

Section C · long answer

  1. 6.

    Write a menu-driven Python program to implement a stack using a list, with push, pop, peek and display.

    Answer

    st = []; push: st.append(x); pop: if st: print(st.pop()) else print("Underflow"); peek: print(st[len(st) − 1]) if st else "Empty"; display: for x in reversed(st): print(x). A while loop shows the menu and calls the chosen function.

Fresh sheets for your child

In the app, every Stack sheet is new: the app remembers the questions your child has already had and leaves them out. Print the sheet as a PDF, or use notebook mode and write the answers in a notebook.

Coming soon for Android and iPhone.

More Class 12 Computer Science