If the following operations are done on a stack, then what will be the…
2016
If the following operations are done on a stack, then what will be the sequence of institutions?
push(4). push(9). pop (). push (5) . pop (). pop ()
- A.
9, 5, 4
- B.
4, 9, 5
- C.
4, 5, 9
- D.
5, 9, 4
Attempted by 134 students.
Show answer & explanation
Correct answer: A
The correct option is A (9, 5, 4).
A Stack is a linear data structure that operates on the LIFO (Last In, First Out) principle. The element that is added last to the stack is always the first one to be removed (popped) from the top.
Let's trace the execution sequence step-by-step:
push(4): Element4is added to the stack.Stack state (Top to Bottom):
[4]
push(9): Element9is added on top of4.Stack state (Top to Bottom):
[9, 4]
pop(): The element at the top is removed. Since9is at the top, it is removed first.Output element: 9
Stack state (Top to Bottom):
[4]
push(5): Element5is added on top of4.Stack state (Top to Bottom):
[5, 4]
pop(): The element at the top is removed. Since5is at the top, it is removed.Output element: 5
Stack state (Top to Bottom):
[4]
pop(): The element at the top is removed.4is now removed.Output element: 4
Stack state (Top to Bottom):
[](Empty)
Combining the popped elements in chronological order gives the sequence: 9, 5, 4.