Stack_and_Queue_QS
Stack_and_Queue_QS
Question 1
How many stacks are needed to implement a queue? Consider the situation where no
other data structure like arrays or linked lists is available to you.
A) 1
B) 2
C) 3
D) 4
Question 2
What is the time complexity of push and pop operations in a stack implemented using
an array?
A) O(n)
B) O(log n)
C) O(1)
D) O(n log n)
Question 3
#include <stdio.h>
#define SIZE 3
int stack[SIZE];
int top = -1;
void display() {
for (int i = top; i >= 0; i--)
printf("%d ", stack[i]);
}
int main() {
push(1);
push(2);
push(3);
push(4); // Overflow
display();
return 0;
}
Options:
A) Overflow 3 2 1
B) 3 2 1 Overflow
C) Overflow 1 2 3
D) Compilation Error
Question 4
Fill in the blanks to implement the peek function that returns the top element without
removing it:
int peek() {
if (________)
printf("Stack is empty\n");
else
return ________;
}
Question 5
Which of the following permutation can be obtained in the same order using a stack
assuming that input is the sequence 5, 6, 7, 8, 9 in that order?
A) 7, 8, 9, 5, 6
B) 5, 9, 6, 7, 8
C) 7, 8, 9, 6, 5
D) 9, 8, 7, 5, 6
Question 6
Convert the following infix expression into its equivalent postfix expression:
(A + B^ D) / (E – F) + G
A) ABD^ + EF – / G+
B) ABD + ^EF – / G+
C) ABD + ^EF / – G+
D) ABD^ + EF / – G+
Question 7
A) Stack
B) Queue
C) Array
D) Tree
Question 8
*+, -, , 3, 2, /, 8, 4, 1
A) 12
B) 5
C) 11
D) 4
Question 9
The five items P, Q, R, S, and T are pushed in a stack, one after the other starting from
P. The stack is popped four times and each element is inserted in a queue. Then two
elements are deleted from the queue and pushed back on the stack. Now one item is
popped from the stack. The popped item is:
A) P
B) R
C) Q
D) S
Question 10
#define SIZE 5
int stack[SIZE];
int pop() {
if (top == -1) {
printf("Stack Underflow");
return -1;
} else {
return stack[top--];
}
}
}
Question 11
#include <stdio.h>
#define SIZE 5
int stack[SIZE];
int top = 0;
int pop() {
if (top == 0)
printf("Stack Underflow\n");
else
return stack[--top];
}
int main() {
push(1);
push(2);
printf("%d", pop());
return 0;
}
Question 12
Consider the following pseudo-code. Assume that IntQueue is an integer queue. What
does the function fun do?
fun(int n)
{
IntQueue q = new IntQueue();
q.enqueue(0);
q.enqueue(1);
for (int i = 0; i < n; i++)
{
int a = q.dequeue();
int b = q.dequeue();
q.enqueue(b);
q.enqueue(a + b);
print(a);
}
}
Question 13
What is the time complexity of enqueue and dequeue operations in a queue (using an
array)?
A) O(n)
B) O(log n)
C) O(1)
D) O(n log n)
Question 14
In a circular queue of size N, what is the condition for the queue to be full?
A) front == rear + 1
B) rear == (front + 1) % N
C) (rear + 1) % N == front
D) rear == front
Question 15
A) 0 and 0
B) -1 and -1
C) 1 and -1
D) 0 and -1
Question 16
A) O(1)
B) O(n)
C) O(log n)
D) O(n log n)
Question 17
A) Recursion
B) CPU scheduling
C) Depth-first search
D) Function call stack
Question 18
What is the initial value of the front and rear in an empty queue implemented using an
array?
A) front = 0, rear = 0
B) front = -1, rear = -1
C) front = 0, rear = -1
D) front = -1, rear = 0
Question 19
#include <stdio.h>
#define SIZE 5
int queue[SIZE];
int front = -1, rear = -1;
int dequeue() {
if (front == -1 || front > rear) {
printf("Queue Underflow\n");
return -1;
}
return queue[front++];
}
int main() {
enqueue(10);
enqueue(20);
printf("%d ", dequeue());
enqueue(30);
printf("%d\n", dequeue());
return 0;
}
A) 10 20
B) 20 30
C) 10 30
D) Queue Overflow
Question 20