Introduction To Data Structures - Study Mode
[#191] What does the following function do? public Object some_func()throws emptyStackException
{
if(isEmpty())
throw new emptyStackException("underflow")
return first.getEle()
}
Correct Answer
(C) retrieve the top-of-the-stack element
[#192] 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)
}
}
Correct Answer
(D) -999
[#193] What is the functionality of the following piece of code? public int function()
{
Node temp = tail.getPrev()
tail.setPrev(temp.getPrev())
temp.getPrev().setNext(tail)
size--
return temp.getItem()
}
Correct Answer
(D) Return the last but one element at the tail of the list and remove it from the list
[#194] How do you insert a node at the beginning of the list?
Correct Answer
(A) public class insertFront(int data)
{
Node node = new Node(data, head, head.getNext())
node.getNext().setPrev(node)
head.setNext(node)
size++
}
[#195] Given below is the Node class to perform basic list operations and a Stack class with a no arg constructor. Select from the options the appropriate pop() operation that can be included in the Stack class. Also 'first' is the top-of-the-stack. class Node
{
protected Node next
protected Object ele
Node()
{
this(null,null)
}
Node(Object e,Node n)
{
ele=e
next=n
}
public void setNext(Node n)
{
next=n
}
public void setEle(Object e)
{
ele=e
}
public Node getNext()
{
return next
}
public Object getEle()
{
return ele
}
}
class Stack
{
Node first
int size=0
Stack()
{
first=null
}
}
Correct Answer
(A) public Object pop()
{
if(size == 0)
System.out.println("underflow")
else
{
Object o = first.getEle()
first = first.getNext()
size--
return o
}
}