Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Stack

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

CHAPTER 5 – INTRODUCTION TO STACK

PRACTICE QUESTIONS

THEORY QUESTIONS
1. What are Data structures?
2. List out the various types of Data structure.
3. Differentiate between primitive and non-primitive data structres.
4. Define Stack.
5. Write the applications of stack.
6. Differentiate between Push and Pop operations in the context of
stacks.
7. At what situation Underflow occurs in Stack?
8. Is Overflow possible in Python in the context of stack?
9. What is meant by peek operation?

ASSERTION & REASONING


1. A: The start index of a stack is -1
R: Stack Data structure can be implemented through list
in Python
2. A: If the size of a stack is 5 and a sixth element is inserted,
then Underflow happens
R: Overflow is a condition which occurs in bounded stack
3. A: A stack has 2 elements in it. If the stack undergoes 3
pop operations one after another then Underflow occurs.
R: Underflow is a condition which occurs when deletion
happens on an empty stack.
4. A: Stack is a linear data structure.
R: A data structure in which elements are organized in a
sequence is called linear data structure.
5. A: Stack is a linear data structure that works on the
principle of FIFO(First In First OUT)
R: The stack is created with the help of a list with some
restrictions. It manages a pointer called stack
pointer (SP) that will increase or decrease by 1, if an
element is entered or removed from the stack.
OBJECTIVE TYPE MCQ
1. What is the name of a data structure in which insertion and
deletion is done from one end only, usually referred to as TOP.
(a) QUEUE (b) STACK (c) LISTS (d) DICTIONARY
2. Stack data structure works on the following principle.
(a) LILO (b)DIFO (c) FIFO (d) LIFO
3. Expand the following term: LIFO
(a) Least in First Out (b) Last in First Out
(c) Last in Fast Out (d) Least in Fast Out
4. Which of the following is not an application of stack in real-life?
(a) Pile of clothes in an almirah
(b) Multiple chairs in a vertical pile
(c) Persons buying movie tickets from a counter
(d) Bangles worn on wrist
5. Traditionally the end of a stack from where PUSH and POP take
place, popularly known as:
(a) Front (b) Top (c) LIFO (d) FIFO
6. Which of the following are two basic operations performed on a
stack for insertion and deletion of elements?
(a) INSERT and DELETE (b) ADD and REMOVE
(c) PUSH and POP (d) APPEND and DELETE
7. Inspecting the value at the stack’s top without removing it. (a)
peak operation (b) insert operation
c) pop operation (d) push operation
8. Stacks serve major role in
(a) Simulation of recursion
(b) Simulation of linked list
(c) Simulation of limited resource allocation
(d) Simulation of all data
9. What is the process of inserting data into a stack called?
(a)Create(b)Insert(c) Push(d) Evaluate
10. What is the process of deleting data from a stack called?
(a)Drop (b) Delete (c) Erase (d) Pop
11. Which of the following exception results when we try to add an
element to a full stack?
(a) Underflow (b) Common flow (c) Down flow(d) Overflow
12. Which pointer is associated with a stack?
(a) First (b) Front (c) Rear (d) Top
13. Assume a stack has size 4. If a user tries to push a fifth element
to a stack, which of the mentioned condition will arise?
(a) Underflow (b) Overflow (c)Crash (d) Successful Insertion
14. If a user tries to pop an empty stack, which of the mentioned
condition will arise?
(a)Empty data (b) Overflow (c) Underflow (d) No error
15. Which of the following condition is necessary for checking a stack
st is full in Python?
(a) Top ==len(st) (b) Top ==len(st)-1
(c) Top <len(st) (d) Top ==st
16. Predict the output of the following Python code.
V = list( )
V.append('Cha') (a)['Cha', 'Bala']
V.append('Bala') (b) ChaBala
V1 = V.pop()
V2 = V.pop() (c) 'Cha' 'Bala'
V3 = V.pop()
(d) IndexError: pop from
print(V1, V2, V3)

17. Choose correct output for the following sequence of


operations.push(5) push(8) pop push(2) push(5) pop poppop
push(1) pop
(a) 8 5 2 51 (b) 8 5 5 2 1 (c) 8 2 5 5 1 (d) 8 1 2 5 5
18. Anu is assigned a stack program which has a function
push_data() to push elements into a stack which is
divisible by 2. She has newly learnt Python Language and is
facing some difficulty in completing the code. Help her in
completing the code. Incomplete Code:
def push_data(list_all):
for i in range(0, ): # statement2
if i%2==0:
Stack. # statement 3
print(Stack) #statement 4

def pop_data():
if ==0: #statement5
print("Empty Stack,Cant pop")
return -1

else:
pop_item=Stack[-1]

Stack. #statement6
return pop_item

