📖 Scenario: Imagine you are managing a stack of books on a table. You can only add a book on top or remove the top book. This is exactly how a stack data structure works using the Last In First Out (LIFO) principle.
🎯 Goal: You will create a stack using Java's Stack class, add some books to it, then remove the top book and finally print the stack to see the remaining books.
📋 What You'll Learn
Create a Stack<String> called books
Add these books in order: "Math", "Science", "History"
Remove the top book using pop()
Print the remaining stack
💡 Why This Matters
🌍 Real World
Stacks are used in real life for undo features in software, browser history, and managing function calls in programming.
💼 Career
Understanding stacks and LIFO is essential for software developers, especially when working with algorithms, parsing expressions, and managing program execution.
Progress0 / 4 steps
1
Create the Stack and add books
Create a Stack<String> called books and add these books in order: "Math", "Science", "History" using the push() method.
DSA Java
Hint
Use Stack<String> books = new Stack<>(); to create the stack. Use books.push("BookName") to add each book.
2
Remove the top book
Use the pop() method on the books stack to remove the top book.
DSA Java
Hint
Use books.pop(); to remove the last book added.
3
Print the remaining books in the stack
Use System.out.println(books); to print the current state of the books stack.
DSA Java
Hint
Use System.out.println(books); to see the books left in the stack.
4
Explain the LIFO principle with the stack output
Add a comment above the print statement explaining that the stack follows the Last In First Out (LIFO) principle, so the last book added was the first to be removed.
DSA Java
Hint
Add a comment like // The stack follows LIFO: last book added (History) was removed first above the print statement.
Practice
(1/5)
1. What does the LIFO principle mean in the context of a stack?
easy
A. First In, First Out - the first element added is the first to be removed
B. Last In, First Out - the last element added is the first to be removed
C. Elements are removed randomly
D. Elements are removed based on their value
Solution
Step 1: Understand the LIFO principle
LIFO means the last item you put in is the first one you take out.
Step 2: Apply to stack behavior
Stacks follow LIFO, so the last pushed element is popped first.
Final Answer:
Last In, First Out - the last element added is the first to be removed -> Option B
Quick Check:
LIFO = Last In, First Out [OK]
Hint: Remember stack like a stack of plates, last on top removed first [OK]
Common Mistakes:
Confusing LIFO with FIFO
Thinking elements are removed randomly
Assuming removal depends on element value
2. Which of the following is the correct way to push an element '5' onto a stack named stack in Java?
easy
A. stack.insert(5);
B. stack.add(5);
C. stack.put(5);
D. stack.push(5);
Solution
Step 1: Recall Java Stack method for adding elements
The method to add an element to a stack is push().
Step 2: Match method with given options
Only stack.push(5); correctly uses the stack push method.
Final Answer:
stack.push(5); -> Option D
Quick Check:
Use push() to add elements to stack [OK]
Hint: Use push() to add elements to stack in Java [OK]
Common Mistakes:
Using add() which is for lists, not stacks
Using insert() or put() which are invalid for Stack
Confusing stack methods with other collections
3. What will be the output of the following Java code?
Stack<Integer> stack = new Stack<>();
stack.push(10);
stack.push(20);
stack.push(30);
System.out.println(stack.pop());
System.out.println(stack.pop());
medium
A. 30\n20
B. 30\n10
C. 20\n30
D. 10\n20
Solution
Step 1: Trace push operations
Elements pushed in order: 10 (bottom), 20, 30 (top).
Step 2: Trace pop operations
First pop removes 30, second pop removes 20.
Final Answer:
30\n20 -> Option A
Quick Check:
Stack pops last pushed first: 30 then 20 [OK]
Hint: Pop returns last pushed element first [OK]
Common Mistakes:
Assuming pop removes from bottom
Mixing order of popped elements
Confusing push and pop effects
4. Identify the error in the following Java code snippet using a stack:
Stack<String> stack = new Stack<>();
stack.push("apple");
stack.push("banana");
String fruit = stack.pop();
System.out.println(stack.peek());
stack.pop();
stack.pop();
medium
A. Using peek() instead of pop() is incorrect
B. Stack cannot hold String objects
C. Calling pop() on an empty stack causes EmptyStackException
D. push() method is not defined for Stack
Solution
Step 1: Trace stack operations
Push "apple", then "banana". Pop removes "banana". Peek shows "apple". Next pop removes "apple".
Step 2: Identify error on last pop
Last pop tries to remove from empty stack, causing EmptyStackException.
Final Answer:
Calling pop() on an empty stack causes EmptyStackException -> Option C
Quick Check:
Pop on empty stack throws exception [OK]
Hint: Pop more times than pushes causes error [OK]
Common Mistakes:
Ignoring exception on empty pop
Thinking peek removes element
Believing stack can't hold Strings
5. Given a stack with elements pushed in this order: 1, 2, 3, 4, 5. Which sequence of pop operations will produce the output 5 4 3 and leave the stack with elements 1 2 remaining?
hard
A. Pop three times consecutively
B. Pop once, push 6, then pop twice
C. Pop twice, push 7, then pop once
D. Pop once, pop twice, push 8
Solution
Step 1: Understand initial stack order
Stack top is 5, bottom is 1 after pushing 1 to 5.
Step 2: Pop three times consecutively
Pop removes 5, then 4, then 3, leaving 1 and 2 in stack.
Final Answer:
Pop three times consecutively -> Option A
Quick Check:
3 pops remove top 3 elements [OK]
Hint: Pop as many times as output elements needed [OK]
Common Mistakes:
Adding elements unnecessarily
Confusing push and pop order
Expecting output without popping
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.