Three elements (10, 20, 30) are pushed into a stack with capacity 3, so stack is full.
Step 2: Evaluate isFull and isEmpty values
isFull = stack.Count == 3 is true; isEmpty = stack.Count == 0 is false.
Final Answer:
True\nFalse -> Option B
Quick Check:
Count=3 means full=true, empty=false [OK]
Hint: Count equals capacity means full; zero means empty [OK]
Common Mistakes:
Assuming stack is empty after pushes
Confusing isFull and isEmpty values
Ignoring stack capacity
4. The following C# code tries to check if a stack is full but causes a compile error:
Stack<int> stack = new Stack<int>();
if (stack.IsFull()) {
Console.WriteLine("Stack is full");
}
What is the error and how to fix it?
medium
A. Change Stack<int> to List<int>
B. Stack cannot be empty; remove the if condition
C. IsFull() method does not exist; use stack.Count == capacity instead
D. Use stack.IsEmpty() instead of IsFull()
Solution
Step 1: Identify the error
The IsFull() method is not defined in C# Stack class, causing a compile error.
Step 2: Correct the check for full stack
Use stack.Count == capacity to check if stack is full, since Stack has no IsFull() method.
Final Answer:
IsFull() method does not exist; use stack.Count == capacity instead -> Option C
Quick Check:
No IsFull() method in Stack class [OK]
Hint: Stack class has no IsFull(); check count vs capacity [OK]
Common Mistakes:
Assuming IsFull() exists by default
Using IsEmpty() to check full
Changing data structure unnecessarily
5. You have a fixed size stack of capacity 5. After pushing 3 elements, you want to check if you can push 2 more without overflow. Which condition correctly checks this in C#?
hard
A. if (stack.Count - 2 >= 0) { /* safe to push */ }
B. if (stack.IsEmpty()) { /* safe to push */ }
C. if (stack.Count == 5) { /* safe to push */ }
D. if (stack.Count + 2 <= 5) { /* safe to push */ }
Solution
Step 1: Understand stack capacity and current count
Stack capacity is 5, current count is 3, so 2 more pushes are possible if total does not exceed 5.
Step 2: Formulate condition to check safe push
Check if current count plus 2 is less than or equal to capacity: stack.Count + 2 <= 5.
Final Answer:
if (stack.Count + 2 <= 5) { /* safe to push */ } -> Option D
Quick Check:
Count + pushes ≤ capacity means safe [OK]
Hint: Add pushes to count; compare with capacity [OK]
Common Mistakes:
Using IsEmpty() to check push safety
Checking count equals capacity incorrectly
Subtracting pushes instead of adding
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.