Machine-translated by Google. Questions are authored in English.
Top

Discussion

What will be the output of the program?

import java.util.*;

public class Item34
{
    public static void main(String[] args)
    {
        Deque<String> stack = new ArrayDeque<String>();
        stack.push("a");
        stack.push("b");
        System.out.println(stack.pop() + stack.peek());
    }
}

  • A.The program prints ab
  • B.The program prints ba
  • C.The program prints aa
  • D.The program prints bb

Answer: B

push and pop both work at the head of a Deque, so this behaves as a stack: b goes on top and comes off first. peek then reports the new head without removing it, which is a. The result is therefore ba, last-in-first-out.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.