Collections and streams: Java 25 practice questions

Collections and streams is 14% of the free Java 25 mock (7 of 50 questions). Pick an answer to see right away whether it is correct, with the explanation and the documentation section it comes from.

J.IV · Java 25 practice question

A developer accidentally inserts a Map into itself as a value. According to the Java SE 25 Map specification, what may happen when calling equals(), hashCode(), toString(), or clone() on that map?

Choose one answer
Check answer and explanation

Correct answer: The operation may fail with an exception because most implementations do not handle self-referential recursive traversal.

Explanation

Recursive traversal methods such as clone(), equals(), hashCode(), and toString() may fail with an exception on self-referential maps, as most current implementations do not handle that scenario.

Common trap

While some implementations like AbstractMap.toString() guard against self-reference, the Map specification states that most implementations do not handle self-referential scenarios in general. Map.put() does not perform cycle detection, so self-referential insertion itself does not fail with IllegalArgumentException.

Source: Java SE 25 API: Map
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.IV · Java 25 practice question

According to JEP 431, what defines a sequenced collection?

Choose one answer
Check answer and explanation

Correct answer: A Collection with a defined encounter order, having first and last elements with successors and predecessors between them.

Explanation

JEP 431 defines a sequenced collection as a Collection whose elements have a defined encounter order, with first and last elements and successors and predecessors between them.

Common trap

Sorted collections like TreeSet are sequenced, but insertion-ordered collections like ArrayList and LinkedHashSet are also sequenced without being sorted. Random access by index is not required; LinkedList, ArrayDeque, and LinkedHashSet are sequenced collections without index-based random access.

Source: JEP 431: Sequenced Collections — Description
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.IV · Java 25 practice question

When the built-in operations on a Stream do not provide the custom traversal behavior a developer needs, which two BaseStream operations can be used for controlled traversal?

Choose one answer
Check answer and explanation

Correct answer: BaseStream.iterator() and BaseStream.spliterator()

Explanation

The Java SE 25 Stream documentation specifies that BaseStream.iterator() and BaseStream.spliterator() can be used to perform a controlled traversal when standard stream operations are insufficient.

Common trap

forEach() and toArray() are terminal operations on Stream rather than BaseStream escape hatches for controlled traversal. sequential() and unordered() only configure stream execution characteristics and do not provide element-by-element traversal.

Source: Java SE 25 API: Stream
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.IV · Java 25 practice question

A developer calls contains(null) on a List implementation that prohibits null elements. According to the Java SE 25 List specification, what behavior is permitted?

Choose one answer
Check answer and explanation

Correct answer: It may either throw an exception or simply return false, depending on the implementation.

Explanation

The List specification states that querying the presence of an ineligible element may either throw an exception or simply return false, as different implementations exhibit different behavior.

Common trap

Returning false seems intuitive because the element cannot be present, but the specification explicitly allows throwing an exception. Although List.of throws NullPointerException on contains(null), the List contract does not mandate throwing for all null-prohibiting implementations.

Source: Java SE 25 API: List
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.IV · Java 25 practice question

A developer is deciding whether to expose a Collection or a Stream from a repository method. According to the Java SE 25 API, what is the primary difference in their goals?

Choose one answer
Check answer and explanation

Correct answer: Collections focus on managing and accessing elements, whereas streams declaratively describe a source and aggregate operations on it.

Explanation

Collections are designed for efficient management of and access to elements, while streams declaratively describe their source and the aggregate computations performed on it without providing direct element manipulation.

Common trap

Many developers associate streams with parallelism, but streams support both sequential and parallel operations rather than being exclusively parallel. Standard collections such as ArrayList are mutable by default, and streams explicitly do not provide direct element manipulation.

Source: Java SE 25 API: Stream
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.IV · Java 25 practice question

How did JEP 431 retrofit SequencedCollection, SequencedSet, and SequencedMap into the existing Java collections hierarchy without breaking existing custom implementations?

Choose one answer
Check answer and explanation

Correct answer: Every new method declared in the sequenced interfaces provides a default implementation.

Explanation

JEP 431 gives all newly declared methods in the sequenced collection interfaces default implementations so existing subclasses continue to compile and work.

Common trap

The sequenced interfaces declare concrete operations such as reversed(), getFirst(), and getLast(), so they are not marker interfaces. Java has no @Sequenced annotation for opting into interface retrofitting.

Source: JEP 431: Sequenced Collections — Description
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.IV · Java 25 practice question

Which of the following groups contains only known subinterfaces of SequencedCollection<E> in Java SE 25?

Choose one answer
Check answer and explanation

Correct answer: List<E>, Deque<E>, and SortedSet<E>

Explanation

List<E>, Deque<E>, and SortedSet<E> are all retrofitted as subinterfaces of SequencedCollection<E> because they have a defined encounter order.

Common trap

General Set<E> and Queue<E> do not guarantee a defined encounter order and are not subinterfaces of SequencedCollection<E>. Map<K, V> does not extend Collection or SequencedCollection; sequenced maps use the separate SequencedMap interface.

Source: Java SE 25 API: SequencedCollection
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.IV · Java 25 practice question

What happens when the following Java 25 program is compiled and run?

