Test Index
CBSE Class 12 Computer Science 2026 Paper Set 4
© examsnet.com
Question : 30 of 37
Marks:
+1,
-0
(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 :
{'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, 218and the output of the program should be :
218 220 924 320
Solution:
(a)Concept: A stack is a LIFO (Last In, First Out) structure. With a Python list, the end of the list is the top:
append() pushes and pop() removes the top element.(a)(i)def push_fruit(FruitStack, Fruit): if Fruit['Price'] < 100: FruitStack.append(Fruit)(a)(ii)
def pop_fruit(FruitStack): if FruitStack == []: print("UNDERFLOW") else: return FruitStack.pop()(a)(iii)def display(FruitStack): if FruitStack == []: print("EMPTY STACK") else: for i in range(len(FruitStack) - 1, -1, -1): print(FruitStack[i])Explanation:(i) Only a record whose Price is less than 100 is pushed; the sample Apple record (Price 120) is not pushed.(ii) An empty list means there is nothing to pop, so UNDERFLOW is shown; otherwise pop() removes and returns the last (topmost) record.(iii) The loop runs from the last index down to 0, so the topmost record is printed first. (for rec in FruitStack[::-1]: works the same way.)OR (b)Concept: A number is a three-digit even integer when it lies from 100 to 999 and leaves remainder 0 on division by 2. Push such numbers, then pop until the stack is empty; LIFO order reverses them.stack = []for i in range(10): num = int(input("Enter an integer: ")) if 100 <= num <= 999 and num % 2 == 0: stack.append(num)while stack: print(stack.pop(), end=' ')Explanation for the sample input:12, 31, 6, 92 and 1 are not three-digit numbers; 457 is three-digit but odd. So the pushes are 320, 924, 220, 218 and the stack is [320, 924, 220, 218] with 218 on top.Popping gives 218, then 220, then 924, then 320, so the output is:218 220 924 320
© examsnet.com
Go to Question: