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 2 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 11 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.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        Map<String, List<String>> g = new HashMap<>();
        String[][] edges = {{"A", "C"}, {"A", "B"}, {"C", "D"}, {"B", "E"}, {"D", "F"}};
        for (String[] e : edges) g.computeIfAbsent(e[0], k -> new ArrayList<>()).add(e[1]);

        List<String> order = new ArrayList<>();
        Set<String> seen = new HashSet<>(Set.of("A"));
        Deque<String> q = new ArrayDeque<>(List.of("A"));
        while (!q.isEmpty()) {
            String u = q.poll();
            order.add(u);
            for (String v : g.getOrDefault(u, List.of())) if (seen.add(v)) q.add(v);
        }
        System.out.println(order);
    }
}
A [A, C, B, D, E, F]
B [A, C, D, F, B, E]
C [A, B, C, D, E, F]
D [A, B, E, C, D, F]
Question page & discussion
Question 12 Medium

What is the output of the following Java program?

Main.java
public class Main {
    public static void main(String[] args) {
        int cap = 5, head = 3;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 7; i++) sb.append((head + i) % cap);
        System.out.println(sb + " " + (0 - 1 + cap) % cap + " " + (-1 % cap));
    }
}
A 3456789 4 4
B 3401234 4 4
C 3401234 4 -1
D 3401234 -1 -1
Question page & discussion
Question 13 Hard

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.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        Map<String, List<String>> g = new HashMap<>();
        String[][] edges = {{"A", "C"}, {"A", "B"}, {"C", "D"}, {"B", "E"}, {"D", "F"}};
        for (String[] e : edges) g.computeIfAbsent(e[0], k -> new ArrayList<>()).add(e[1]);

        List<String> order = new ArrayList<>();
        Set<String> seen = new HashSet<>();
        Deque<String> stack = new ArrayDeque<>();
        stack.push("A");
        while (!stack.isEmpty()) {
            String u = stack.pop();
            if (!seen.add(u)) continue;
            order.add(u);
            for (String v : g.getOrDefault(u, List.of())) stack.push(v);
        }
        System.out.println(order);
    }
}
A [A, C, B, D, E, F]
B [A, C, D, F, B, E]
C [A, B, C, D, E, F]
D [A, B, E, C, D, F]
Question page & discussion
Question 14 Medium

What is the output of the following Java program?

Main.java
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        int capacity = 10, size = 0;
        List<Integer> grows = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            if (size == capacity) {
                capacity = capacity + (capacity >> 1);
                grows.add(capacity);
            }
            size++;
        }
        System.out.println(grows + " final=" + capacity);
    }
}
A [20, 40, 80] final=80
B [15, 22, 33, 49, 73] final=73
C [15, 22, 33, 49] final=49
D [15, 23, 35, 53] final=53
Question page & discussion
Question 15 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) {
        String expr = "5 1 2 + 4 * + 3 -";
        Deque<Integer> st = new ArrayDeque<>();
        for (String tok : expr.split(" ")) {
            switch (tok) {
                case "+" -> st.push(st.pop() + st.pop());
                case "*" -> st.push(st.pop() * st.pop());
                case "-" -> { int b = st.pop(), a = st.pop(); st.push(a - b); }
                default -> st.push(Integer.parseInt(tok));
            }
        }
        System.out.println(st.pop() + " " + st.isEmpty());
    }
}
A 14 false
B 20 true
C 14 true
D -14 true
Question page & discussion
Question 16 Medium

What is the output of the following Java program?

Main.java
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        int buckets = 8;
        Map<Integer, List<Integer>> table = new TreeMap<>();
        for (int key : new int[]{3, 11, 19, 4, 12, 27, 8})
            table.computeIfAbsent(Integer.hashCode(key) % buckets, b -> new ArrayList<>()).add(key);
        System.out.println(table);
    }
}
A {3=[3], 4=[4], 8=[8], 11=[11], 12=[12], 19=[19], 27=[27]}
B {0=[8], 3=[3, 11, 19], 4=[4, 12], 7=[27]}
C {0=[8], 3=[27, 19, 11, 3], 4=[12, 4]}
D {0=[8], 3=[3, 11, 19, 27], 4=[4, 12]}
Question page & discussion
Question 17 Hard

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 meet(Node head) {
        Node s = head, f = head;
        while (f != null && f.next != null) {
            s = s.next;
            f = f.next.next;
            if (s == f) return s;
        }
        return null;
    }

    public static void main(String[] args) {
        Node[] n = new Node[5];
        for (int i = 4; i >= 0; i--) n[i] = new Node(i + 1, i == 4 ? null : n[i + 1]);
        Node m1 = meet(n[0]);
        n[4].next = n[2];                    // 5 -> 3 makes a loop
        Node m2 = meet(n[0]);
        System.out.println((m1 == null) + " " + (m2 == null ? "none" : "meet at " + m2.val));
    }
}
A true meet at 3
B true meet at 4
C false meet at 5
D true none
Question page & discussion
Question 18 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<int[]> st = new ArrayDeque<>();          // each entry: {value, minimum so far}
        for (int x : new int[]{5, 3, 7, 2, 8})
            st.push(new int[]{x, st.isEmpty() ? x : Math.min(x, st.peek()[1])});

        StringBuilder sb = new StringBuilder();
        sb.append(st.peek()[1]);
        st.pop();
        st.pop();
        sb.append(' ').append(st.peek()[1]).append(' ').append(st.peek()[0]);
        st.pop();
        sb.append(' ').append(st.peek()[1]);
        System.out.println(sb);
    }
}
A 2 3 7 3
B 2 2 7 3
C 2 3 3 5
D 8 3 7 3
Question page & discussion
Question 19 Medium

What is the output of the following Java program?

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

public class Main {
    public static void main(String[] args) {
        Stack<Integer> legacy = new Stack<>();
        Deque<Integer> modern = new ArrayDeque<>();
        for (int i = 1; i <= 3; i++) { legacy.push(i); modern.push(i); }
        System.out.println(legacy + " " + modern + " " + legacy.peek() + " " + modern.peek());
    }
}
A [3, 2, 1] [3, 2, 1] 3 3
B [1, 2, 3] [1, 2, 3] 3 1
C [1, 2, 3] [3, 2, 1] 3 3
D [3, 2, 1] [1, 2, 3] 3 3
Question page & discussion
Question 20 Hard

What is the output of the following Java program?

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

public class Main {
    public static void main(String[] args) {
        int[] a = {4, 5, 2, 25, 7, 18};
        int[] next = new int[a.length];
        Arrays.fill(next, -1);
        Deque<Integer> st = new ArrayDeque<>();          // indexes still waiting for a greater value
        for (int i = 0; i < a.length; i++) {
            while (!st.isEmpty() && a[st.peek()] < a[i]) next[st.pop()] = a[i];
            st.push(i);
        }
        System.out.println(Arrays.toString(next));
    }
}
A [5, 25, 7, 18, 18, -1]
B [5, 25, 25, 18, 18, -1]
C [-1, 4, 5, 5, 25, 25]
D [5, 25, 25, -1, 18, -1]
Question page & discussion
Up next · Exercise 3Data Structures in Java – Find the Error10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links