A. Accessing top of empty stack causes runtime error
B. Syntax error in stack declaration
C. Cannot push integer 5 into stack<int>
D. pop() does not remove elements
Solution
Step 1: Analyze stack operations
Push 5 adds one element, first pop removes it, second pop tries to remove from empty stack.
Step 2: Check consequences of extra pop and top()
Second pop on empty stack is undefined; then top() on empty stack causes runtime error.
Final Answer:
Accessing top of empty stack causes runtime error -> Option A
Quick Check:
Empty stack top() = error [OK]
Hint: Never pop or top from empty stack [OK]
Common Mistakes:
Ignoring empty stack before pop
Assuming pop() always safe
Thinking push fails for int type
5. You want to reverse a string using a stack in C++. Which approach correctly uses a stack to reverse the string "hello"?
hard
A. Push all characters, then print stack without popping
B. Push each character, then pop all to build reversed string
C. Pop characters first, then push to stack
D. Use stack to sort characters alphabetically
Solution
Step 1: Understand stack reversal logic
Pushing characters stores them in order; popping reverses order due to LIFO.
Step 2: Evaluate options for correctness
Push each character, then pop all to build reversed string correctly pushes then pops to reverse; B prints original order; C pops before push invalid; D sorts, not reverses.
Final Answer:
Push each character, then pop all to build reversed string -> Option B
Quick Check:
Stack reverses order by pop after push [OK]
Hint: Push then pop to reverse order [OK]
Common Mistakes:
Printing stack without popping reverses nothing
Trying to pop before pushing
Confusing reversal with sorting
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.