Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Tests  >  Test: Stack - Computer Science Engineering (CSE) MCQ

Test: Stack - Computer Science Engineering (CSE) MCQ


Test Description

10 Questions MCQ Test - Test: Stack

Test: Stack for Computer Science Engineering (CSE) 2024 is part of Computer Science Engineering (CSE) preparation. The Test: Stack questions and answers have been prepared according to the Computer Science Engineering (CSE) exam syllabus.The Test: Stack MCQs are made for Computer Science Engineering (CSE) 2024 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests for Test: Stack below.
Solutions of Test: Stack questions in English are available as part of our course for Computer Science Engineering (CSE) & Test: Stack solutions in Hindi for Computer Science Engineering (CSE) course. Download more important topics, notes, lectures and mock test series for Computer Science Engineering (CSE) Exam by signing up for free. Attempt Test: Stack | 10 questions in 30 minutes | Mock test for Computer Science Engineering (CSE) preparation | Free important questions MCQ to study for Computer Science Engineering (CSE) Exam | Download free PDF with solutions
*Answer can only contain numeric values
Test: Stack - Question 1

Consider the following pseudo-code to perform push and pop operation on a stack using enqueue(EQ) and dequeue(DQ) operations on 2 queues q1 and q2. Let x be an element to be pushed in the stack. answer the following questions.

push(q1,q2,x)
{
if(q1 is empty)
{
EQ(q2,x);
}
Else
{
EQ(q1,x);
}
 
}
pop(q1, q2)
{
if (q1 is empty)
{
if (q2 is empty) 

print “stack underflow”;
Exit;
}
else
{
while(q2 does not contain one element)
{
k= DQ(q2);
EQ(q1,k);
}
Return (DQ(q2));
}
}
Else
{
while(q1 does not contain one element)
{
k= DQ(q1);
EQ(q2,k);
}
Return (DQ(q1));
}
}

If we perform 8 push and 2 pop operations on the above algorithm then total how many enqueue and dequeue operations are performed on queues?


Detailed Solution for Test: Stack - Question 1

8 push operation is equivalent to 8 EQ operation.
2 pop operation is equivalent = 1st pop and 2nd pop one after the other
1st pop operation = (7 DQ(q1) + 7 EQ(q2) + 1 DQ(q1) )
2nd pop operation = (6 DQ(q2) + 6 EQ(q1) + 1 DQ(q2) )
Total =8EQ+ 15 DQ + 13 EQ = 36

Hence the correct answer is 36.

Test: Stack - Question 2

