A stack is empty when it has no elements inside it. This means there is nothing to pop or peek.
Click to reveal answer
beginner
How can you check if a stack is full in a fixed-size stack?
A stack is full when the number of elements reaches its maximum capacity. You check if the top index equals the maximum size minus one.
Click to reveal answer
beginner
In Rust, what data structure property helps to check if a stack is empty?
You can check if the vector (Vec) used for the stack has length zero using the is_empty() method.
Click to reveal answer
beginner
Why is it important to check if a stack is empty before popping?
Popping from an empty stack causes errors or crashes because there is no element to remove.
Click to reveal answer
intermediate
What is a simple Rust code snippet to check if a stack is full given a max size?
if stack.len() == max_size { /* stack is full */ } else { /* not full */ }
Click to reveal answer
What does stack.is_empty() return in Rust when the stack has no elements?
ANone
Bfalse
C0
Dtrue
✗ Incorrect
The is_empty() method returns true if the stack has zero elements.
If a stack has max size 5, when is it considered full?
AWhen it has 5 elements
BWhen it has 0 elements
CWhen it has 6 elements
DWhen it has 4 elements
✗ Incorrect
A stack is full when it reaches its maximum capacity, which is 5 elements here.
What happens if you pop from an empty stack?
AYou get the last element
BIt causes an error or panic
CNothing happens
DThe stack becomes full
✗ Incorrect
Popping from an empty stack causes an error because there is no element to remove.
Which Rust method checks if a vector is empty?
Ais_empty()
Blen()
Cpop()
Dpush()
✗ Incorrect
The is_empty() method returns true if the vector has no elements.
How do you check if a stack is full in Rust if you know max_size?
Astack.is_empty()
Bstack.len() > max_size
Cstack.len() == max_size
Dstack.len() < max_size
✗ Incorrect
When the stack length equals max_size, the stack is full.
Explain how to check if a stack is empty or full in Rust using a vector and max size.
Think about vector length and max capacity.
You got /3 concepts.
Describe why checking stack state (empty/full) is important in stack operations.
Consider what happens if you ignore these checks.
You got /3 concepts.
Practice
(1/5)
1. What does the is_empty() method of a stack typically check?
easy
A. If the stack has more than one element
B. If the stack is full and cannot accept more elements
C. If the stack has exactly one element
D. If the stack has no elements inside
Solution
Step 1: Understand the purpose of is_empty()
This method checks whether the stack contains any elements or not.
Step 2: Interpret the meaning of empty stack
An empty stack means it has zero elements inside, so is_empty() returns true only in this case.
Final Answer:
If the stack has no elements inside -> Option D
Quick Check:
Empty stack means no elements [OK]
Hint: Empty means zero elements inside the stack [OK]
Common Mistakes:
Confusing empty with full
Thinking empty means one element
Assuming empty means stack is null
2. Which of the following is the correct Rust syntax to check if a stack named stack is full assuming it has a fixed capacity stored in capacity and current size in size?
easy
A. if stack.len() == capacity { /* full */ }
B. if stack.size == capacity { /* full */ }
C. if stack.is_full() { /* full */ }
D. if stack.size > capacity { /* full */ }
Solution
Step 1: Identify how to get current stack size
In Rust, len() method is commonly used to get the current number of elements in a collection like a stack.
Step 2: Compare current size with capacity
If stack.len() equals capacity, the stack is full.
Final Answer:
if stack.len() == capacity { /* full */ } -> Option A
Quick Check:
Use len() to check size [OK]
Hint: Use stack.len() to get current size in Rust [OK]
Common Mistakes:
Using stack.size instead of stack.len()
Checking size > capacity instead of == capacity
Assuming is_full() method exists by default
3. What will be the output of the following Rust code snippet?
A. Vec<i32> cannot be used to store stack elements
B. Missing return type in is_empty method
C. Using '=' instead of '==' for comparison in is_empty method
D. is_empty method should take mutable self parameter
Solution
Step 1: Check the comparison operator in is_empty
The code uses single '=' which is assignment, not comparison. It should use '==' to compare length with zero.
Step 2: Confirm other parts are correct
The return type bool is present, Vec<i32> is valid, and self does not need to be mutable for this check.
Final Answer:
Using '=' instead of '==' for comparison in is_empty method -> Option C
Quick Check:
Use '==' for comparison, not '=' [OK]
Hint: Use '==' to compare values, '=' is assignment [OK]
Common Mistakes:
Using '=' instead of '==' in conditions
Forgetting return type bool
Making self mutable unnecessarily
5. You have a stack with capacity 5 and current elements [10, 20, 30]. You want to write a Rust function can_push that returns true if the stack is not full and false otherwise. Which implementation is correct?