Stack= # statement1
push_data ([20,11,30,15,2])
print(pop_data())
1. Identify the missing code in statement 1.
2. Identify the missing code in statement 2 for completing
the stop parameter in the for loop.
3. Identify the missing function in statement 3 for inserting
Data into the stack.
4. What will be the contents of stack which will be printed
in statement 4?
5. Identify the missing code in statement 5 in
pop_data() function.
6. Identify the missing code in statement 6 for popping the
element in the stack.
3- MARKS
1. Write a function in Python PUSH(Arr), where Arr is a list of
numbers. From this list push all numbers divisible by 5 into a
stack implemented by using a list. Display the stack if it has at
least one element, otherwise display appropriate error message.
2. Write a function in Python POP(Arr), where Arr is a stack
implemented by a list of numbers. The function returns the value
deleted from the stack.
3. Alam 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
4. Jiya has a list containing 8 integers. You need to help her create
a program with two user defined functions to perform the
following operations based on this list.
• Traverse the content of the list and push those numbers
into a stack which is divisible by both 5 and 3.
• Pop and display the content of the stack.
For example: If the sample Content of the list is as follows:
L=[5,15,21,30,45,50,60,75] Sample Output of the code should
be: 75 60 45 30 15
5. Varun 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 the 2 digit
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 -22 98 21 56 34
6. Write the definition of a function POP_PUSH(LPop, LPush, N) in
Python.
The function should Pop out the last N elements of the list LPop
and Push them into the list LPush.
For example :
If the contents of the list LPop are [10, 15, 20, 30]
And value of N passed is 2,
then the function should create the list LPush as [30,20] And the
list LPop should now contain [10, 15]

NOTE : If the value of N is more than the number of elements


present in LPop, then display the message
"Pop not possible"
7. A list contains following record of a doctor:
[Doc_ID, Doc_name, Phone_number, Specialization]
Write the following user defined functions to perform given
operations on the stack named "status":
(i) Push_element() - To Push an object containing Doc_ID
and Doc_nameof doctors who
specialize in Anesthesia to the stack.
(ii) Pop_element() - To Pop the objects from the stack and
display them. Also, display ―Stack
Empty when there are no elements in
the stack.
9. Write a function in Python, Push(stack, SItem) where , SItem is
a List containing the details of stationary items in a format like –
[Name , price , Unit , Company , MRP ].
The function should push the company names of those items in
the stack whose price is 10% percent less than its MRP. Also
write a function print_stack( ) to display the Item Names and the
count of Items pushed into the stack.
10. Raja received a message(string) that has upper case and lower-
case alphabet. He want to extract all the upper case letters
separately .Help him to do his task by performing the following
user defined function in Python:
a) Push the upper case alphabets in the string into a
STACK
b) Pop and display the content of the stack.
For example:
If the message is “All the Best for your Pre-board
Examination”
The output should be : E P B A
11. A dictionary R contains the following records:
R={"OM":76, "JAI":45, "BOB":89, "ALI":65, "ANU":90,
"TOM":82}
Write the following user defined functions to perform given
operations on the stack named "STU_NAME"
(i) Push() – Push the keys (name of the student) of the
dictionary into a stack, where the
corresponding value (marks) is greater than
75.
(ii) Pop() – Pop and display the content of the stack.
12. Write a function in Python, Push(KItem), where KItem is a
dictionary containing the details of Kitchen items– {Item:price}.
The function should push the names of those items in a stack
which have priceless than 100. Also display the average price of
elements pushed into the stack.
For example: If the dictionary contains the following data:
{"Spoons":116,"Knife":50,"Plates":180,"Glass":60}
The stack should contain
Glass
Knife
The output should be: The average price of an item is
55.0
13. Two list Lname and Lage contain name of person and age of
person respectively. A list named Lnameage is empty. Write
functions as details given below
(i) Push_na() :- It will push the tuple containing pair
of name and age from Lname and Lage
whose age is above 50.
(ii) Pop_na() : It will remove the last pair of name and age
and also print name and age of removed
person. It should also print “underflow” if
There is nothing to remove.
For example the two lists has following data:
Lname=[‘narender’, ‘jaya’, ‘raju’, ‘ramesh’, ‘amit’,’anu’]
Lage=[45,23,59,34,51,43]
After Push_na() the contains of Lnameage stack is [(‘raju’,59),
(‘amit’,51)]
The output of first execution of pop_na() is
The name removed is amit
The age of person is 51
14. 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' :
(i) Push_element(): To push an object containing the
Course_name, Fees and Duration of a
course, which has fees greater than
100000 to the stack.
(ii) Pop_element(): To pop the object from the stack and
display it.

15. A dictionary stu contains rollno and marks of students. Two


empty list stack_roll and stack_mark will be used as stack.
Two function push_stu() and pop_stu() is defined and perfom
following operation
(a) Push_stu() :- It reads dictionary stu and add keys into
stack_roll and values into stack_marks for
all students who secured more than 60
marks.
(b) Pop_stu() :- It removes last rollno and marks from both
list and print "underflow" if there is
nothing to remove
For example:
stu={1:56,2:45,3:78,4:65,5:35,6:90}
values of stack_roll and stack_mark after push_stu()
[3,4,6] and {78,65,90}
16. Given a 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.
(i) 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.

(ii) 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
5
Stack Empty
*********************************************************************

You might also like