You are given two stacks with the element 1,2,3,4,5, which must be placed in the same sequence in both stacks. Once an element is added to a stack, it can only be popped and displayed (but it cannot be pushed onto the other stack again). So the possible actions are s.pop(), s.push(), and s.top() (to return the stack's top element), and the method display(int) outputs an integer result. Both stacks have access to these three operations. If it is not empty, you can do a pop operation whenever you want. Which of the following sequences cannot be produced with the aforementioned constraints?

Detailed Solution for Test: Stack - Question 2

Let S1 and S2 be two stacks and let pop(), push (), and top() be the operations on the stack.

The element 1,2,3,4,5 in order.
Option 1:
To obtain 3 1 2 4 5
S1.push(1) ;
S2.push(2);
S1.push(3);
display(S1.top());
S1.pop();
display(S1.top())
S1.pop();
display(S2.top())
S2.pop();
S1.push(4)
display(S1.top())
S1.pop();
S1.push(5);
display(S1.top())
S1.pop();

Option 2 : 4 1 5 2 3
There can be no way to generate option 2.

Option 3:
To generate  4 2 1 3 5
S1.push(1);
S1.push(2);
S2.push(3);
S2.push(4);
display(S2.top());
S2.pop();
display(S1.top());
S1.pop();
display(S1.top());
S1.pop();
display(S2.top());
S2.pop();
S1.push(5);
display(S1.top());
S1.pop();

Option 4:
To obtain option 5 4 3 2 1.
Push all elements onto the  stacks, The stack will be like:
1 2 3 4 5, where 5 is present at the top of the stack and 1 is at the bottom of the stack. Now popping those elements and printing will output the sequence 5 4 3 2 1.

Hence the correct answer is 4 1 5 2 3.

1 Crore+ students have signed up on EduRev. Have you? Download the App
Test: Stack - Question 3

What is the postfix representation of the following infix expression?

(A + B) * C – D * E / F

Detailed Solution for Test: Stack - Question 3

(A + B) * C – D * E / F
A B + * C – D * E / F
A B + C * – D * E / F
A B + C * – D E * / F
A B + C * – D E * F /
A B + C * D E * F / –

Test: Stack - Question 4

The result evaluating the postfix expression 10 5 + 60 6 / * 8 − is

Detailed Solution for Test: Stack - Question 4

Postfix expression 10 5 + 60 6 / * 8 −

PUSH: 10 and 5(TOS)

Add: 10 + 5 = 15
PUSH: 15 60 6 (TOS)

Divide: 60/6 = 10
PUSH: 10

Multiply: 15 × 10 = 150
PUSH: 150 and 8

Subtract: 150 - 8 = 142

Diagram:


Evaluate Infix notation:
(((10 + 5) * (60 / 6)) - 8)  = ((15 * 10) - 8) = 150 - 8 = 142

Test: Stack - Question 5

In stack, memory allocation and deallocation is performed in ________.

Detailed Solution for Test: Stack - Question 5

A stack is a linear data type that acts as a collection of elements. A stack is a linear data structure that operates memory allocation and deallocation on the Last-In-First-Out (LIFO) principle. In linear data structures, the elements are accessed in sequential order but it is not compulsory to store all elements sequentially. It has two main operations:

  • Push, which adds a new element to the collection, and
  • Pop, which removes the most recently added element that hasn't been removed yet.

Hence the correct answer is Last­-in-­first­-out (LIFO) manner.

Test: Stack - Question 6

A stack can be implemented using queue, but then we need to use atleast:

Detailed Solution for Test: Stack - Question 6

A stack can be implemented using two queues. Let stack to be implemented be ‘x’ and queues used to implement be ‘a’ and ‘b’.

Method 1 (By push operation)
This method makes sure that the newly entered element is always at the front of ‘a’, so that pop operation just dequeues from ‘a’. ‘b’ is used to put every new element at front of ‘b’.

Method 2 (By making pop operation costly)
In a push operation, the new element is always enqueued to a. In pop() operation, if b is empty then all the elements except the last, are moved to b. Finally, the last element is dequeued from a and returned.

Therefore Option 2 is correct

Test: Stack - Question 7

Which of the following is the postfix equivalent of (9 - 5) + 2 ?

Detailed Solution for Test: Stack - Question 7

The given data:

Infix: (9 - 5) + 2 
Postfix: 95-2+


Hence the correct answer is 95-2+.

Test: Stack - Question 8

A stack is implemented with an array of ‘A [0..N – 1]’ and a variable ‘pos’. The push and pop operations are defined by the following code.

push(x)

    A[pos] ← x
    pos ← pos – 1

end push

pop( )

   pos ← pos + 1
   return A[pos]

end pop

Q. Which of the following will initialize an empty stack with capacity N for the above implementation?

Detailed Solution for Test: Stack - Question 8

For example, if we take an array with N = 4 to represent our stack. It will be A [0, 1, 2, 3].

Now, in order to push four elements, say x, y, z, w

  • First we will push at x index 3 which is 4 – 1
  • Then, we will decrement pos and push y at index 2 and so on.

Note that, the pop operation is also behaving inversely here.

  • In usual stack, pop would decrement value of pos.

In our case, pop is incrementing the value of pos.

Test: Stack - Question 9

A recursive problem like tower of hanoi can be rewritten without recursion using:

Detailed Solution for Test: Stack - Question 9

A recursive problem like the Tower of Hanoi can be rewritten using system stack or user-defined stack
Recurrence relation of tower of Hanoi:  T(n) = 2T(n - 1) + 1 

Test: Stack - Question 10

The queue data structure is to be realized by using stack. The number of stacks needed would be

Detailed Solution for Test: Stack - Question 10

Method 1 (efficient push):

  • push:
    • enqueue in queue1
  • pop:
    • while the size of queue1 is bigger than 1, pipe dequeued items from queue1 into queue2
    • dequeue and return the last item of queue1, then switch the names of queue1 and queue2

Method 2 (efficient pop):

  • push:
    • enqueue in queue2
    • enqueue all items of queue1 in queue2, then switch the names of queue1 and queue2
  • pop:
    • deqeue from queue1
Information about Test: Stack Page
In this test you can find the Exam questions for Test: Stack solved & explained in the simplest way possible. Besides giving Questions and answers for Test: Stack, EduRev gives you an ample number of Online tests for practice

Top Courses for Computer Science Engineering (CSE)

Download as PDF

Top Courses for Computer Science Engineering (CSE)