All Exams  >   Software Development  >   DSA in C++  >   All Questions

All questions of Stacks and Queues for Software Development Exam

Which data structure follows the Last-In-First-Out (LIFO) principle?
  • a)
    Stack
  • b)
    Queue
  • c)
    Linked List
  • d)
    Heap
Correct answer is option 'A'. Can you explain this answer?

Hackers World answered
Stacks follow the Last-In-First-Out (LIFO) principle, where the last element added is the first one to be removed.

What will be the output of the following code?
#include <iostream>
#include <stack>
using namespace std;
int main() {
   stack<int> myStack;
   myStack.push(10);
   myStack.push(20);
   myStack.push(30);
   myStack.pop();
   cout << myStack.size() << endl;
   return 0;
}
  • a)
    0
  • b)
    1
  • c)
    2
  • d)
    3
Correct answer is option 'C'. Can you explain this answer?

Amrutha Datta answered
Understanding the Code
The provided C++ code utilizes the standard library's stack container to manage a collection of integers. Here's a breakdown of the operations performed:
Stack Operations
- Initialization: A stack named `myStack` is created.
- Pushing Elements:
- `myStack.push(10);` - The integer 10 is added to the stack.
- `myStack.push(20);` - The integer 20 is added to the stack.
- `myStack.push(30);` - The integer 30 is added to the stack.
After these operations, the stack contains three elements: 10, 20, and 30.
Pop Operation
- Removing the Top Element:
- `myStack.pop();` - This operation removes the top element of the stack, which is 30. After this operation, the stack now contains the elements 10 and 20.
Calculating the Size
- Getting the Size:
- `cout < mystack.size()="" />< endl;`="" -="" this="" line="" prints="" the="" current="" size="" of="" the="" stack.="" since="" we="" removed="" one="" element="" (30),="" the="" stack="" now="" has="" two="" remaining="" elements="" (10="" and="" />
Conclusion
- The final output of `myStack.size()` is 2, as there are two elements left in the stack after the pop operation.
Thus, the correct answer is option 'C', which corresponds to the size of the stack being 2.

Which of the following operations is NOT supported by a stack?
  • a)
    Push
  • b)
    Pop
  • c)
    Peek
  • d)
    Enqueue
Correct answer is option 'D'. Can you explain this answer?

Code Nation answered
Enqueue is an operation supported by a queue, not a stack. Enqueue adds an element to the rear end of a queue.

Given a stack st with elements [1, 2, 3, 4, 5], what will be the output of the following code?
#include <iostream>
#include <stack>
using namespace std;
void display(stack<int>& st) {
   while (!st.empty()) {
       cout << st.top() << " ";
       st.pop();
   }
}
int main() {
   stack<int> st;
   st.push(1);
   st.push(2);
   st.push(3);
   display(st);
   return 0;
}
  • a)
    1 2 3
  • b)
    3 2 1
  • c)
    Compile-time error
  • d)
    Run-time error
Correct answer is option 'B'. Can you explain this answer?

Understanding the Code
The provided C++ code manipulates a stack and displays its contents. Let's break down the key components.
Stack Initialization
- A stack of integers is created.
- Elements 1, 2, and 3 are pushed onto the stack in that order.
Function Definition
- The `display` function takes a stack by reference.
- It uses a `while` loop to print the top element and then pops it from the stack until the stack is empty.
Function Execution
- When calling `display(st)` in `main()`, the stack contains only the elements 1, 2, and 3.
- The `display` function will print the top element (3), then pop it.
- This process continues, printing 2 next, then popping it, and finally printing 1.
Result of the Code
- The output sequence will be 3, 2, 1 since the stack is a Last In, First Out (LIFO) structure.
Final Output
- Therefore, the correct answer is option 'B': 3 2 1.
In summary, the stack's behavior leads to the last elements pushed being the first ones to be displayed, resulting in the output being reversed from their original insertion order.

What will be the output of the following code?
#include <iostream>
#include <queue>
using namespace std;
int main() {
   queue<int> myQueue;
   myQueue.push(5);
   myQueue.push(10);
   myQueue.push(15);
   cout << myQueue.front() << endl;
   myQueue.pop();
   cout << myQueue.front() << endl;
   return 0;
}
  • a)
    5, 10
  • b)
    15, 10
  • c)
    10, 15
  • d)
    5, 15
Correct answer is option 'A'. Can you explain this answer?

Maulik Sen answered
Explanation:

Queue Operations:
- Initially, the queue is empty.
- We push three elements (5, 10, 15) into the queue.
- The front of the queue is the first element, which is 5.
- We pop the front element from the queue.
- The front of the queue now becomes 10.

Output:
- After the first push operation, the queue contains {5}.
- After the second push operation, the queue contains {5, 10}.
- After the third push operation, the queue contains {5, 10, 15}.
- The front of the queue is 5.
- After the pop operation, the queue contains {10, 15}.
- The front of the queue is now 10.
Therefore, the output of the code will be:
5
10

Which data structure follows the First-In-First-Out (FIFO) principle?
  • a)
    Stack
  • b)
    Queue
  • c)
    Linked List
  • d)
    Heap
Correct answer is option 'B'. Can you explain this answer?

Tom Tattle answered
Queues follow the First-In-First-Out (FIFO) principle, where the first element added is the first one to be removed.

Chapter doubts & questions for Stacks and Queues - DSA in C++ 2025 is part of Software Development exam preparation. The chapters have been prepared according to the Software Development exam syllabus. The Chapter doubts & questions, notes, tests & MCQs are made for Software Development 2025 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

Chapter doubts & questions of Stacks and Queues - DSA in C++ in English & Hindi are available as part of Software Development exam. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free.

DSA in C++

153 videos|115 docs|24 tests

Top Courses Software Development