Skip to main content

Advanced Topics MCQ

Code Tracing: Stacks and Queues

All AT CS Tests

Code Tracing: Stacks and Queues

Question 1 of 25

Answered 0 of 25

Choose one answer.

What stack contents are printed, listed from bottom to top?

import java.util.Stack;

public class StackTrace
{
    public static void main(String[] args)
    {
        int[] data = {4, 9, 2, 7, 6, 3};
        Stack<Integer> stack = new Stack<Integer>();
        for (int value : data)
        {
            if (value % 2 == 0) stack.push(value + 5);
            else if (!stack.empty() && stack.peek() > value)
                stack.push(stack.pop() - value);
            else stack.push(value);
        }
        System.out.println(stack);
    }
}
Choose one answer.