RG's E-Learning Centre · Book a free demo class · WhatsApp +91-9379416374

Stack operations: Class 12 practice

A stack is last-in, first-out (LIFO): push adds to the top and pop removes the top. Read the method, follow the three worked examples, then try the six questions.

What you should know first

  • Python lists

The idea behind stack operations

A stack is last-in, first-out (LIFO): push adds to the top and pop removes the top. In Python a list works as a stack with append() and pop().

Step-by-step method

  1. Start with the given stack (top at the right).
  2. Apply each operation in order.
  3. Record popped values and the final stack.

Worked example 1 (Basic)

Starting with an empty stack, perform: push(19), push(59), push(7), push(5), push(65), push(21). What values are popped and what is the final stack (bottom → top)?

  1. Popped: none
  2. Final: [19, 59, 7, 5, 65, 21]
  3. Answer: Popped none; final [19, 59, 7, 5, 65, 21]

Worked example 2 (Standard)

Starting with an empty stack, perform: push(54), push(7), pop(), pop(), push(5), push(44), push(22), push(30). What values are popped and what is the final stack (bottom → top)?

  1. Popped: 7, 54
  2. Final: [5, 44, 22, 30]
  3. Answer: Popped 7, 54; final [5, 44, 22, 30]

Worked example 3 (Challenge)

Starting with an empty stack, perform: push(3), push(98), pop(), push(58), pop(), push(24), push(7), push(94), pop(), push(83). What values are popped and what is the final stack (bottom → top)?

  1. Popped: 98, 58, 94
  2. Final: [3, 24, 7, 83]
  3. Answer: Popped 98, 58, 94; final [3, 24, 7, 83]

Common mistakes and misconceptions

  • Popping from the bottom
  • Confusing a stack with a queue

Practice questions

  1. (Basic) Starting with an empty stack, perform: push(22), pop(), push(62), push(60), push(12), pop(). What values are popped and what is the final stack (bottom → top)?
  2. (Basic) Starting with an empty stack, perform: push(93), pop(), push(46), push(55), push(56), pop(). What values are popped and what is the final stack (bottom → top)?
  3. (Standard) Starting with an empty stack, perform: push(88), push(79), push(9), push(96), push(72), pop(), push(33), push(61). What values are popped and what is the final stack (bottom → top)?
  4. (Standard) Starting with an empty stack, perform: push(31), push(93), push(95), push(66), pop(), push(91), pop(), push(31). What values are popped and what is the final stack (bottom → top)?
  5. (Challenge) Starting with an empty stack, perform: push(57), push(9), pop(), pop(), push(69), push(62), push(89), pop(), push(58), push(87). What values are popped and what is the final stack (bottom → top)?
  6. (Challenge) Starting with an empty stack, perform: push(71), push(88), pop(), push(38), pop(), pop(), push(16), push(88), pop(), pop(). What values are popped and what is the final stack (bottom → top)?

Answer key with full working

  • Q1: Popped: 22, 12 → Final: [62, 60] → Answer: Popped 22, 12; final [62, 60]
  • Q2: Popped: 93, 56 → Final: [46, 55] → Answer: Popped 93, 56; final [46, 55]
  • Q3: Popped: 72 → Final: [88, 79, 9, 96, 33, 61] → Answer: Popped 72; final [88, 79, 9, 96, 33, 61]
  • Q4: Popped: 66, 91 → Final: [31, 93, 95, 31] → Answer: Popped 66, 91; final [31, 93, 95, 31]
  • Q5: Popped: 9, 57, 89 → Final: [69, 62, 58, 87] → Answer: Popped 9, 57, 89; final [69, 62, 58, 87]
  • Q6: Popped: 88, 38, 71, 88, 16 → Final: [] → Answer: Popped 88, 38, 71, 88, 16; final []

Syllabus note

This skill is listed under "Data Structures" for Class 12 in the NCERT/CBSE books. ICSE, IGCSE, IB and state boards may teach it in a different chapter or year.

Answers on this page are generated and automatically re-checked by a second, independent calculation. They have not been reviewed by a teacher — if you spot a problem, tell us on WhatsApp.

Related practice and notes

Frequently asked questions

Which chapter is stack operations in?

"Data Structures", Class 12 Computer Science (NCERT/CBSE). Other boards may place it differently.

How were the answers checked?

Answers on this page are generated and automatically re-checked by a second, independent calculation. They have not been reviewed by a teacher — if you spot a problem, tell us on WhatsApp.

Book a free demo class