Final answer:
To detect underflow in a stack, check if a pop operation is attempted when the stack is empty (top is -1). To detect overflow, check if a push operation is attempted when the stack is full (top is sizeOfArray-1).
Step-by-step explanation:
When operating on a stack in Java, underflow and overflow conditions can occur when the stack is too empty or too full, respectively. The correct answer to check for these conditions is option (b).
- An underflow condition occurs when trying to perform a pop operation from an empty stack, meaning when the top index is outside of the range [0, sizeOfArray-1], typically when the top index is at -1.
- An overflow condition occurs when trying to perform a push operation on a full stack, meaning when the top index is equal to sizeOfArray-1 and you attempt to add another element.