#include <iostream> #include <deque> int main() { std::deque<int> dq; dq.push_back(10); dq.push_front(20); dq.push_back(30); dq.pop_front(); dq.push_front(40); for (int x : dq) std::cout << x << " -> "; std::cout << "null" << std::endl; return 0; }
The operations modify the deque as follows:
- push_back(10): deque = 10
- push_front(20): deque = 20, 10
- push_back(30): deque = 20, 10, 30
- pop_front(): removes 20, deque = 10, 30
- push_front(40): deque = 40, 10, 30
Printing shows: 40 -> 10 -> 30 -> null
push_back(5), push_front(3), push_back(7), pop_back(), pop_front(), push_front(9)
What is the size of the deque now?
Step-by-step size changes:
- push_back(5): size = 1
- push_front(3): size = 2
- push_back(7): size = 3
- pop_back(): removes 7, size = 2
- pop_front(): removes 3, size = 1
- push_front(9): size = 2
Final size is 2, but note the last operation adds one element, so the final size is 2.
Rechecking: Actually, after pop_front size was 1, then push_front(9) makes size 2.
#include <iostream> #include <deque> int main() { std::deque<int> dq = {1, 2, 3}; std::cout << dq.at(3) << std::endl; return 0; }
The deque has elements at indices 0, 1, and 2. Accessing dq.at(3) is out of range.
The at() method throws std::out_of_range exception when index is invalid.
The correct methods are push_front() and push_back().
Options C and D use invalid method names.
Option B reverses the intended positions.
To check palindrome, compare characters from front and back moving inward.
Option D correctly uses front() and back() to compare, then removes them if equal.
Other options do not compare both ends or do not remove characters properly.
