Why do stacks follow the Last In, First Out (LIFO) principle?
Think about how you would remove items from a pile where you can only take from the top.
Stacks work like a pile of objects where you can only add or remove from the top. The last item placed on the stack is the first one you can take off, which is why stacks follow the LIFO principle.
Which real-life example best illustrates the LIFO principle used by stacks?
Consider how you would access items if you can only take from the top of a pile.
A stack of books where you add and remove only from the top perfectly shows LIFO because the last book placed on top is the first one you take off.
Given a stack where elements are pushed in the order: 1, 2, 3, what will be the order of elements popped out?
Remember that the last element pushed is the first to be popped.
Since stacks follow LIFO, the last element pushed (3) is popped first, then 2, then 1.
Which statement correctly compares the LIFO principle of stacks with the FIFO principle of queues?
Think about how a line (queue) works compared to a pile (stack).
Stacks follow LIFO, removing the last item added first. Queues follow FIFO, removing the first item added first.
Why do programming languages use the LIFO principle (stack) to manage function calls?
Think about how a program remembers where to return after a function finishes.
When a function calls another, the program must wait for the new function to finish before continuing. Using a stack (LIFO) ensures the last called function finishes first, preserving the correct execution order.