LearnFrenzy
Sign in Take a free test

Data Structures in Java · Exercise 2 of 4

Data Structures in Java Data Structures in Java Output Questions

  • 20 questions
  • Page 1 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Twenty predict-the-output programs on data structures in Java – stacks, queues and deques, PriorityQueue, linked-list pointers, tree traversals, BFS and DFS orders, circular indexes and hashing. Work out the output, then run the code to check.

Question 1 Easy

What is the output of the following Java program?

Main.java
import java.util.ArrayDeque;
import java.util.Deque;

public class Main {
    public static void main(String[] args) {
        Deque<Integer> st = new ArrayDeque<>();
        st.push(1); st.push(2); st.push(3);
        st.pop();
        st.push(4); st.push(5);
        st.pop();
        System.out.println(st.peek() + " " + st);
    }
}
A 4 [4, 2, 1]
B 4 [1, 2, 4]
C 5 [5, 4, 2, 1]
D 2 [4, 2, 1]
Question page & discussion
Question 2 Easy

What is the output of the following Java program?

Main.java
import java.util.ArrayDeque;
import java.util.Queue;

public class Main {
    public static void main(String[] args) {
        Queue<String> q = new ArrayDeque<>();
        q.offer("A"); q.offer("B"); q.offer("C");
        q.poll();
        q.offer("D");
        q.offer(q.poll());
        System.out.println(q);
    }
}
A [B, C, D]
B [C, D, B]
C [D, C, B]
D [C, B, D]
Question page & discussion
Question 3 Medium

What is the output of the following Java program?

Main.java
import java.util.ArrayDeque;
import java.util.Deque;

public class Main {
    public static void main(String[] args) {
        Deque<Integer> dq = new ArrayDeque<>();
        dq.offerFirst(1);
        dq.offerLast(2);
        dq.offerFirst(3);
        dq.offerLast(4);
        System.out.println(dq + " " + dq.pollLast() + " " + dq.pollFirst() + " " + dq);
    }
}
A [1, 2, 3, 4] 4 1 [2, 3]
B [3, 1, 2, 4] 3 4 [1, 2]
C [3, 1, 2, 4] 4 3 [1, 2]
D [4, 2, 1, 3] 3 4 [2, 1]
Question page & discussion
Question 4 Hard

What is the output of the following Java program?

Main.java
import java.util.PriorityQueue;

public class Main {
    public static void main(String[] args) {
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        for (int x : new int[]{5, 1, 4, 2, 3}) pq.add(x);
        System.out.println(pq);
        pq.poll();
        System.out.println(pq);
    }
}
A [1, 2, 3, 4, 5] [2, 3, 4, 5]
B [1, 2, 4, 5, 3] [2, 3, 4, 5]
C [5, 1, 4, 2, 3] [1, 4, 2, 3]
D [1, 2, 4, 5, 3] [3, 2, 4, 5]
Question page & discussion
Question 5 Medium

What is the output of the following Java program?

Main.java
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;

public class Main {
    public static void main(String[] args) {
        PriorityQueue<String> pq = new PriorityQueue<>(
                Comparator.comparing(String::length).thenComparing(Comparator.reverseOrder()));
        pq.addAll(List.of("fig", "kiwi", "apple", "date", "pear"));
        StringBuilder sb = new StringBuilder();
        while (!pq.isEmpty()) sb.append(pq.poll()).append(' ');
        System.out.println(sb.toString().trim());
    }
}
A fig date kiwi pear apple
B apple pear kiwi date fig
C fig kiwi date pear apple
D fig pear kiwi date apple
Question page & discussion
Question 6 Medium

What is the output of the following Java program?

Main.java
public class Main {
    static class Node {
        int val;
        Node next;
        Node(int val, Node next) { this.val = val; this.next = next; }
    }

    static Node reverse(Node head) {
        if (head == null || head.next == null) return head;
        Node rest = reverse(head.next);
        head.next.next = head;
        head.next = null;
        return rest;
    }

    public static void main(String[] args) {
        Node head = new Node(1, new Node(2, new Node(3, new Node(4, null))));
        Node r = reverse(head);
        StringBuilder sb = new StringBuilder();
        for (Node n = r; n != null; n = n.next) sb.append(n.val);
        System.out.println(sb + " " + head.val + " " + (head.next == null));
    }
}
A 4321 1 true
B 4321 4 false
C 1234 1 false
D 4321 1 false
Question page & discussion
Question 7 Medium

