1.
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).
Answer: Option 'A'
Array indexing start from 0, hence N-1 is the last index
2.
‘Array implementation of Stack is not dynamic’, which of the following statements supports this argument?
Answer: Option 'A'
You cannot modify the size of an array once the memory has been allocated, adding fewer elements than the array size would cause wastage of space, and adding more elements than the array size at run time would cause Stack Overflow.
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; }
Answer: Option 'C'
An empty stack is represented with the top-of-the-stack(‘top’ in this case) to be equal to -1.
4.
What is the time complexity of pop() operation when the stack is implemented using an array?
Answer: Option 'A'
pop() accesses only one end of the structure, and hence constant time.
5.
What happens when you pop from an empty stack while implementing using the Stack ADT in Java?
Answer: Option 'C'
The Stack ADT throws an EmptyStackException if the stack is empty and a pop() operation is tried on it.
6.
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]); } }
Answer: Option 'D'
reverse the array
Every element from the given array ‘a’ is pushed into the stack, and then the elements are popped out into the array ‘b’. Stack is a LIFO structure, this results in reversing the given array.
7.
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).
Answer: Option 'B'
Elements are pushed at the end, hence N.
8.
Which of the following real world scenarios would you associate with a stack data structure?
Answer: Option 'A'
Stack follows Last In First Out(LIFO) policy. Piling up of chairs one above the other is based on LIFO, people standing in a line is a queue and if the service is based on priority, then it can be associated with a priority queue.
9.
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); } }
Answer: Option 'D'
The first call to pop() returns 10, whereas the second call to pop() would result in stack underflow and the program returns -999.
10.
What does ‘stack underflow’ refer to?
Answer: Option 'C'
Removing items from an empty stack is termed as stack underflow.