Consider the following C++ code that uses a queue. What will be printed after peeking the front element?
std::queue<int> q; q.push(10); q.push(20); q.push(30); std::cout << q.front() << std::endl;
The front() function returns the first element inserted in the queue.
The queue stores elements in FIFO order. The first element pushed is 10, so front() returns 10.
What will happen if we call front() on an empty std::queue in C++?
std::queue<int> q;
std::cout << q.front() << std::endl;Calling front() on an empty queue is not safe.
Accessing front on an empty queue causes undefined behavior because no element exists to return.
Given a queue, which of the following operations does NOT change the front element?
Think about which operations modify the queue contents.
Calling front() only reads the front element without removing or adding anything, so the front element stays the same.
Identify the reason for the crash in the following code snippet:
std::queue<int> q; q.push(5); q.pop(); std::cout << q.front() << std::endl;
Check the state of the queue after pop() before calling front().
After pop(), the queue becomes empty. Calling front() on an empty queue causes undefined behavior and likely crash.
Given the following code, what is the printed output sequence?
std::queue<int> q; q.push(1); q.push(2); q.push(3); std::cout << q.front() << " "; q.pop(); std::cout << q.front() << " "; q.pop(); q.push(4); std::cout << q.front() << " ";
Track the queue contents after each operation carefully.
Initially queue: [1,2,3]. After first front(): prints 1. After pop(): queue is [2,3]. Second front(): prints 2. After pop(): queue is [3]. After push(4): queue is [3,4]. Third front(): prints 3.
