Anjeev Singh Academy

30+ Important Questions Stack in Python Class 12 Computer Science Code 083

30 Important Questions Stack in Python Class 12 Computer Science Code 083

I tried my best to select the most important categories of questions for the class 12 Computer Science 083 board examination. In the Class 12 Computer Science 083 syllabus, you have to learn about Python Data Structure Stack. Here I am providing you most important questions based on Board Examination. According to latest curriculum of Computer Science, the weightage of Stack questions in CBSE Board examination is 3 to 5 Marks. They can be asked the questions of different categories like – definition/concepts (learning) based (MCQ’s, Fill ups, True False) and programming based question of 3 marks on a stack.


Concept Based Questions – Theory


Question 1: What is Data Structure?

Answer: Data structure is a way of storing and organising information in the computer. It has well-defined operations, properties and behaviours.

For Examples- Stack, Queue, Linked List, Array, List, etc.

Question 2: What do you mean by Stack? What is the significance of TOP in the stack? Write two characteristics of Stack?

Answer: Stack – a stack is an abstract data type and a linear or user-defined data structure based on the principle of Last In First Out (LIFO).

Significance of Top: A stack is a list where insertion and deletion can take place only at one end called Top.

Characteristics of Stacks:

  • It is a LIFO data structure.
  • The insertion and deletion happen at one end i.e. from the top of the stack.

Question 3: What are the operations on the stack? Define each.

Answer: Operations on the Stack are –

  • (a) push – to insert an element in the stack at the top position
  • (b) pop – to delete an element from the stack from the top position
  • (c) peek – to get the topmost element without deletion.
  • (d) traversal – to access all the elements from the stack, from top to bottom.

Question 4: What are the applications of the stack?

Answer: Applications of Stack are:

(a) Infix to postfix conversion using stack.

(b) Evaluation of postfix expression.

(c) Reverse a string using a stack.

(d) Implement two stacks in an array.

Question 5: What do you mean by Underflow and Overflow?

Answer: UNDERFLOW – In stack, the UNDERFLOW condition occurs, when the stack is empty and we try to delete an element from the stack.

OVERFLOW – In stack, the OVERFLOW condition occurs, when the stack is full, and we try to insert an element into the stack.


Programming/Coding Based Questions – Data Structure Stack in Python [ 3 Marks]


Question 1: Stack Question – CBSE Board Examination 2025 with Answer

A stack, named ClrStack, contains records of some colors. Each record is represented as a tuple containing four elements – ColorName, Red, Green, Blue. ColorName is a String, and Red, Green, Blue are integers. For example, a record in the stack may be (‘Yellow’, 237, 250, 68). [CBSE Main 2025]

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

  • (i) push_Clr (ClrStack, new_Clr): This function takes the stack ClrStack and a new record new_Clr as arguments and pushes this new record onto the stack.
  • (ii) pop_Clr(ClrStack): This function takes the stack ClrStack and a new record new_Clr as arguments and pushes this new record onto the stack.
  • (iii) isEmpty(ClrStack): This function checks whether the stack is empty. If the stack is empty, the function should return True, otherwise the function should return False.

Answer:

ClrStack = [ ]

#(i)

def push_Clr(ClrStack, new_Clr):
	ClrStack.append(new_Clr)


#(ii) 
def pop_Clr(ClrStack):
	If ClrStack == [ ]:
		print(“Underflow”)
	else:
		return ClrStack.pop( )

# (iii)
def isEmplty(ClrStack):
	if ClrStack == [ ]:
		return True
	else:
		return False;


Question 2: Stack Question – CBSE Board Examination 2025 with Answer

Write the following user-defined functions in Python:

  • (i) push_trail( N, myStack): Here N and myStack are list, and myStack represents a stack. The function should push the last 5 elements from the list N onto the stack myStack. For example, if the list N is [1, 2, 3, 4, 5, 6, 7], then the function push_trail( ) should push the elements 3, 4, 5, 6, 7 onto the stack. Therefore the value of stack will be [3, 4. 5, 6, 7].

    Assume that N contains at least 5 elements.
  • (ii) pop_one(myStack): The function should pop an element from the stack myStack, and return this element. If the stack is empty, then the function should display the message ‘Stack Underflow’ and return None.
  • (iii) display_all(myStack): The function should display all the elements of the stack myStack, without deleting them. If the stack is empty, the function should display the message ‘Empty Stack’.

Answer:

#Last five elements

myStack = [ ]   #Empty Stack

#(i) 
def push_trail(N, myStack):
	if len(N) > = 5:                             #to check the N have at least 5 elements.
		for i in range(-5, 0, 1 ):
			myStack.append(N[i])

