CHAPTER 5 – INTRODUCTION TO STACK PRACTICE QUESTIONS 1. 2. 3. 4. 5. 1. 2. 3. 4. 5. ASSERTION & REASONING A: The start index of a stack is -1 R: Stack Data structure can be implemented through list in Python 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 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 . A: Stack is a linear data structure. R: A data structure in which elements are organized in a sequence is called linear data structure. 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 respectively. OBJECTIVE TYPE MCQ 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 Which of the following data type/data structure can be used to implement the stack in Python? (a)integer (b)tuple (c)list (d) sets Stack data structure works on the following principle. (a) LILO (b)DIFO (c) FIFO (d) LIFO 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 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 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. Traditionally the end of a stack from where PUSH and POP take place, popularly known as: ______ (a) Front (b) Top (c) LIFO (d) FIFO 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 Inspecting the value at the stack‟s top without removing it. (a) peak operation (b) insert operation c) pop operation (d) push operation 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 Which of the following statement is not correct in respect of Stack? (a) Stack is a linear data structure.(b) Stack is ordered. (c) Stack does not follow LIFO rule (d) None of the above. What is the process of inserting data into a stack called? (a)Create(b)Insert(c) Push(d) Evaluate What is the process of deleting data from a stack called? (a)Drop(b)Delete (c) Erase(d) Pop Which data structure works on the principle of LIFO? (a) Linear List (b) Stack (c) Queue (d) Tree Which of the following operation is used to remove the top most element of the stack? (a)PUSH (b) DISPLAY (c) POP (d) INSERT Which pointer is associated with a stack? (a) First (b) Front (c) Rear (d) Top 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 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 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 Which is the correct way to create a stack of numbers with contents 8,10 and 100, in Python? (a) Stack=(8,10,100) (b) Stack =[8,10,100] (c) Stack( 8,10,100) (d) None of the above 19. 20. 21. 22. 23. While trying to delete an element from an empty stack which of the following exception results? (a) Underflow (b) Common flow (c) Down flow (d) Overflow 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 Assume a stack Stk implemented as a list. Predict the output of the following code. defpush_char(ch): Stk.append(ch) defdisplay_stk(): strdata="" if len(Stk)==0: print("Empty Stack") else: for i in Stk: strdata+=i print(strdata[::-1]) push_char('c') push_char('b') push_char('s') push_char('e') push_char('2') push_char('0') push_char('2') push_char('1') (a) 1202esbc (b) cbse2021 (c) 1 (d) s What will the output of the following Python code? result=0 numberList=[10,20,30] numberList.append(40) result=result+numberList.pop() result=result+numberList.pop() print(result) (a)40 (b) 70 (c)30 (d) 10 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) empty list 24. 25. 26. 27. 28. Which of these is not an application of stack? (a) Parenthesis Balancing program (b)Evaluating Arithmetic Expressions (c) Reversing Data (d) Data Transfer between Process Predict the output of the following code: def insert_data(stk,num): stk.append(num) stk=[2,3,4] insert_data(stk,10) print(stk) (a) [2,3,4,10] (b) [10,2,3,4] (c) Overflow (d) Underflow Predict the output of the following code: def push(stk,num): stk.append(num) def pop(): (a) 200(b) 100 if len(stk)==0: print("Underflow") (c) -1(d) 4 else: stk.pop() stk=[10,100,-1,2,4] push(stk,200) pop() pop() pop() push(stk,4) print(stk[-1]) The contents of a stack st are as shown in the figure. Mention the value of Top after a series of these operations: Push(st,9) Pop() Pop() Pop() Push(st,-1) (a) 1(b) 2(c) 3(d) 4 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 29. Anu is assigned a stack program which has a function push_data() to push elements into a stack which is divisible by 2. The program also has a function pop_data() which will delete elements from a stack. 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. 30. Sohan is developing a program in Computer Lab of his school to perform some operations in Stack. He has created push(Student_Name), stack_Delete(Student_Name) and display(Student_List) functions in Python to add a new Student name, delete a Student name and print list of student from a stack, considering them to function/work as insert, delete and print operations of the Stack data structure. He is not getting the desired result. Help him to get the desired result from the given python code. Student_List = list( ) def push(Name): #Push an element into the stack Student_List._____(Name) #Statement 1 def pop(): if __________ : #Statement 2 return ('Underflow') else: return Student_List.________ ( ) #Statement 3 def display(): for st_Name in _____________: #Statement 4 ________ (st_Name) #Statement 5 Fill the above statement based on given questions: 1. 2. 3. 4. 5. Identify the suitable code for the blank of statement1 Fill the statement 2, to check the stack is empty. Fill the statement 3, to delete an element from the stack. Identify the suitable code for the blank of statement 4. Identify the suitable code for the blank of statement 5. 3- MARKS 1. 2. 3. 4. 5. Write a function in python, Push(Stu) and MakePop(Stu) to add a new student and delete studentfrom a List of Stu contain rollno, Sname and Class as list, considering them to act as push andpop operations of the Stack data structure Write a function in python, Push(Package) and Pop(Package) to add details ofemployee contain information (Empid, Ename and Salary) in the form of tuple inPackage and delete a Package from a List of PackageDescription, considering them toact as push and pop operations of the Stack data structure. Jiya has a list containing 8 integers. You need to help her create a program with twouser 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 aredivisible 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 A list contains following record of a doctor: [Doc_ID, Doc_name, Phone_number, Speciality] Write the following user defined functions to perform given operations onthe 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. Write a function in Python, Push(KItem), where KItem is a dictionarycontaining 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 thestack. 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 6. 7. 8. 9. Mr.Ajay has created a list of elements. Help him to write a program in python with functions,PushEl(element) and PopEl(element) to add a new element and delete an element from a List ofelement Description, considering them to act as push and pop operations of the Stack data structure. Push the element into the stack only when the element is divisible by 4. For eg: if L=[2,5,6,8,24,32] then stack content will be 32 24 8 A dictionary contains records of a tourist place like : Tour_dict = {'Name' : 'Goa', 'PeakSeason' : 'December', 'Budget' : 15000 ,'Famous' : 'Beaches'} Write the following user defined functions to perform given operations on the stack named„tour‟: (i) Push_tour(Tour_dict) – To Push a list containing values for Name and PeakSeason where value of budget is less than 10000 into the tour stack. (ii) Pop_tour() – To Pop the list objects from the stack and display them. Also, display “Nomore tours” when there are no elements in the stack. Write a function in Python, Push(stack, SItem) where , SItem is a List containing the detailsof 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. Muthu has created a dictionary containing names and marks of computer science as key, value pairs of 5 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 (CS marks) are more than or equal to 90 . ● Pop and display the content of the stack, if the stack is empty display the message as “UNDER FLOW” For example: If the sample content of the dictionary is as follows: CS={"Raj":80, "Anu":91, "Vishwa":95, "Moni":80, “Govind":90} The push operatio‟s output from the program should be: Anu Vishwa Govind The pop operation must display Govind Vishwa Anu “UNDER FLOW” 10. 11. 12. Sakthi have a list of 10 numbers. 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 numbers divisible by 5 into the stack. ● Pop and display the content of the stack, if the stack is empty display the message” STACK EMPTY” Two list Lname and Lage contains 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‟, „Piyush‟] 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 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} **********************************************************************