Machine-translated by Google. Questions are authored in English.
Top

Stacks Questions and Answers

1.

A stack is a liner data structure in which insertion and deletion of elements are done at only one end, which is known as the top of the stack. Stack is called a last-in, first-out (LIFO) structure because the last element which is added to the stack is the first element that is deleted from the stack.

TopL
M
N
O
P
BottomQ

Read the above paragraph and answer the following.

Which of the following items will be at the top in the stack after removing three items?

Exam Year(s)

DSSSB TGT - 2023,

Answer: A

O will be at the top. A stack removes items from the top, last in and first out. The stack from top to bottom is L, M, N, O, P, Q. Removing three items takes away L, then M, then N, and O is left at the top. L is the item removed first, N is removed third and P lies below O, so none of them is on top. Elements are added and removed only at the top, so the order of removal is the reverse of the order of addition.

Please log in or register to report this question.

2.

Consider a stack data structure into which we can PUSH and POP records. Assume that each record pushed in the stack has a positive integer key and that all keys are distinct.

We wish to augment the stack data structure with an O(1) time MIN operation that returns a pointer to the record with smallest key present in the stack
1) without deleting the corresponding record, and
2) without increasing the complexities of the standard stack operations.

Which one or more of the following approach(es) can achieve it?

Exam Year(s)

GATE CS - 2025,

Answer: A

Only keeping, with every record, a pointer to the record with the smallest key below it works.

On PUSH, compare the new key with the minimum stored in the current top record and save a pointer to whichever is smaller (the new record itself if it is smaller): O(1). On POP, simply discard the top record; the record beneath already holds the minimum of everything below it. MIN reads the top record's pointer: O(1). No standard operation becomes slower.

A single pointer to the minimum fails when the minimum is popped: finding the new minimum then needs a scan of the stack, O(n). A sorted auxiliary array makes PUSH and POP O(n), since an element must be inserted or removed in the middle. A min-heap gives O(1) MIN but makes PUSH and POP O(log n), increasing their complexity.

Please log in or register to report this question.

3.

Which is the post fix notation of (9 - 5) + 2

Exam Year(s)

MH SET - 2018, SET Exam - 2018,

Answer: B

The postfix notation is 95 - 2 +.

In postfix each operator follows its two operands. Convert the inner expression first, then the outer one:

\[ \begin{aligned} (9 - 5) &\to 9\ 5\ - \\ (9 - 5) + 2 &\to 9\ 5\ -\ 2\ + \end{aligned} \]

Evaluating it on a stack gives 9 - 5 = 4 and then 4 + 2 = 6, as the original does. '59 -' would compute 5 - 9, the operands reversed, '2 + 95 -' puts an operator before its operands, and '92 + 5 -' computes 9 + 2 - 5, a different expression.

Please log in or register to report this question.

4.

Which of the following applications may use a stack ?

Exam Year(s)

MH SET - 2019, SET Exam - 2019,

Answer: D

The answer is All of the mentioned.

A parentheses-balancing program pushes each opening bracket and pops on each closing one; the expression is balanced if every pop matches and the stack ends empty. At run time, local variables live in activation records on the call stack, pushed on a function call and popped on return. A compiler's syntax analyser, such as an LR or LL parser, keeps grammar symbols and states on a parsing stack.

Since each of the three uses a stack, choosing any one alone would be incomplete.

Please log in or register to report this question.

5.

Consider the usual algorithm for determining whether a sequence of parentheses is balanced.

The maximum number of parentheses that appear on the stack AT ANY ONE TIME when the algorithm analyzes ( ( ) ( ( ) ) ( ( ) ) ) is :

Exam Year(s)

MH SET - 2020, SET Exam - 2020,

Answer: C

The stack holds at most 3 parentheses.

The algorithm pushes each '(' and pops one for each ')'. The maximum stack size is the deepest nesting. For ( ( ) ( ( ) ) ( ( ) ) ): the outer '(' gives depth 1, the next pairs reach depth 2, and the inner '(' of each '( ( ) )' group reaches depth 3. The stack never holds more than three at once.