# (ii)
def pop_one(myStack):
	if myStack != [ ]:
		return myStack.pop( )
	else:
		print(“Underflow”)
		Return None

# (iii)
def display_all(myStack):
	if myStack == [ ]:
		print(“Empty Stack”)
	else:
		for v in myStack:
			print(v)


Question 3: Stack Question – CBSE Board Comptt./Supplementary Examination 2024 with Answer

A dictionary, d_city contains the records in the following format: {state:city}
Define the following functions with the given specifications:

  • (i) 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.
  • (ii) pop_city(): This function pops the cities and displays “Stack empty” when there are no more cities in the stack.

Answer:

CITY = [ ]

# (i) push_city(d_city)

def push_city(d_city) :
    for key in d_city:
        if len(key) > 4 : 
            CITY.append(d_city[key])

# (ii) pop_city()

def pop_city( ):
   while CITY:
        print(CITY.pop( )
   print("Stack Empty")


Question 4: Stack Question – CBSE Board Examination 2024 with Answer

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. [CBSE 2024]

  • (i) PushBig(): It checks every number from the list Nums and pushes all such numbers which have 5 or more digits into the stack, BigNums.
  • (ii) 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 of 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
    StackEmpty

Answer:

# (i) PushBig():

def  PushBig(Nums, BigNums):
    for N in Nums:
        if len(str(N)) > = 5:
            BigNums.append(N)

#OR

def PushBig(Nums, BigNums):
    for N in Nums:
        if N > = 10000:
              BigNums.append(N)

#( ii) PopBig( )

def PopBig(BigNums):
    while BigNums != [ ]  :  # while BigNums
          print(BigNums.pop( ))
    else:
          print("Stack Empty)

Question 5: Stack Question – CBSE Board Examination 2023 with Answer

A list contains following record of customer : [Customer_name, Room Type]
Write the following user defined functions to perform given operations on the stack named ‘Hotel’ :

  • (i) Push_Cust() – To Push customers’ names of those customers who are staying in ‘Delux’ Room Type.
  • (ii) 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 :
[“Siddarth”, “Delux”]
[“Rahul”, “Standard”]
[“Jerry”, “Delux”]
The stack should contain
Jerry
Siddharth
The output should be:
Jerry
Siddharth
Underflow

Answer:

Hotel=[] 
Customer=[["Siddarth","Delux"],["Rahul","Standard"],["Jerry", "Delux"]] 
def Push_Cust(): 
    for rec in Customer: 
        if rec[1]=="Delux": 
            Hotel.append(rec[0]) 
 
def Pop_Cust(): 
    while len(Hotel)>0: 
        print(Hotel.pop()) 
    else: 
        print("Underflow") 

Question 6: Stack in Python Question Answer

Ajay has a list containing integers. You need to help him create a program with separate user-defined functions to perform the following operations based on this list.

  • Traverse the content of the list and push all positive numbers into a stack.
  • Pop and display the content of the stack.

For Example:
If the sample Content of the list is as follows: N=[-2, 131,-34, 56, 21, -379, 98, -22, 35, 38]
Sample Output of the code should be: 38 35 98 21 56 131

Answer:

Output:


Question 7: Stack in Python Question Answer

Sunil has a list containing 10 integers. You need to help him create a program with separate user-defined functions to perform the following operations based on this list.

● Traverse the content of the list and push the even numbers into a stack.
● Pop and display the content of the stack.

For Example:
If the sample Content of the list is as follows: N=[12, 13, 34, 56, 21, 79, 98, 22, 35, 38]
Sample Output of the code should be: 38 22 98 56 34 12

Answer:

Output:


Question 8: Stack in Python Question Answer

Anjeev has a message (string) that has an upper case alphabet in it. Write a program, with separate user-defined functions to perform the following operations:

  • Push the upper case alphabets in the string into a stack.
  • Pop and display the content of the stack.

For example:
If the message is “All the Best, for your Best Performance”

The output from the program should be: P B B A

Answer:

Output:


Question 9: Stack in Python Question Answer

Shalu has created a dictionary containing names and marks as key-value pairs of 6 students. Write a program, with separate user-defined functions to perform the following operations:

  • Push the keys (name of the student) of the dictionary into a stack, where the corresponding value (marks) is greater than 75.
  • Pop and display the content of the stack.

For example:
If the sample content of the dictionary is as follows:
R = {“OM”:76, “JAI”:45, “BOB”:89, “ALI”:65, “ANU”:90, “TOM”:82}

The output from the program should be:
TOM ANU BOB OM

Answer:

Output:


Sorry! You cannot copy content of this page. Please contact, in case you want this content.

Scroll to Top