Class 12 - Stacks in Python - Computer Science (Question and Answers)

Q1: What is a data structure?

Answer: A data structure defines a mechanism to store, organise and access data along with operations (processing) that can be efficiently performed on the data.

Q2: What are the two main types of data structures?

1. Primitive Data Structure

2. Non-Primitive Data Structure

Q3. Give examples of non-primitive data structures.

Answer: Stack, Queue, Array, Linked List, Binary Trees, Heaps, Graph, Sparse Matrix etc.

Q4: What is a stack?

Answer: A stack is an ordered list in which insertion and deletion are done at one end, called the top. The last element inserted is the first one to be deleted. Hence, it is called the Last in First out (LIFO) or First in Last out (FILO) list.

Q5: What is a linear data structure? Give an example.

Answer: A data structure in which elements are organised in a sequence is called a linear data structure. e.g. stack, queue.

Q6: Give some examples of a stack in real life.

Answer: Some of the applications of a stack in real-life are:

• Pile of clothes in an almirah

• Multiple chairs in a vertical pile

• Bangles are worn on the wrist

• Pile of boxes of eatables in a pantry or on a kitchen shelf

Q7: Give examples of the application of stack in programming.

• Reversing a string

• a compiler or an interpreter handle function calls in a program

• In an application, redo/undo steps of actions.

• Solving numerical expression. (Infix to Postfix conversion)

• Browsing web: Go Back and Go Forward link chain.

Q8: Name the two fundamental operations performed on the stack.

push (data): Inserts data onto the stack.

pop(): Removes and returns the last inserted element from the stack.

Q9: What are auxiliary stack operations?

top(): Returns the last inserted element without removing it.

size(): Returns the number of elements stored in the stack.

isEmpty(): Indicates whether any elements are stored in the stack or not.

isFull(): Indicates whether the stack is full or not.

Q10: Any attempt to insert a new element in an already full stack shows what kind of error?

Q11: In a stack, if a user tries to remove an element from an empty stack, which error is thrown?

Q12: Write a function pop() that remove a name from the stack named "MyStack".

def Pop(MyStack):

if len(MyStack) > 0:

MyStack.pop()

else:

print("Underflow: Stack is empty.")

Q13: Write a function Push() which takes a number as an argument and add in a stack "MyValue".

MyValue=[]

def Push(value):

MyValue.append(value)

Q14: When evaluating any Postfix expression using Stack, what elements are PUSHed onto it?