Stack using Array

Uncategorized

This set of Data Structure Multiple Choice Questions & Answers (MCQs) focuses on “Stack using Array”.

1. What does ‘stack underflow’ refer to?
A) accessing item from an undefined stack
B) adding items to a full stack
C) removing items from an empty stack
D) index out of bounds exception

Answer: C
Explanation: Stack underflow occurs when objects are removed from an empty stack.

2. Which of the following real world scenarios would you associate with a stack data structure?
A) piling up of chairs one above the other
B) people standing in a line to be serviced at a counter
C) offer services based on the priority of the customer
D) tatkal Ticket Booking in IRCTC

Answer: A
Explanation: The Last In First Out (LIFO) strategy is preceded by Stack. A LIFO queue is formed by stacking chairs one on top of the other; a queue is formed by people standing in a line; and if the service is based on priority, it may be associated with a priority queue. Tatkal Ticket Booking is based on a first-come, first-served basis. Those who press the book now button first will be taken to the booking tab.

3. What does the following function check for? (all necessary headers to be included and function is called from main)

#define MAX 10
 
typedef struct stack
{
    int top;
    int item[MAX];
}stack;
 
int function(stack *s)
{
    if(s->top == -1)
        return 1;
    else return 0;
}

A) full stack
B) invalid index
C) empty stack
D) infinite stack

Answer: C
Explanation: An empty stack is represented with the top-of-the-stack(‘top’ in this case) to be equal to -1.

4. What is the output of the following program?

public class Stack
{
	protected static final int CAPACITY = 100;
	protected int size,top = -1;
	protected Object stk[];
 
	public Stack()
	{
		stk = new Object[CAPACITY];
	}
 
	public void push(Object item)
	{
		if(size_of_stack==size)
		{
			System.out.println("Stack overflow");
				return;
		}
		else
		{
			top++;
			stk[top]=item;
		}
	}
	public Object pop()
	{
		if(top<0)
		{
			return -999;
		}
		else
		{
			Object ele=stk[top];
			top--;
			size_of_stack--;
			return ele;
		}
	}
}
 
public class StackDemo
{
	public static void main(String args[])
	{
		Stack myStack = new Stack();
		myStack.push(10);
		Object element1 = myStack.pop();
		Object element2 = myStack.pop();
		System.out.println(element2);
	}
}

A) stack is full
B) 20
C) 0
D) -999

Answer: D
Explanation: The first call to pop() returns 10, while the second call to pop() causes stack underflow and returns -999.

5. Which of the following array position will be occupied by a new element being pushed for a stack of size N elements(capacity of stack > N)?
A) S[N-1]
B) S[N]
C) S[1]
D) S[0]

Answer: B
Explanation: As a result, the elements are moved at the end, resulting in N.

6. What is the time complexity of pop() operation when the stack is implemented using an array?
A) O(1)
B) O(n)
C) O(logn)
D) O(nlogn)

Answer: A
Explanation: Because pop() only accesses one end of the structure, it takes a fixed amount of time.

7. What is the functionality of the following piece of Java code?
Assume: ‘a’ is a non empty array of integers, the Stack class creates an array of specified size and provides a top pointer indicating TOS(top of stack), push and pop have normal meaning.

public void some_function(int[] a)
{
	Stack S=new Stack(a.length);
	int[] b=new int[a.length];
	for(int i=0;i<a.length;i++)
	{
		S.push(a[i]);
	}
	for(int i=0;i<a.length;i++)
	{
		b[i]=(int)(S.pop());
	}
	System.out.println("output :");
	for(int i=0;i<b.length;i++)
	{
		System.out.println(b[i]);
	}
}

A) print alternate elements of array
B) duplicate the given array
C) parentheses matching
D) reverse the array

Answer: D
Explanation: The elements of the provided array ‘a’ are pushed into the stack and then popped out into the array ‘b.’ Since the stack is a LIFO structure, the given array is reversed.

8. What happens when you pop from an empty stack while implementing using the Stack ADT in Java?
A) Undefined error
B) Compiler displays a warning
C) EmptyStackException is thrown
D) NoStackException is thrown

Answer: C
Explanation: The Stack ADT throws an EmptyStackException if the stack is empty and a pop() operation is tried on it.

9. Which of the following array element will return the top-of-the-stack-element for a stack of size N elements(capacity of stack > N)?
A) S[N-1]
B) S[N]
C) S[N-2]
D) S[N+1]

Answer: A
Explanation: Since array indexing begins at zero, N-1 is the last index.

10. Array implementation of Stack is not dynamic, which of the following statements supports this argument?
A) space allocation for array is fixed and cannot be changed during run-time
B) user unable to give the input for stack operations
C) a runtime exception halts execution
D) improper program compilation

Answer: A
Explanation: After memory has been allocated, you can’t change the size of an array; inserting less elements than the array size wastes space, and adding more elements than the array size at run time causes Stack Overflow.

The stack is generated using the array in array implementation. Arrays are used to execute all of the operations on the stack. An Abstract Data Type (ADT) is a data type that is used in almost all programming languages. It’s called stack because it looks like a real-life stack, such as a deck of cards or a stack of plates. A real-world stack can only perform operations on one end. Simply describe a one-dimensional array of a given size and use the LIFO principle to insert or remove values into that array using a variable called ‘top.’ The top is initially set to -1. In order to insert a value into the stack, we must first increment the top value by one and then insert.

Leave a Reply

Your email address will not be published. Required fields are marked *