Question 333 marksShort AnswerSection C

(a) A stack named FruitStack, implemented using list, contains records of some fruits. Each record is represented as a dictionary with keys Name', Origin', Price', and Expiry'. A sample record is given here :

``python

{'Name':'Apple','Origin':'France','Price':120,

'Expiry':'12-08-2025'}

`

Write the following user-defined functions in Python to perform the specified operations on FruitStack :

(i) push_fruit(FruitStack, Fruit): This function takes the stack FruitStack and a new record Fruit as arguments and pushes the record stored in Fruit onto FruitStack if the Price is less than 100.

(ii) pop_fruit(FruitStack): This function pops the topmost record from the stack and returns it. If the stack is already empty, the function should display "UNDERFLOW".

(iii) display(FruitStack): This function displays all the elements of the stack starting from the topmost element. If the stack is empty, the function should display EMPTY STACK'.

OR

(b) Write a Python program to accept 10 integers from the user. If the entered number is a three-digit even integer, push it onto a stack. After all inputs are taken, pop all the three-digit even integers from the stack and display them. For example, if the user enters 12, 31, 320, 457, 6, 92, 924, 220, 1, 218, then the stack should contain :

320, 924, 220, 218

and the output of the program should be :

218 220 924 320

Data Structures using Python — Stack
Stack Implementation using List — PUSH, POP and Display Operations

Official answer

Two independent OR options are answered in full below — a student attempts only one.


Part (a) — Stack of Fruit records

``python

def push_fruit(FruitStack, Fruit):

if Fruit['Price'] < 100:

FruitStack.append(Fruit)


def pop_fruit(FruitStack):

if FruitStack == []:

print("UNDERFLOW")

else:

return FruitStack.pop()


def display(FruitStack):

if FruitStack == []:

print("EMPTY STACK")

else:

top = len(FruitStack) - 1

while top >= 0:

print(FruitStack[top])

top -= 1

`


Part (b) — Three-digit even integers on a stack

`python

Stack = []

for i in range(10):

num = int(input("Enter a number: "))

if len(str(num)) == 3 and num % 2 == 0:

Stack.append(num)


while Stack:

print(Stack.pop(), end=' ')

``

Answer keywords:stackpushpopLIFOappend()UNDERFLOWEMPTY STACKlist as stack

Marking scheme

  1. 1.Part (a): 1 mark for push_fruit() with the Price<100 condition; 1 mark for pop_fruit() with the UNDERFLOW check; 1 mark for display() printing top-to-bottom with the EMPTY STACK check.
  2. 2.Part (b): 1 mark for correctly reading 10 integers and testing three-digit + even (len(str(num))==3 and num%2==0); 1 mark for pushing qualifying numbers onto the stack; 1 mark for popping and printing all stack elements in LIFO order.
  3. 3.Either OR option, correctly and completely coded, earns the full 3 marks.

Hint

Stack push = append(), pop = list.pop(); always check for an empty list before popping or displaying.

Quick oral answer

A stack follows LIFO; in Python we push with list.append() and pop with list.pop(), always testing 'if stack == []' first to catch underflow before removing an element.

Common mistakes

  1. 1.Forgetting to check for UNDERFLOW/EMPTY STACK before popping or displaying an empty list, causing an IndexError in real code.
  2. 2.In display(), iterating the list front-to-back instead of from the last index backwards, which prints the stack in the wrong (non-LIFO) order.
  3. 3.In part (b), checking num>=100 and num<=999 with the wrong operator, or forgetting the even-number condition entirely.

Analysis and explanation

Both parts test the LIFO (Last-In-First-Out) behaviour of a stack implemented on a Python list.


Concept

  • append() pushes an item onto the top of the list-stack; pop() removes and returns the topmost (last) item — this is exactly LIFO.
  • An empty list stack ([]) must be checked before every pop/display to avoid an UNDERFLOW error.

Part (a) — condition-guarded push

  • push_fruit only appends when Price < 100, so costlier fruit is silently rejected.
  • display() must print from the LAST index backwards, since the top of the stack is the last appended element, not the first.

Part (b) — filtering while stacking

  • len(str(num)) == 3 is a quick built-in way to test "three-digit" without arithmetic range checks.
  • Because pop() always removes the last-pushed number first, the popped/output order is naturally the reverse of the input order — 218, 220, 924, 320 for the given sample.

Exam trap

  • Forgetting the UNDERFLOW/EMPTY STACK messages costs marks even if the push logic itself is correct.

Interesting facts

A Python list already provides fast append/pop-from-end operations, which is exactly why list.append() and list.pop() are the standard way to build a stack without importing any extra module.

The collections.deque class is often preferred over a plain list for very large stacks because it avoids the occasional memory reallocation that a growing list can incur.

The stack data structure is named after a physical stack of plates — you can only add or remove from the top, never from the middle.

Question details

Q33 of 48 questions

Board
CBSE
Class
Class 12
Subject
Computer Science
Year
2026
Section
Section C
Marks
3
Type
Short Answer
Chapter
Data Structures using Python — Stack
Topic
Stack Implementation using List — PUSH, POP and Display Operations
Bloom's level
apply
Syllabus topic
Unit 1: Computational Thinking and Programming – II — Data Structures: Stack (LIFO) implemented using a List; user-defined PUSH/POP/Traversal functions
Author
CBSE
Reviewed by
Padhantu Academic Team

Spotted a mistake or something unclear?

Tell us and we will fix it.

Frequently asked questions

Why does display() print from the last index backwards?

Because the top of a stack is the last element pushed (highest index in the list); printing top-to-bottom means starting from len(FruitStack)-1 down to 0.

What does len(str(num))==3 check?

Converting the integer to a string and checking its length is a quick way to confirm it has exactly three digits, equivalent to checking 100 <= num <= 999.

What happens if pop_fruit() is called on an empty stack?

It prints 'UNDERFLOW' instead of raising an error, because the function explicitly checks 'if FruitStack == []' before calling pop().