Java OCA OCP Practice Question 31

Question

What output does the following program display?

import java.util.EmptyStackException;
import java.util.Stack;

public class Main {
   public static void main(String args[]) {
      String s1 = "abc";
      String s2 = "def";
      Stack<String> stack = new Stack<>();
      stack.push(s1);//www .j av a 2  s  . c  o m
      stack.push(s2);
      try {
         String s3 = stack.pop() + stack.pop();
         System.out.println(s3);
      } catch (EmptyStackException ex) {
      }
   }
}
  • A. abcdef
  • B. defabc
  • C. abcabc
  • D. defdef


B.

Note

Stacks are last-in first out.




PreviousNext

Related