Which header file is required to use `stack` in C++ STL?
A
<vector> B
<queue> C
<stack> D
<list>
Analysis & Theory
`std::stack` is defined in the `<stack>` header.
Which data structure does `std::stack` use by default internally?
A
Array B
Vector C
Deque D
List
Analysis & Theory
By default, `std::stack` uses `std::deque` as its underlying container.
Which function is used to add an element to the top of a stack?
A
insert() B
push() C
add() D
enqueue()
Analysis & Theory
`push()` adds an element to the top of the stack.
What does `pop()` do in a stack?
A
Returns top element B
Removes top element C
Inserts new element D
Clears the stack
Analysis & Theory
`pop()` removes the top element of the stack.
Which function is used to view the top element of a stack?
A
peek() B
view() C
top() D
front()
Analysis & Theory
`top()` returns a reference to the top element of the stack.
How can you check if a stack is empty?
A
stack.length() == 0 B
stack.empty() C
stack.isEmpty() D
stack.size() == 0
Analysis & Theory
`empty()` returns true if the stack has no elements.
What will `stack.size()` return?
A
Size of the stack B
Top element of the stack C
Max stack capacity D
Returns boolean
Analysis & Theory
`size()` returns the number of elements currently in the stack.
Which of the following best describes stack behavior?
A
FIFO B
FILO C
LIFO D
LILO
Analysis & Theory
Stack follows Last In First Out (LIFO) principle.
What happens if you call `top()` on an empty stack?
A
Returns NULL B
Returns 0 C
Causes undefined behavior D
Returns -1
Analysis & Theory
Calling `top()` on an empty stack leads to undefined behavior.
Which of the following is not a valid `std::stack` operation?
A
push() B
pop() C
top() D
back()
Analysis & Theory
`back()` is not a valid member function of `std::stack`; it belongs to containers like `deque`.