Advanced Topics MCQ

Code Tracing: Stacks and Queues

Practice mode shows one question at a time and lets students check each answer before moving on.

All AT CS Tests

For practice use only.

Code Tracing: Stacks and Queues

Code Tracing: Stacks and Queues with immediate answer checks and explanations for Advanced Topics in Computer Science.

Question 1 of 25

Answered 0 of 25

Choose one answer.

Trace the stack operations. What is printed 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);
    }
}