12 is the total number of characters, and 6 the number of '(' symbols, neither of which is ever on the stack at the same time. 4 would need one more level of nesting.

Please log in or register to report this question.

6.

The prefix form of (A - B / C) * (A / K - L) is :

Exam Year(s)

MH SET - 2021, SET Exam - 2021,

Answer: C

The prefix form is * - A / B C - / A K L.

Convert each part, giving / priority over -:

  • A - B / C: B / C becomes / B C, so the whole is - A / B C.
  • A / K - L: A / K becomes / A K, so the whole is - / A K L.

The two bracketed parts are multiplied, so * goes in front: * - A / B C - / A K L.

The option A B C A K L * - / - / is a postfix-style string with the operators gathered at the end, and the other two options place the operators in an order that does not reproduce the expression when read back.

Please log in or register to report this question.

7.

The postfix form of A*B+C/D is :

Exam Year(s)

MH SET - 2023, SET Exam - 2023,

Answer: B

The postfix form is A B * C D / +.

Multiplication and division bind more tightly than addition, so the expression is (A*B) + (C/D). Converting each part to postfix puts the operator after its operands:

\[ \begin{aligned} A*B &\rightarrow A\ B\ * \\ C/D &\rightarrow C\ D\ / \\ (A*B)+(C/D) &\rightarrow A\ B\ *\ C\ D\ /\ + \end{aligned} \]

Option A puts the operators first, which is closer to prefix form. Options C and D change the grouping, applying + before * and /, which is not what the expression means.

Please log in or register to report this question.

8.

The equivalent postfix express for d / (e + f) + b * c is :

Exam Year(s)

MH SET - 2024, SET Exam - 2024,

Answer: C

The postfix form is def + / bc * +.

Convert by precedence: the parenthesised e + f becomes ef+; dividing d by it gives def+/; b * c becomes bc*; finally the two parts are added.

\[ d / (e + f) + b * c \;\to\; d\,e\,f + /\ b\,c * + \]

Option B, def+/bc+*, evaluates to (d / (e + f)) * (b + c): it adds b and c and then multiplies, the reverse of the given expression. Options A and D are not valid for this expression at all; in A the division is applied to b and c, and D runs out of operands for its second +.

Please log in or register to report this question.

9.

Stack has applications in :

Exam Year(s)

MH SET - 2025, SET Exam - 2025,

Answer: D

Stacks are used in all of the above.

Function calls are managed on a call stack that holds return addresses, parameters and local variables. Arithmetic expressions are converted from infix to postfix and evaluated with a stack. In large number arithmetic, digits can be pushed onto stacks so that addition proceeds from the least significant digit with carries.

Since each listed use is a genuine stack application, no single option is complete on its own.

Please log in or register to report this question.

10.

Which data structure is most appropriate for evaluating a postfix expression ?

Exam Year(s)

Tripura Public Service Commission - 2026,

Answer: A

The most appropriate structure is a stack.

To evaluate a postfix expression, read it left to right. Push each operand on the stack. When an operator arrives, pop the top two operands, apply the operator (the first one popped is the right operand) and push the result. At the end the single value left on the stack is the answer. For 2 3 4 * +: push 2 and 3 and 4, then * gives 12, then + gives 14.

A queue is first-in first-out, so it would give operands back in the wrong order. A priority queue or an AVL tree orders items by value, which is irrelevant to evaluating postfix, where operator precedence is already settled by position.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: Stacks Questions and Answers || Stacks MCQ Questions and Answers || Stacks GK Questions and Answers || Stacks GK MCQ Questions || Stacks Multiple Choice Questions and Answers || Stacks GK || GK on Stacks || Data Structures and Algorithms Questions and Answers || Data Structures and Algorithms MCQ Questions and Answers || Data Structures and Algorithms GK Questions and Answers || GK on Data Structures and Algorithms