Consider a generic Stack class implemented using linked list with the following methods:
public boolean isEmpty(), which returns true if and only if the stack is empty;
public T pop() throws StackUnderflowException, which removes and returns the top element of the stack (if the stack is empty, it throws StackUnderflowException);
public T peek() throws StackUnderflowException, which returns the top element of the stack (but does not remove it; it throws exception if stack is empty);
public void push(T element), which pushes an element onto the stack
Predict the output of each of the following code snippets
(a) Stack<Integer> s = new Stack<Integer>();
try{
s.push(5);
s.push(10);
System.out.println(s.peek());
System.out.println(s.pop());
s.push(20);
System.out.println(s.pop());
}
catch(Exception e) { System.out.print(“An exception was thrown”);
}
(b) Stack<Integer> s = new
Stack<Integer>();
try{
s.push(5);
s.push(50);
System.out.println(s.pop());
System.out.println(s.pop());
System.out.println(s.peek());
s.push(30);
}
catch(Exception e) { System.out.println(“An exception was thrown”);
}
(c) Stack<Integer> s = new Stack<Integer>();
try{
s.push(5);
System.out.println(s.pop());
System.out.println(s.isEmpty());
s.push(10);
s.push(43);
s.push(s.pop());
System.out.println(s.peek());
} catch(Exception e) {
System.out.println(“An exception was thrown”);
}
(d) Stack<Integer> s = new Stack<Integer>();
try {
s.push(5);
s.push(10);
while(!s.isEmpty())
{
System.out.println(s.peek());
}
}
catch(Exception e) { System.out.println(“An exception was thrown”);
}
(a) Stack<Integer> s = new Stack<Integer>();
try{
s.push(5); // Stores value 5 on to stack. Now Stack contains:
5
s.push(10); // Stores value 10 on to stack. Now Stack contains: 5
10
System.out.println(s.peek()); // Prints element at top of the stack
which is 10 => Prints value 10
System.out.println(s.pop()); // Prints element at top of the stack
which is 10 and removes it from stack. Now stack contains: 5 =>
Prints value 10
s.push(20); // Now stack contains: 5 20
System.out.println(s.pop()); // Prints element at top of the stack
which is 20 and removes it from stack. Now stack contains: 5 =>
Prints value 20
}
catch(Exception e) { System.out.print(“An exception was thrown”);
}
Hence output will be:
10
10
20
______________________________________________________________________________________________________________________________________________________
(b) Stack<Integer> s = new Stack<Integer>();
try{
s.push(5); // Stores value 5 on to stack. Now Stack contains:
5
s.push(50); // Stores value 50 on to stack. Now Stack contains: 5
50
System.out.println(s.pop()); // Prints element at top of the stack
which is 50 and removes it from stack. Now stack contains: 5 =>
Prints value 50
System.out.println(s.pop()); // Prints element at top of the stack
which is 5 and removes it from stack. Now stack contains: EMPTY
=> Prints value 5
System.out.println(s.peek()); // As stack is empty Raises an
exception and prints the exception
s.push(30);
}
catch(Exception e) { System.out.println(“An exception was thrown”);
}
Hence output will be:
50
5
An exception was thrown
______________________________________________________________________________________________________________________________________________________
(c) Stack<Integer> s = new Stack<Integer>();
try{
s.push(5); // Stores value 5 on to stack. Now Stack contains:
5
System.out.println(s.pop()); // Prints element at top of the stack
which is 5 and removes it from stack. Now stack contains: EMPTY
=> Prints value 5
System.out.println(s.isEmpty()); // As stack is empty prints
true
s.push(10); // Stores value 10 on to stack. Now Stack contains:
10
s.push(43); // Stores value 43 on to stack. Now Stack contains: 10
43
s.push(s.pop()); // Pushes element present at top of stack which is
43 => Stack contains: 10 43
System.out.println(s.peek()); // Element present at top of the
stack is 43 => Prints value 43
} catch(Exception e) {
System.out.println(“An exception was thrown”);
}
Hence output will be:
5
true
43
______________________________________________________________________________________________________________________________________________________
(d) Stack<Integer> s = new Stack<Integer>();
try {
s.push(5); // Stores value 5 on to stack. Now Stack contains:
5
s.push(10); // Stores value 10 on to stack. Now Stack contains: 5
10
while(!s.isEmpty())
{
System.out.println(s.peek()); // Prints element 10 which is at top
of the stack infinite times
}
}
catch(Exception e) { System.out.println(“An exception was thrown”);
}
Hence output will be:
10
10
10
.. Infinite Times
Get Answers For Free
Most questions answered within 1 hours.