QuizzesJava Stack & Queue
intermediate
java

Java Stack & Queue

Test your knowledge of Stack, Queue, Deque, ArrayDeque, PriorityQueue, and their real-world applications in Java.

25 minutes 20 questions Pass at 70%
Time Limit
25 minutes - timer starts when you begin
Change Answers Freely
Revisit and change any answer before submitting
Full Review After Submit
Correct answers and explanations shown for every question
Auto-submit on timeout
Quiz submits automatically when time runs out

Questions in this quiz

20 questions on Java Stack & Queue. Work through them here, then start the quiz to answer them and see your score.

  1. 1.What is the defining property of a Stack data structure?

    • AElements are always sorted
    • BLast In First Out (LIFO)
    • CFirst In First Out (FIFO)
    • DRandom access by index
  2. 2.What class does Java recommend for Stack behaviour today?

    • Ajava.util.LinkedList
    • Bjava.util.Vector
    • Cjava.util.ArrayDeque used as a stack
    • Djava.util.Stack
  3. 3.What is the defining property of a Queue data structure?

    • ADouble-ended access
    • BLast In First Out (LIFO)
    • CElements sorted by priority
    • DFirst In First Out (FIFO)
  4. 4.What does this code print?

    import java.util.*;
    public class Main {
        public static void main(String[] args) {
            Deque<String> stack = new ArrayDeque<>();
            stack.push("first");
            stack.push("second");
            stack.push("third");
            System.out.println(stack.pop());
            System.out.println(stack.peek());
            System.out.println(stack.size());
        }
    }
    • Athird second 2
    • Bfirst first 3
    • Cthird first 1
    • Dfirst second 2
  5. 5.What is a Deque in Java?

    • AA thread-safe queue for concurrent use
    • BA double-ended queue that allows insertion and removal at both ends
    • CA queue that only allows unique elements
    • DA sorted queue backed by a tree
  6. 6.What is the difference between poll() and remove() on a Queue?

    • AThey are identical
    • Bremove() is faster; poll() is safer
    • Cpoll() removes from the tail; remove() removes from the head
    • Dpoll() returns null if the queue is empty; remove() throws NoSuchElementException
  7. 7.What does this code print?

    import java.util.*;
    public class Main {
        public static void main(String[] args) {
            Queue<Integer> queue = new ArrayDeque<>();
            queue.offer(10);
            queue.offer(20);
            queue.offer(30);
            System.out.println(queue.poll());
            System.out.println(queue.peek());
            System.out.println(queue.size());
        }
    }
    • A10 10 3
    • B10 20 2
    • C30 30 2
    • D30 20 2
  8. 8.What is a PriorityQueue in Java?

    • AA Queue that always dequeues the smallest (or highest-priority) element first, backed by a min-heap
    • BA Queue that maintains insertion order but sorts on output
    • CA Queue that processes elements in FIFO order with priority labels
    • DA thread-safe sorted queue
  9. 9.What does this code print?

    import java.util.*;
    public class Main {
        public static void main(String[] args) {
            PriorityQueue<Integer> pq = new PriorityQueue<>();
            pq.offer(50);
            pq.offer(10);
            pq.offer(30);
            pq.offer(20);
            while (!pq.isEmpty()) {
                System.out.print(pq.poll() + " ");
            }
        }
    }
    • A10 30 20 50
    • B10 20 30 50
    • C50 10 30 20
    • D50 30 20 10
  10. 10.How do you create a max-heap PriorityQueue in Java?

    • Anew PriorityQueue<>(Comparator.reverseOrder())
    • Bnew PriorityQueue<>(Collections.maxOrder())
    • CPriorityQueue.maxHeap()
    • Dnew PriorityQueue<>(true)
  11. 11.What does this code print?

    import java.util.*;
    public class Main {
        public static void main(String[] args) {
            Deque<Integer> deque = new ArrayDeque<>();
            deque.addFirst(1);
            deque.addLast(2);
            deque.addFirst(0);
            deque.addLast(3);
            System.out.println(deque);
            System.out.println(deque.peekFirst());
            System.out.println(deque.peekLast());
        }
    }
    • A[0, 1, 3, 2] 0 2
    • B[1, 2, 0, 3] 1 3
    • C[1, 0, 2, 3] 1 3
    • D[0, 1, 2, 3] 0 3
  12. 12.What is the time complexity of offer() and poll() in a PriorityQueue?

    • AO(n) for both
    • BO(log n) for both
    • CO(1) offer, O(n) poll
    • DO(1) for both
  13. 13.What is a common use case for a Stack in algorithm problems?

    • AShortest path in a weighted graph
    • BBreadth-first search (BFS)
    • CFinding the minimum element in a list
    • DMatching parentheses and evaluating expressions
  14. 14.What does this code print?

    import java.util.*;
    public class Main {
        static boolean isBalanced(String s) {
            Deque<Character> stack = new ArrayDeque<>();
            for (char c : s.toCharArray()) {
                if (c == '(') stack.push(c);
                else if (c == ')') {
                    if (stack.isEmpty()) return false;
                    stack.pop();
                }
            }
            return stack.isEmpty();
        }
        public static void main(String[] args) {
            System.out.println(isBalanced("(()())"));
            System.out.println(isBalanced("((())"));
        }
    }
    • Afalse true
    • Bfalse false
    • Ctrue false
    • Dtrue true
  15. 15.What is a LinkedList's role in the Java collections hierarchy for queues?

    • ALinkedList implements both Deque and List, making it usable as a queue or stack
    • BLinkedList only implements List, not Queue
    • CLinkedList is faster than ArrayDeque for all queue operations
    • DLinkedList is the only class that implements Queue
  16. 16.What does this code print?

    import java.util.*;
    public class Main {
        public static void main(String[] args) {
            PriorityQueue<String> pq = new PriorityQueue<>(Comparator.comparingInt(String::length));
            pq.offer("banana");
            pq.offer("fig");
            pq.offer("kiwi");
            pq.offer("apple");
            System.out.println(pq.poll());
            System.out.println(pq.poll());
        }
    }
    • Abanana apple
    • Bfig kiwi
    • Cfig apple
    • Dapple fig
  17. 17.What is a BlockingQueue and when is it used?

    • AA thread-safe queue where put() blocks when full and take() blocks when empty — used for producer-consumer patterns
    • BA queue that prevents all modification
    • CA queue backed by a database for persistent storage
    • DA queue that blocks the JVM thread scheduler
  18. 18.What does this code print?

    import java.util.*;
    public class Main {
        public static void main(String[] args) {
            Deque<Integer> deque = new ArrayDeque<>(Arrays.asList(1, 2, 3, 4, 5));
            deque.pollFirst();
            deque.pollLast();
            System.out.println(deque);
        }
    }
    • A[2, 3, 4, 5]
    • B[1, 2, 3, 4]
    • C[2, 3, 4]
    • D[1, 2, 3]
  19. 19.Which collection should you use for a BFS (Breadth-First Search) traversal?

    • ATreeSet for sorted visited nodes
    • BPriorityQueue sorted by node value
    • CQueue / ArrayDeque used as a queue
    • DStack / ArrayDeque used as a stack
  20. 20.What is the time and space complexity of ArrayDeque vs LinkedList for queue operations?

    • AArrayDeque is O(n) for offer; LinkedList is O(1)
    • BArrayDeque is O(log n); LinkedList is O(1)
    • CLinkedList is faster because no array resizing
    • DBoth O(1) time, but ArrayDeque uses less memory per element