What is the output of the following Java program?

Main.java
public class Main {
    static class Node {
        int val;
        Node next;
        Node(int val, Node next) { this.val = val; this.next = next; }
    }

    static Node build(int n) {
        Node head = null;
        for (int i = n; i >= 1; i--) head = new Node(i * 10, head);
        return head;
    }

    static int middle(Node head) {
        Node slow = head, fast = head;
        while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; }
        return slow.val;
    }

    public static void main(String[] args) {
        System.out.println(middle(build(5)) + " " + middle(build(6)) + " " + middle(build(1)));
    }
}
A 30 30 10
B 20 30 10
C 30 40 10
D 30 40 0
Question page & discussion
Question 8 Medium

What is the output of the following Java program?

Main.java
public class Main {
    static class Node {
        int key;
        Node left, right;
        Node(int key) { this.key = key; }
    }

    static Node insert(Node n, int k) {
        if (n == null) return new Node(k);
        if (k < n.key) n.left = insert(n.left, k);
        else n.right = insert(n.right, k);
        return n;
    }

    static void post(Node n, StringBuilder sb) {
        if (n == null) return;
        post(n.left, sb);
        post(n.right, sb);
        sb.append(n.key).append(' ');
    }

    public static void main(String[] args) {
        Node root = null;
        for (int k : new int[]{8, 3, 10, 1, 6, 14, 4}) root = insert(root, k);
        StringBuilder sb = new StringBuilder();
        post(root, sb);
        System.out.println(sb.toString().trim());
    }
}
A 1 3 4 6 8 10 14
B 1 4 6 3 14 10 8
C 8 3 1 6 4 10 14
D 4 6 1 3 14 10 8
Question page & discussion
Question 9 Medium

What is the output of the following Java program?

Main.java
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import java.util.StringJoiner;

public class Main {
    static class Node {
        int key;
        Node left, right;
        Node(int key) { this.key = key; }
    }

    static Node insert(Node n, int k) {
        if (n == null) return new Node(k);
        if (k < n.key) n.left = insert(n.left, k);
        else n.right = insert(n.right, k);
        return n;
    }

    public static void main(String[] args) {
        Node root = null;
        for (int k : new int[]{40, 20, 60, 10, 30, 50, 70, 25}) root = insert(root, k);

        List<String> levels = new ArrayList<>();
        Deque<Node> q = new ArrayDeque<>();
        q.add(root);
        while (!q.isEmpty()) {
            StringJoiner sj = new StringJoiner(" ");
            for (int i = q.size(); i > 0; i--) {
                Node n = q.poll();
                sj.add(String.valueOf(n.key));
                if (n.left != null) q.add(n.left);
                if (n.right != null) q.add(n.right);
            }
            levels.add(sj.toString());
        }
        System.out.println(String.join(" | ", levels));
    }
}
A 40 | 20 60 | 10 25 30 50 70
B 10 | 20 25 30 | 40 50 60 70
C 40 20 10 30 25 60 50 70
D 40 | 20 60 | 10 30 50 70 | 25
Question page & discussion
Question 10 Medium

What is the output of the following Java program?

Main.java
public class Main {
    static class Node {
        int key;
        Node left, right;
        Node(int key) { this.key = key; }
    }

    static Node insert(Node n, int k) {
        if (n == null) return new Node(k);
        if (k < n.key) n.left = insert(n.left, k);
        else n.right = insert(n.right, k);
        return n;
    }

    static int height(Node n) { return n == null ? -1 : 1 + Math.max(height(n.left), height(n.right)); }
    static int leaves(Node n) {
        if (n == null) return 0;
        if (n.left == null && n.right == null) return 1;
        return leaves(n.left) + leaves(n.right);
    }
    static int count(Node n) { return n == null ? 0 : 1 + count(n.left) + count(n.right); }

    public static void main(String[] args) {
        Node root = null;
        for (int k : new int[]{50, 30, 70, 20, 40, 80, 45}) root = insert(root, k);
        System.out.println("height=" + height(root) + " leaves=" + leaves(root) + " nodes=" + count(root));
    }
}
A height=4 leaves=3 nodes=7
B height=3 leaves=3 nodes=7
C height=3 leaves=4 nodes=7
D height=2 leaves=3 nodes=7
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links