What is the output of the following program? [code] public class Stack { protected static final int CAPACITY = 100; protected int size,top

Thành Đạt

New member
What is the output of the following program?
Code:
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
 

Tìm kiếm

Back
Top