arrow
arrow
arrow
What will be the output of the following C++ program?#include <iostream>#include <stack>using namespace std;void insertBottom(stack<int
Question

What will be the output of the following C++ program?

#include <iostream>
#include <stack>
using namespace std;

void insertBottom(stack<int>& s, int x) {
      if (s.empty()) {
          s.push(x);
         return;
       }

     int t = s.top();
     s.pop();

     insertBottom(s, x);

     s.push(t);
 }

void reverseStack(stack<int>& s) {
       if (s.empty())
             return;

      int t = s.top();
      s.pop();

     reverseStack(s);

    insertBottom(s, t);
  }  

void printStack(stack<int> s) {
       while (!s.empty()) {
             cout << s.top() << " ";
             s.pop();
       }
}

int main() {
      stack<int> s;

      s.push(34);
      s.push(25);
      s.push(58);
      s.push(96);
      s.push(12);

      reverseStack(s);

      cout << "Reversed Stack: ";
      printStack(s);

      return 0;
}​

A.

Reversed Stack: 12 96 58 25 34

B.

Reversed Stack: 34 25 58 96 12

C.

Reversed Stack: 25 34 58 96 12

D.

Reversed Stack: 96 58 25 34 12

E.

Compilation Error

Correct option is B

The stack elements are pushed in the following order:

  • 34
  • 25
  • 58
  • 96
  • 12

Since a stack follows the LIFO (Last In First Out) principle, the original top-to-bottom order is:

12 96 58 25 34

The function reverseStack() recursively removes all elements and uses insertBottom() to insert each element at the bottom of the stack. This completely reverses the stack.

After reversal, the stack becomes:

34 25 58 96 12

The printStack() function prints elements from top to bottom, so the output will be:

Reversed Stack: 34 25 58 96 12

Important Key Points:

  1. A stack follows the LIFO (Last In First Out) principle.
  2. reverseStack() uses recursion to reverse the order of elements.
  3. insertBottom() inserts an element at the bottom of the stack recursively.
  4. After reversal, the original bottom element becomes the new top element.

Knowledge Booster:

  1. (a) Reversed Stack: 12 96 58 25 34: Incorrect because this is the original top-to-bottom order before reversal.
  2. (c) Reversed Stack: 25 34 58 96 12: Incorrect because the order after proper reversal should start with 34 at the top.
  3. (d) Reversed Stack: 96 58 25 34 12: Incorrect because this is not the correctly reversed sequence of the original stack.
  4. (e) Compilation Error: Incorrect because the program is syntactically correct and compiles successfully in C++.

Free Tests

Free
Must Attempt

SBI Clerk Mains 2026 : Reasoning Section Test - 01

languageIcon English
  • pdpQsnIcon50 Questions
  • pdpsheetsIcon60 Marks
  • timerIcon45 Mins
languageIcon English
Free
Must Attempt

SBI Clerk Mains 2026 Full Mock Test-01

languageIcon English
  • pdpQsnIcon190 Questions
  • pdpsheetsIcon200 Marks
  • timerIcon160 Mins
languageIcon English
Free
Must Attempt

SBI Clerk Mains MBT (29th October 2026 S1)- Based on Backlog Vacancies

languageIcon English
  • pdpQsnIcon190 Questions
  • pdpsheetsIcon200 Marks
  • timerIcon160 Mins
languageIcon English
test-prime-package

Access ‘IBPS SO IT Officer’ Mock Tests with

  • 1.5 Lakhs+ Mocks and Previous Year Papers
  • Unlimited Re-Attempts
  • Personalised Report Card
  • 500% Refund on Final Selection
  • Largest Community
students-icon
585k+ students have already unlocked exclusive benefits with Test Prime!
Our Plans
Monthsup-arrow