B. Change condition to s->top == -1 to check empty stack
C. Change s->top-- to s->top++
D. Remove the if condition entirely
Solution
Step 1: Understand empty stack condition
Stack is empty when top is -1, not 0, because top starts at -1.
Step 2: Fix condition to detect underflow
Change condition to s->top == -1 to correctly detect empty stack before popping.
Final Answer:
Change condition to s->top == -1 to check empty stack -> Option B
Quick Check:
Empty stack top = -1, check before pop [OK]
Hint: Empty stack top is -1, not 0 [OK]
Common Mistakes:
Checking top == 0 instead of -1
Incrementing top on pop
Ignoring underflow check
5. Given a stack implemented with an array of size 5 and top initially -1, after pushing 3 elements and then popping 4 times, what will be the value of top?
hard
A. 0
B. 1
C. -2
D. -1
Solution
Step 1: Calculate top after pushing 3 elements
Starting top = -1, pushing 3 times increments top to 2 (0,1,2).
Step 2: Calculate top after popping 4 times
Each pop decrements top by 1: 2 -> 1 -> 0 -> -1. The 4th pop tries to pop empty stack, top stays at -1.
Final Answer:
-1 -> Option D
Quick Check:
Pop beyond empty stack keeps top at -1 [OK]
Hint: Top never goes below -1 after pops [OK]
Common Mistakes:
Assuming top can go below -1
Counting pushes and pops incorrectly
Ignoring underflow protection
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.