import java.util.List;

void main() {
    List<String> items = List.of("alpha", null);
    IO.println(items.size());
}
Choose one answer
Check answer and explanation

Correct answer: It throws NullPointerException at runtime.

Explanation

List implementations that prohibit null elements throw an unchecked exception—typically NullPointerException—when an ineligible null element is supplied, and List.of prohibits nulls.

Common trap

ArrayList allows null elements and would have size 2, which makes this tempting if you forget that List.of prohibits nulls. The null prohibition is enforced at runtime by the factory method, not as a compile-time error.

Source: Java SE 25 API: List
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.IV · Java 25 practice question

What does the following Java 25 program print?

import java.util.*;
import java.util.stream.*;

void main() {
    Map<Integer, List<String>> grouped = Stream.of("a", "bb", "c")
        .collect(Collectors.groupingBy(String::length));
    IO.println(grouped.get(1));
}
Choose one answer
Check answer and explanation

Correct answer: [a, c]

Explanation

Collectors.groupingBy(String::length) groups input strings by their length into a Map<Integer, List<String>>, so key 1 maps to the list [a, c].

Common trap

This would only occur if all elements mapped to key 1, ignoring that "bb" has length 2. This is the list mapped to key 2, not key 1.

Source: Java SE 25 API: Collectors.groupingBy
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.IV · Java 25 practice question

What does the following Java 25 program print when the stream has fewer elements than the window size?

import java.util.stream.*;

void main() {
    var windows = Stream.of("x", "y")
        .gather(Gatherers.windowSliding(5))
        .toList();
    IO.println(windows);
}
Choose one answer
Check answer and explanation

Correct answer: [[x, y]]

Explanation

When a non-empty stream is smaller than the requested window size, Gatherers.windowSliding emits a single window containing all elements in the stream.

Common trap

It is tempting to assume no full window can be formed, but the API specifies emitting one window containing all available elements. windowSliding never pads incomplete windows with null values.

Source: Java SE 25 API: Gatherers.windowSliding
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.IV · Java 25 practice question

What does the following Java 25 program print using Gatherers.windowSliding?

import java.util.stream.*;

void main() {
    var windows = Stream.of(1, 2, 3, 4)
        .gather(Gatherers.windowSliding(3))
        .toList();
    IO.println(windows);
}
Choose one answer
Check answer and explanation

Correct answer: [[1, 2, 3], [2, 3, 4]]

Explanation

Gatherers.windowSliding(3) produces overlapping windows of size 3, dropping the least recent element and adding the next element for each subsequent window: [1, 2, 3] then [2, 3, 4].

Common trap

Non-overlapping chunks of size 3 with a remainder of [4] is the behavior of Gatherers.windowFixed(3), not windowSliding(3). Three windows of size 2 would be produced by windowSliding(2), not windowSliding(3).

Source: Java SE 25 API: Gatherers.windowSliding
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.IV · Java 25 practice question

What does the following Java 25 program print using Stream and IntStream aggregate operations?

import java.util.List;

record Widget(String color, int weight) {}

void main() {
    List<Widget> widgets = List.of(
        new Widget("RED", 10),
        new Widget("BLUE", 25),
        new Widget("RED", 15)
    );
    int sum = widgets.stream()
        .filter(w -> w.color().equals("RED"))
        .mapToInt(Widget::weight)
        .sum();
    IO.println(sum);
}
Choose one answer
Check answer and explanation

Correct answer: 25

Explanation

The pipeline filters for RED widgets (weights 10 and 15), converts them to an IntStream with mapToInt, and sums them to produce 25.

Common trap

50 is the total weight of all widgets including the BLUE widget that filter() removes. 10 is only the weight of the first RED widget rather than the sum of all matching widgets.

Source: Java SE 25 API: Stream
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.IV · Java 25 practice question

Which set of static factory methods on java.util.Map is documented as the convenient way to create unmodifiable maps in Java SE 25?

Choose one answer
Check answer and explanation

Correct answer: Map.of, Map.ofEntries, and Map.copyOf

Explanation

The Java SE 25 Map API explicitly lists Map.of, Map.ofEntries, and Map.copyOf as the static factory methods for creating unmodifiable maps.

Common trap

Map.fromEntries and Map.unmodifiable do not exist; Collections.unmodifiableMap is a separate wrapper utility. Map.create and Map.cloneOf are invented names not present in java.util.Map.

Source: Java SE 25 API: Map
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.IV · Java 25 practice question

A developer wants to assign a standard Set implementation to a SequencedCollection<String> variable. Which class compiles without a cast?

import java.util.*;

void main() {
    SequencedCollection<String> sc = new LinkedHashSet<>();
    sc.add("first");
    IO.println(sc.size());
}
Choose one answer
Check answer and explanation

Correct answer: LinkedHashSet

Explanation

LinkedHashSet is a known implementing class of SequencedCollection (via SequencedSet), so it can be assigned directly to SequencedCollection<String>.

Common trap

HashSet has no defined encounter order and does not implement SequencedCollection. EnumSet is not listed as an implementing class of SequencedCollection and also cannot be instantiated with new.

Source: Java SE 25 API: SequencedCollection
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


Back to all topics