0% found this document useful (0 votes)
8 views

Stack_and_Queue_QS

The document contains a series of practice questions related to data structures, specifically focusing on stacks and queues. It includes multiple-choice questions on implementation, time complexity, and specific operations related to these data structures. The questions cover various concepts such as stack overflow, postfix expressions, and queue operations.

Uploaded by

lalitha vaddadi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Stack_and_Queue_QS

The document contains a series of practice questions related to data structures, specifically focusing on stacks and queues. It includes multiple-choice questions on implementation, time complexity, and specific operations related to these data structures. The questions cover various concepts such as stack overflow, postfix expressions, and queue operations.

Uploaded by

lalitha vaddadi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Data Structures: Stack and Queue — Practice Questions

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

Predict the Output:

#include <stdio.h>
#define SIZE 3

int stack[SIZE];
int top = -1;

void push(int val) {


if (top == SIZE - 1) {
printf("Overflow ");
return;
}
stack[++top] = val;
}

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

The data structure required to check whether an expression contains a balanced


parenthesis is?

A) Stack
B) Queue
C) Array
D) Tree

Question 8

What is the outcome of the prefix expression:

*+, -, , 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

Identify the error in the following stack implementation (using an array):

#define SIZE 5

int stack[SIZE];

int top = -1;


void push(int x) {
if (top == 5) {
printf("Stack Overflow");
} else {
stack[++top] = x;
}
}

int pop() {
if (top == -1) {
printf("Stack Underflow");
return -1;
} else {
return stack[top--];
}
}
}

What is the issue?

A) Incorrect condition for stack overflow


B) Incorrect condition for stack underflow
C) Array size exceeds capacity
D) No error

Question 11

Correct the error in the following stack implementation:

#include <stdio.h>
#define SIZE 5

int stack[SIZE];
int top = 0;

void push(int val) {


if (top == SIZE)
printf("Stack Overflow\n");
else
stack[top++] = val;
}

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);
}
}

A) Prints numbers from 0 to n-1


B) Prints numbers from n-1 to 0
C) Prints first n Fibonacci numbers
D) Prints first n Fibonacci numbers in reverse order

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

What is the initial value of front and rear in an empty queue?

A) 0 and 0
B) -1 and -1
C) 1 and -1
D) 0 and -1

Question 16

What is the time complexity of searching for an element in a queue?

A) O(1)
B) O(n)
C) O(log n)
D) O(n log n)

Question 17

Which of the following applications uses a queue?

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

What will be the output of the following code?

#include <stdio.h>
#define SIZE 5

int queue[SIZE];
int front = -1, rear = -1;

void enqueue(int val) {


if (rear == SIZE - 1) {
printf("Queue Overflow\n");
return;
}
if (front == -1) front = 0;
queue[++rear] = val;
}

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

Identify the error in the following queue implementation code:

void enqueue(int val) {


if (rear == SIZE) {
printf("Queue Overflow\n");
return;
}
queue[rear++] = val;
}

A) rear should be initialized to -1


B) Condition should check rear == SIZE - 1
C) front should be updated after each enqueue
D) No error

You might also like