Class 12 CS Python Stack Practice Questions
1. Suppose L = [3, 4, 5, 20, 4, 5], What is L after L.pop()?
1. [3,4,5,20,4]
2. [3,5,20,4,5]
3. [3,5,20,5]
4. Error
2. A School has created a dictionary containing top players and their runs as
key value pairs of cricket team. Write a program with separate user defined
functions to perform the following operations:
(a) Push the name of the players(Keys) of the dictionary into a stack, where
the corresponding runs (value) is greater than 49.
(b) Pop and display the content of the stack.
For Example
If dictionary has the following values:
Data={'Rohan':40, 'Rihaan':55, 'Tejas':80,'Ajay':90}
The output should be:
Ajay
Tejas
Rihaan
3. Write a program in Python, with separate user defined functions to
perform the following operations on Stack 'City'.
(a) - Push the pin code and name of the city in the stack 'City'
(b) - Display the latest added element in the stack 'City'
4. In a stack, if a user tries to remove an element from empty stack, it is
called --------
1. Empty Collection
2. Overflow
3. Underflow
4. None of these
5 - Priyanka has created a dictionary 'emeployee_data' containing EmpCode
and Salary as key value pairs for 5 Employees of Cyber Intratech. Write a
program, With separate user defined function, as mentioned below, to
perform the following operations:
(a) push_emp(): Push all those EmpCode, where the Salary is less than
25000, from the dictionary into a stack 'stk_emp'
(b) pop_emp(): Remove all the elements from the stack, one at a time, in a
Last-In-First-Out(LIFO) manner and displays them. It also displays 'Stack is
empty' once all the element have been removed.
For Example:
If the sample content of the dictionary is as follows:
{'E001':15000,'E002':27000,'E003':30000,'E004':15000,'E005':19000}, then
the stack 'stk_emp' will contain EmpCode E001, E004, E005 after
push_emp(). pop_emp() will pop and display employee record in LIFO fashion
and display 'Stack is empty' at last.
6 - A school stores records of Class XII students using a list that contains
multiple lists as its elements. The structure of each such element is
[Student_Name, Marks, MainSubject]. Crate user-defined functions to
perform the operations as mentioned below:
(a) Push_student(): To push the Student_Name and Marks of all those
students, who have Science as MainSubject, into a Stack StudentInfo
(b) Pop_student(): To delete all items (one at a time) from the stack
StudentInfo in LIFO order and display them. Also display "Empty Stack" when
there are no items remaining in the stack.
For Example:
If the stored information is:
[['Akansha',98,"Mathematics"],["Priti",96,"Science"],["Garima",99,"Science"],
["Ayushi",78,"English"]]
The stack should contain:
["Garima",99]
["Priti",96]
The output should be:
["Garima",99]
["Priti",96]
Empty Stack
7 - "Stack is a linear data structure which follows a particular order in which
the operations are performed"
What is the order in which the operations ae performed in a Stack?
Name the List method/function available in Python which is used to remove
the last element from a list implemented stack.
Also write an example using Python statements for removing the last
element of the list.
8 - Write a program in Python to input 5 words and push them one by one
into a list named All.
The program should then use the function PushNV() to create a stack of
words in the list NoVowel so that it store only those words which do not have
any vowel present in it, from the list All.
Thereafter, pop each word from the list NoVowel and display the popped
word. When the stack is empty display the message 'EmptyStack'.
For Example:
If the words accepted and pushed into the list All are
['DRY','LIKE','RHYTHM','WORK','GYM']
Then the stack NoVowel should store
['DRY','RHYTHM','GYM']
And the output should be displayed as
GYM RHYTHM DRY EmptyStack
9 - Write a program in Python to input 5 integers into a list named NUM.
The program should then use the function Push3_5() to push all those
integers which are divisible by 3 or divisible by 5 from the list NUM into the
stack of the list Only3_5.
Thereafter pop each integer from the list Only3_5 and display the popped
value. When the list is empty, display the message "StackEmpty".
For Example:
If the integers input into the list NUM are:
[10,6,14,18,30]
Then the stack Only3_5 should store
[10,6,18,30]
And the output should be displayed as
30 18 6 10 StackEmpty
10 - Differentiate between Push and Pop operations in the context of stacks.
11 - A dictionary, d_city contains the records in the following format:
{state:city}
Define the following functions with the given specifications:
(a) push_city(d_city): It takes the dictionary as an argument and pushes all
the cities in the stack CITY whose states are of more than 4 characters.
(b) pop_city(): This function pops the cities and displays "Stack empty" when
there are no more cities in the stack.
12 - Consider a list named Nums which contains random integers.
Write the following user defined functions in Python and perform the
specified operations on a stack named BigNums.
(a) PushBig(): It checks every number from the list Nums and pushes all such
numbers which have 5 or more digits into the stack BigNums.
(b) PopBig(): It pops the numbers from the stack, BigNums and displays
them. The function should also display "Stack Empty" when there are no
more numbers left in the stack.
For example:
If the list Nums contains the following data:
Nums = [213, 10025, 167, 254923, 14, 1297653, 31498, 386, 92765]
Then on execution of PushBig(), the stack BigNums should store:
[10025, 254923, 1297653, 31498, 92765]
And on execution of PopBig(), the following output should be displayed:
92765
31498
1297653
254923
10025
Stack Empty
13 - A list contains following record of course details for a University:
[Course_name, Fees, Duration]
Write the following user defined functions to perform given operations on the
stack named 'Univ':
(a) Push_element(): To push an object containing the Course_name, Fees,
and Duration of a course, which has fee greater than 100000 to the stack.
(b) Pop_element(): To pop the object from the stack and display it. Also,
display "Underflow" when there is no element in the stack.
For Example:
If the lists of courses details are:
["MCA", 200000, 3]
["MBA", 500000, 2]
["BA", 100000, 3]
The stack should contain:
["MCA", 200000, 3]
["MBA", 500000, 2]
14 - Write separate user defined functions for the following:
(a) PUSH(N) : This function accepts a list of names, N as parameter. It then
pushes only those names in the stack named OnlyA which contain the letter
'A'
(b) POPA(OnlyA) : This function pops each name from the stack OnlyA and
displays it. When the stack is empty, the message "EMPTY" is displayed.
For Example:
If the names in the list N are
['ANKITA','NITISH','ANWAR','DIPLE','HARKIRAT']
Then the stack OnlyA should store
['ANKITA','ANWAR','HARKIRAT']
And the output should be displayed as
HARKIRAT ANWAR ANKITA EMPTY
15 - Write the following user defined functions:
(a) pushEven(N) : This function accepts a list of integers named N as
parameter. It then pushes only even numbers into the stack named EVEN.
(b) popEven(EVEN) : This function pops each integer from the stack EVEN
and displays the popped value. When the stack is empty, the message
"Stack Empty" is displayed.
For Example:
If the list N contains
[10,5,3,8,15,4]
Then the stack, EVEN should store
[10,8,4]
And the output should be
4 8 10 Stack Empty
16 - A list contains following record of customer:
[Customer_name, Room_Type]
Write the following user defined function to perform given operations on the
stack named 'Hotel':
(a) Push_Cust(): To push customers' names of those who are staying in
'Delux' Room Type.
(b) Pop_Cust(): To pop the names of customers from the stack and display
them. Also display "Underflow" when there are no customers in the stack.
For Example:
If the lists with customer details are as follows:
["Aman","Delux"]
["Rahul","Standard"]
["Jerry","Delux"]
The stack should contain
Jerry
Aman
The output should be
Jerry
Aman
Underflow
17 - Write a function in Python, Push(Vehicle) where, Vehicle is a dictionary
containing details of vehicles - {Car_Name: Maker}
The function should push the name of car manufactured by 'TATA'(including
all the possible cases like TaTa, Tata, etc.) to the stack.
For Example:
If the dictionary contains the following data:
Vehicle={"Santro":"Hyundai","Nexon":"TATA", "Safari":"Tata"}
The stack should contain
Safari
Nexon
18 - Given Dictionary Stu_dict containing marks of students for three test
series in the form Stu_ID:(TS1,TS2,TS3) as key-value pairs.
Write a Python program with the following user defined functions to perform
the specified operations on a stack named Stu_Stk
(a) Push_elements(Stu_Stk, Stu_dict): It allows pushing IDs of those students,
from the dictionary Stu_dict into the stack Stu_Stk, Who have scored more
than or equal to 80 marks in the TS3 Test.
(b) Pop_elements(Stu_Stk): It removes all elements present inside the stack
in LIFO order and prints them. Also, the function displays 'Stack Empty' when
there are no elements in the stack.
Call both functions to execute queries.
For example:
If the dictionary Stu_dict contains the following data:
Stu_dict={5:(87,68,89), 10:(57,54,61),12:(71,67,90), 14: (66,81,80), 18:
(80,48,91)}
After executing Push_elements(), Stk_ID should contain [5, 12, 14, 18]
After executing Pop_elements(), the output should be:
18
14
12
Stack Empty
19 - A list contains following record of a customer:
[Customer_name, Phone_number, City]
Write the following user defined functions to perform given operations on the
stack named 'status':
(a) Push_element(): To push an object containing name and phone number of
customers who live in Goa to the stack.
(b) Pop_element(): To pop the objects from the stack and display them. Also,
display "Stack Empty" when there are no elements in the stack.
20 - Write a function in Python(SItem) where, SItem is a dictionary containing
the details of stationary items - {Sname:Price}.
The function should push the names of those items in the stack who have
price greater than 75. Also display the count of elements pushed into the
stack.
For example:
If the dictionary contains the following data:
Ditem={"Pen":106,"Pencil":59,"Notebook":80,"Eraser":25}
The stack should contain
Notebook
Pen
The output should be
The count of elements in the stack is 2
21 - A list, NList contains following record as list elements:
[City, Country, distance from Delhi]
Each of these records are nested together to form a nested list. Write the
following user defined functions in Pythons to perform the specified
operations on the stack named travel.
(a) Push_element(NList): It takes the nested list as an argument and pushes
a list object containing name of the city and country, which are not in India
and distance is less than 3500 km from Delhi.
(b) Pop_element(): It pops the objects from the stack and displays them.
Also, the function should display "Stack Empty: when there are no elements
in the stack.
For example:
If the nested list contains the following data:
NList=[["New York", "USA",11734],["Naypyidaw","Myanmar", 3219],
["Dubai","UAE",2194],["London","England",6693],["Gangtok","India",1580],
["Columbo","Sri Lanka",3405]]
The stack should contain:
["Naypyidaw","Myanmar"]
["Dubai","UAE"]
["Columbo","Sri Lanka"]
The output should be:
["Columbo","Sri Lanka"]
["Dubai","UAE"]
["Naypyidaw","Myanmar"]
Stack Empty
Objective type questions of Data Structure stack Class 12 Questions contain
MCQs, 1-word answer questions, Fill in the blanks or true false.
[1] A ____________ is a way to store, organize, or manage data in efficient and
productive manner.
Data Structure
[2] A stack is one of the following types of data structure?
a) Linear b) Dynamic c) Circular d) All of these
[3] Stack data structure is following ____________ principle.
LIFO
[4] In stack data can be inserted or deleted from ____________ only.
Top
[5] The insert operation in the stack is known as pop. (True/False)
[6] You can replace any element position in the stack. (True/False)
[7] The peek operation refers to accessing/inspecting the top element in the
stack. (True/False)
[8] A condition raise due to the stack is full is known as ___________.
a) Underflow b) Overflow c) List is full d) Completely Filled
[9] While popping the element from the stack, a condition will be raised, this
condition is known as ____________.
a) Underflow b) Overflow c) List is Empty d) Blank List
[10] Stack overflow condition is raised in ____________ operation where as
Stack underflow condition is raised in _____________ operations.
Push, Pop
In the next section, you will get short answer questions or case study based
Data Structure stack Class 12 Questions. You can find the answer at the
following link.
Recommended: Stack using List
Short answer questions (2 Marks – Questions) – Data Structure stack
Class 12 Questions
So here we start the next section of Data Structure stack Class 12 Questions
for 2 marks questions. Here we go!
[1] What do you mean by data structure? Explain your answer with a
suitable example.
”Show_Answer”
[2] What do you mean by the LIFO structure? Support your answer
with real-life examples.
”Show_Answer”
[3] Enlist a few of the fields where you feel a stack is used in real
life.
”Show_Answer”
[4] What are the basic operations that can be performed on the
stack?
”Show_Answer”
[5] What are the underflow and overflow conditions?
”Show_Answer”
[6] Write steps on how you implement stack?
”Show_Answer”
[7] Write a python function named is_underflow() to check a stack is
an underflow.
def is_underflow(stk):
if stk==[]:
return True
else:
return False
[8] Write a function to push an element into the stack.
def push(stk,e):
stk.append(e)
top = len(stk)-1
[9] Write a python function to delete an element from the stack.
def pop_stack(stk):
if stk==[]:
return "UnderFlow"
else:
e = stk.pop()
if len(stk)==0:
top = None
else:
top = len(stk)-1
return e
[10] Write a function to display the stack elements.
def display(stk):
if stk==[]:
print("Stack is Empty")
else:
top = len(stk)-1
print(stk[top],"-Top")
for i in range(top-1,-1,-1):
print(stk[i])
[11] Write a function to inspect an element from the stack.
def peek(stk):
if stk==[]:
return "UnderFlow"
else:
top = len(stk)-1
return stk[top]
[12] Write functions AddPlayer(player) and DeletePlayer(player) in
python to add and remove a player by considering them as push and
pop operations in a stack.
def AddPlayer(player):
pn=input("enter player name:")
player.append(pn)
def DeletePlayer(player):
if player==[]:
print("No player found")
else:
return player.pop()
[13] Vedika has created a dictionary containing names and marks as
key-value pairs of 5 students. Write a program, with separate user-
defined functions to perform the following operations:
1. Push the keys (name of the student) of the dictionary into a
stack, where the corresponding value (marks) is greater than
70.
2. Pop and display the content of the stack.
The dictionary should be as follows:
d={“Ramesh”:58, “Umesh”:78, “Vishal”:90, “Khushi”:60, “Ishika”:95}
Then the output will be: Umesh Vishal Ishika
def push(stk,item):
stk.append(item)
def Pop(stk):
if stk==[]:
return None
else:
return stk.pop()
stk=[]
d={"Ramesh":58, "Umesh":78, "Vishal":90, "Khushi":60, "Ishika":95}
for i in d:
if d[i]>70:
push(stk,i)
while True:
if stk!=[]:
print(Pop(stk),end=" ")
else:
break
Note:
https://anjeevsinghacademy.com/stack-5-questions-5-marks-class-12-
computer-science-code-083/