Top MCQs on Stack Data Structure with Answers

Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first, comes out last.

More on stack Data Struucture

Stack Quiz

Stack Quiz

Question 1
Following is C like pseudo code of a function that takes a number as an argument, and uses a stack S to do processing.
void fun(int n)
{
    Stack S;  // Say it creates an empty stack S
    while (n > 0)
    {
      // This line pushes the value of n%2 to stack S
      push(&S, n%2);

      n = n/2;
    }

    // Run while Stack S is not empty
    while (!isEmpty(&S))
      printf("%d ", pop(&S)); // pop an element from S and print it
}
What does the above function do in general?
Cross
Prints binary representation of n in reverse order
Tick
Prints binary representation of n
Cross
Prints the value of Logn
Cross
Prints the value of Logn in reverse order


Question 1-Explanation: 
Question 2
Following is C like pseudo code of a function that takes a number as an argument, and uses a stack S to do processing.
void fun(int n)
{
    Stack S;  // Say it creates an empty stack S
    while (n > 0)
    {
      // This line pushes the value of n%2 to stack S
      push(&S, n%2);

      n = n/2;
    }

    // Run while Stack S is not empty
    while (!isEmpty(&S))
      printf("%d ", pop(&S)); // pop an element from S and print it
}
What does the above function do in general?
Cross
Prints binary representation of n in reverse order
Tick
Prints binary representation of n
Cross
Prints the value of Logn
Cross
Prints the value of Logn in reverse order


Question 2-Explanation: 
Question 3
Which one of the following is an application of Stack Data Structure?
Cross
Managing function calls
Cross
The stock span problem
Cross
Arithmetic expression evaluation
Tick
All of the above


Question 4
Which one of the following is an application of Stack Data Structure?
Cross
Managing function calls
Cross
The stock span problem
Cross
Arithmetic expression evaluation
Tick
All of the above


Question 5
Which of the following is true about linked list implementation of stack?
Cross
In push operation, if new nodes are inserted at the beginning of linked list, then in pop operation, nodes must be removed from end.
Cross
In push operation, if new nodes are inserted at the end, then in pop operation, nodes must be removed from the beginning.
Cross
Both of the above
Tick
None of the above


Question 5-Explanation: 
To keep the Last In First Out order, a stack can be implemented using linked list in two ways: a) In push operation, if new nodes are inserted at the beginning of linked list, then in pop operation, nodes must be removed from beginning. b) In push operation, if new nodes are inserted at the end of linked list, then in pop operation, nodes must be removed from end.
Question 6
Which of the following is true about linked list implementation of stack?
Cross
In push operation, if new nodes are inserted at the beginning of linked list, then in pop operation, nodes must be removed from end.
Cross
In push operation, if new nodes are inserted at the end, then in pop operation, nodes must be removed from the beginning.
Cross
Both of the above
Tick
None of the above


Question 6-Explanation: 
To keep the Last In First Out order, a stack can be implemented using linked list in two ways: a) In push operation, if new nodes are inserted at the beginning of linked list, then in pop operation, nodes must be removed from beginning. b) In push operation, if new nodes are inserted at the end of linked list, then in pop operation, nodes must be removed from end.
Question 7

Consider the following pseudocode that uses a stack 

C

   declare a stack of characters
   while ( there are more characters in the word to read )
   {
      read a character
      push the character on the stack
   }
   while ( the stack is not empty )
   {
      pop a character off the stack
      write the character to the screen
   }

What is output for input "geeksquiz"?

Cross

geeksquizgeeksquiz

Tick

ziuqskeeg

Cross

geeksquiz

Cross

ziuqskeegziuqskeeg



Question 7-Explanation: 

Since the stack data structure follows LIFO order. When we pop() items from stack, they are popped in reverse order of their insertion (or push())

Question 8

Consider the following pseudocode that uses a stack 

C

   declare a stack of characters
   while ( there are more characters in the word to read )
   {
      read a character
      push the character on the stack
   }
   while ( the stack is not empty )
   {
      pop a character off the stack
      write the character to the screen
   }

What is output for input "geeksquiz"?

Cross

geeksquizgeeksquiz

Tick

ziuqskeeg

Cross

geeksquiz

Cross

ziuqskeegziuqskeeg



Question 8-Explanation: 

Since the stack data structure follows LIFO order. When we pop() items from stack, they are popped in reverse order of their insertion (or push())

Question 9

Following is an incorrect pseudocode for the algorithm which is supposed to determine whether a sequence of parentheses is balanced: 

C

   declare a character stack 
   while ( more input is available)
   {
      read a character
      if ( the character is a '(' ) 
         push it on the stack
      else if ( the character is a ')' and the stack is not empty )
         pop a character off the stack
      else
         print "unbalanced" and exit
    }
    print "balanced"

Which of these unbalanced sequences does the above code think is balanced? 

Tick

((())

Cross

())(()

Cross

(()()))

Cross

(()))()



Question 9-Explanation: 

At the end of while loop, we must check whether the stack is empty or not. For input ((()), the stack doesn\'t remain empty after the loop. See http://www.geeksforgeeks.org/check-for-balanced-parentheses-in-an-expression/ for details.

Question 10

Following is an incorrect pseudocode for the algorithm which is supposed to determine whether a sequence of parentheses is balanced: 

C

   declare a character stack 
   while ( more input is available)
   {
      read a character
      if ( the character is a '(' ) 
         push it on the stack
      else if ( the character is a ')' and the stack is not empty )
         pop a character off the stack
      else
         print "unbalanced" and exit
    }
    print "balanced"

Which of these unbalanced sequences does the above code think is balanced? 

Tick

((())

Cross

())(()

Cross

(()()))

Cross

(()))()



Question 10-Explanation: 

At the end of while loop, we must check whether the stack is empty or not. For input ((()), the stack doesn\'t remain empty after the loop. See http://www.geeksforgeeks.org/check-for-balanced-parentheses-in-an-expression/ for details.

There are 60 questions to complete.


  • Last Updated : 27 Sep, 2023

Share your thoughts in the comments
Similar Reads