SaveMyCert
Log in
5 of 5 free questions left today·for 30 a day
1Z0-831 · Domain 6

Processing Data Using Streams and Lambda Expressions practice questions

Processing Data Using Streams and Lambda Expressions is worth 9% of the 1Z0-831 exam — the 5th-heaviest of the 10 domains. Object and primitive streams with lambdas, plus decomposition, concatenation, reduction, grouping, partitioning and gatherers. Oracle publishes no domain weights; weight approximates the objective share, with a floor for every section. 6 fully worked examples are further down this page, answers included.

Exam weight
9%
the 5th-heaviest of the 10 domains
Questions
86
across 2 topics
Free, no account
5/day
a free account allows 30 a day
Explanations
Every option
right and wrong

Build a practice session

5 free questions left today.

Domains

How many?

Mode

Ready when you are

10 fresh questions drawn across 1 of 10 domains, in Learn mode.

Focused review

Every question you answer incorrectly, and every question you flag while practising, is saved here automatically. Finish a session and you can come back to re-drill just those.

6 sample Processing Data Using Streams and Lambda Expressions questions, fully explained

Questions from the 1Z0-831 bank mapped to domain 6, with the answer key and the reasoning behind every option. None of them repeat the examples on the main 1Z0-831 practice page.

Question 1Processing Data Using Streams and Lambda Expressions

Given:import java.util.stream.IntStream; public class Main { public static void main(String[] args) { int a = IntStream.rangeClosed(1, 4).sum(); int b = IntStream.range(1, 4).sum(); System.out.println(a + " " + b); } }What is the result?

Choose one.

  • a
    10 10

    This treats range like rangeClosed. range has an exclusive upper bound, so 4 is not included in b.

  • b
    6 6

    This treats rangeClosed like range. rangeClosed includes its upper bound, so a includes 4.

  • c
    10 6 Correct

    rangeClosed(1, 4) yields 1, 2, 3, 4 (sum 10); range(1, 4) excludes the end bound and yields 1, 2, 3 (sum 6).

  • d
    6 10

    This swaps the two methods: the closed range is the one that includes 4, and it is assigned to a.

The concept

IntStream.range(start, end) excludes end; IntStream.rangeClosed(start, end) includes it.

Why that’s the answer

Both factory methods produce ascending int streams starting at the first argument. range stops before the second argument, matching the familiar for (int i = start; i < end; i++) loop, so range(1, 4) is 1, 2, 3 with sum 6. rangeClosed stops at the second argument, so rangeClosed(1, 4) is 1, 2, 3, 4 with sum 10. sum() on an IntStream returns an int, so both assignments compile.

How to reason it out
  1. Expand rangeClosed(1, 4): 1, 2, 3, 4.
  2. Expand range(1, 4): 1, 2, 3.
  3. Sum each stream: 10 and 6.
  4. Print a then b.

Exam tip: range is half-open like a for loop; rangeClosed includes both ends.

Java Streams, Lambdas and Primitive Streams (1Z0-831) — the lesson that teaches this.

Question 2Processing Data Using Streams and Lambda Expressions

Given:import java.util.function.*; public class Main { public static void main(String[] args) { // insert here if (check.test("java")) { System.out.println("long word"); } } }Which two declarations, inserted at the comment, make the program print long word? (Select TWO.)

Choose TWO.

  • a
    Predicate<String> check = s -> s.length() > 3; Correct

    Predicate<String> declares boolean test(String), so check.test("java") compiles and returns true because java has four characters, and long word is printed.

  • b
    Function<String, Boolean> check = s -> s.length() > 3;

    The lambda fits Function, but Function's method is apply, not test, so check.test(...) does not compile (cannot find symbol).

  • c
    Predicate<String> check = String::isBlank;

    This compiles, but java is not blank, so test returns false and nothing is printed.

  • d
    Predicate<String> check = s -> { return s.contains("av"); }; Correct

    A block body with an explicit return is a valid Predicate lambda, and java contains av, so test returns true and long word is printed.

  • e
    Supplier<Boolean> check = () -> "java".length() > 3;

    Supplier's method is get() and takes no argument, so check.test("java") does not compile.

The concept

Each core functional interface has one abstract method with a fixed name and shape: Predicate.test, Function.apply, Supplier.get, Consumer.accept.

Why that’s the answer

The call check.test("java") needs a type whose abstract method is test taking a String, which is Predicate<String>. Every Predicate declaration compiles, but only the ones whose logic is true for java print long word: the length check and the contains check. Function<String, Boolean> has the right shape but calls its method apply, and Supplier takes no argument. The isBlank reference is a valid Predicate that returns false, so nothing is printed.

How to reason it out
  1. Identify the method the code calls: test with one String argument.
  2. Match it to Predicate<String>; Function uses apply and Supplier uses get.
  3. For each Predicate, evaluate it on java.
  4. Keep the two that both compile and return true, so the if body runs.

Exam tip: Know the method names: Predicate test, Function apply, Supplier get, Consumer accept.

Java Streams, Lambdas and Primitive Streams (1Z0-831) — the lesson that teaches this.

Question 3Processing Data Using Streams and Lambda Expressions

Given:import java.util.List; public class Main { public static void main(String[] args) { List.of("a", "bb", "ccc", "dddd").stream() .map(s -> s.length()) .filter(n -> n % 2 == 1) .forEach(System.out::print); } }What is the result?

Choose one.

  • a
    24

    These are the even lengths. The predicate n % 2 == 1 keeps the elements whose remainder is 1, which are the odd lengths.

  • b
    accc

    This prints the words of odd length, but map has already replaced each word with its length, so the stream holds Integers when filter and forEach run.

  • c
    1234

    This ignores the filter. filter removes the elements for which the predicate is false, so 2 and 4 are dropped.

  • d
    13 Correct

    map turns the words into their lengths 1, 2, 3, 4; filter keeps the odd ones, 1 and 3, and they print with no separator.

The concept

map transforms each element into a new value (possibly a new type); later operations see only the transformed values.

Why that’s the answer

After map(s -> s.length()) the stream is a Stream<Integer> holding 1, 2, 3 and 4; the original strings are gone. filter keeps the elements for which n % 2 == 1 is true, which are 1 and 3. forEach(System.out::print) prints them back to back, giving 13. The accc answer forgets that map replaced the strings; 24 inverts the predicate.

How to reason it out
  1. Apply map: a, bb, ccc, dddd become 1, 2, 3, 4.
  2. Apply filter: keep values with remainder 1, so 1 and 3.
  3. Print each kept value with no separator.
  4. Result: 13.

Exam tip: After map, the pipeline works on the mapped values, not the originals.

Java Streams, Lambdas and Primitive Streams (1Z0-831) — the lesson that teaches this.

Question 4Processing Data Using Streams and Lambda Expressions

Given:import java.util.stream.Stream; public class Main { public static void main(String[] args) { Stream.of("one", "two", "three", "four") .filter(s -> { System.out.print("f" + s.length() + " "); return s.length() > 3; }) .map(s -> { System.out.print("m "); return s.toUpperCase(); }) .findFirst() .ifPresent(System.out::print); } }What is the result?

Choose one.

  • a
    f3 f3 f5 f4 m m THREE

    This processes the stream stage by stage (all filters, then all maps). Streams process elements one at a time, and findFirst short-circuits after the first match.

  • b
    f3 f3 f5 f4 m THREE

    This gets the per-element order partly right but still filters four. findFirst stops pulling elements as soon as three reaches it.

  • c
    f3 f3 f5 THREE

    three passes the filter, so the map lambda runs for it and prints m before findFirst receives THREE.

  • d
    f3 f3 f5 m THREE Correct

    Each element travels the whole pipeline before the next starts. one and two fail the filter, three passes and is mapped, and findFirst then stops the stream, so four is never examined.

The concept

Stream elements flow through the pipeline one at a time, and short-circuiting terminal operations such as findFirst stop pulling elements once they have a result.

Why that’s the answer

A sequential stream does not run filter over the whole source and then map over the survivors. It pulls one element, sends it through filter and, if it passes, through map to findFirst, then pulls the next. one (f3) and two (f3) are rejected. three (f5) passes, map prints m and returns THREE, and findFirst has its answer, so the stream stops and four is never filtered. ifPresent then prints THREE. Both tempting answers include f4 because they assume every element is filtered.

How to reason it out
  1. Trace one: filter prints f3 and rejects it.
  2. Trace two the same way: f3, rejected.
  3. Trace three: f5, accepted, map prints m, findFirst receives THREE.
  4. Note that findFirst short-circuits, so four is never pulled.
  5. ifPresent prints THREE.

Exam tip: Trace streams element by element, and stop at the first result for findFirst, anyMatch and limit.

Java Streams, Lambdas and Primitive Streams (1Z0-831) — the lesson that teaches this.

Question 5Processing Data Using Streams and Lambda Expressions

Given:import java.util.List; import java.util.stream.Stream; public class Main { public static void main(String[] args) { Stream<Integer> stream = List.of(1, 2, 3).stream(); long n = stream.filter(i -> i > 1).count(); System.out.print(n + " "); stream.forEach(System.out::print); } }What is the result?

Choose one.

  • a
    It prints 2 123, then ends without an error

    This treats the stream like a reusable collection. Once count has consumed the stream, it cannot be traversed again.

  • b
    It prints 2 23, then ends without an error

    The filter was applied to the earlier pipeline, not to the variable stream itself, and in any case the stream has already been consumed.

  • c
    It fails to compile because stream is reused

    Reusing a stream is not detected by the compiler; the code compiles and the failure happens at run time.

  • d
    It prints 2, then throws IllegalStateException Correct

    count consumes the stream; the second call on the same Stream object throws IllegalStateException: stream has already been operated upon or closed.

The concept

A Stream can be traversed only once; any second operation on the same stream object throws IllegalStateException.

Why that’s the answer

stream.filter(...) links a new stage to stream, and count() consumes the pipeline, so n is 2 and "2 " is printed. The next line calls forEach on the same stream object, which is already linked and consumed. The JDK throws IllegalStateException with the message stream has already been operated upon or closed. The compiler cannot see this, so the code compiles; the answers 2 123 and 2 23 assume the stream can be replayed like the list it came from.

How to reason it out
  1. Evaluate the first pipeline: 2 and 3 pass the filter, count returns 2.
  2. Print "2 ".
  3. Notice that forEach is called on the same Stream object already used by filter.
  4. A reused stream throws IllegalStateException at run time.

Exam tip: Streams are single-use: create a new one from the source (list.stream()) for every pipeline.

Java Streams, Lambdas and Primitive Streams (1Z0-831) — the lesson that teaches this.

Question 6Processing Data Using Streams and Lambda Expressions

Given:import java.util.stream.Stream; public class Main { public static void main(String[] args) { Stream<Integer> base = Stream.of(1, 2, 3); Stream<Integer> evens = base.filter(n -> n % 2 == 0); System.out.print("A "); Stream<Integer> odds = base.filter(n -> n % 2 == 1); System.out.print("B "); odds.forEach(System.out::print); } }What is the result?

Choose one.

  • a
    A B 13

    This assumes base can feed two pipelines. A stream accepts only one downstream operation, so the second filter call fails before B is printed.

  • b
    A B and then an IllegalStateException is thrown

    The failure is not deferred to forEach. Linking the second intermediate operation to base already throws, so B is never printed.

  • c
    A and then an IllegalStateException is thrown Correct

    The first filter links base to a pipeline. The second filter on base throws IllegalStateException immediately, even though no terminal operation has run yet.

  • d
    Compilation fails

    The compiler does not track how many times a stream is used; the code compiles and fails at run time.

The concept

A stream can have only one downstream operation, and the check happens when an operation is linked, not when a terminal operation runs.

Why that’s the answer

Calling base.filter(...) for evens marks base as linked. evens is never consumed, but that does not matter: the second call base.filter(...) checks the source stage, finds it already linked, and throws IllegalStateException (stream has already been operated upon or closed) at that line. So A is printed, the exception is thrown, and B never appears. The tempting answer that prints B first combines laziness (true for element processing) with the reuse rule (which is checked eagerly).

How to reason it out
  1. Run the first filter: base is now linked to a downstream stage.
  2. Print "A ".
  3. Run the second filter on base: the stage is already linked, so it throws immediately.
  4. No later line runs, so B and the elements are never printed.

Exam tip: Element processing is lazy, but stream reuse is detected eagerly, at the second operation call.

Java Streams, Lambdas and Primitive Streams (1Z0-831) — the lesson that teaches this.

What 1Z0-831 domain 6 tests, topic by topic

The official exam guide breaks Processing Data Using Streams and Lambda Expressions into 2 topics. The question bank follows the same split, so a weak topic shows up as a cluster of misses you can go back and read.

Published 1Z0-831 practice questions per topic in Processing Data Using Streams and Lambda Expressions
TopicWhat it coversQuestions
Object and primitive streams with lambda expressionsOfficial 1Z0-831 objective 6.1: "Use Java object and primitive Streams, including lambda expressions implementing functional interfaces, to create, filter, transform, process, and sort data."43
Reduction, grouping, partitioning and gatherers on streamsOfficial 1Z0-831 objective 6.2: "Perform decomposition, concatenation, reduction, grouping and partitioning, and gather operations on sequential and parallel streams."43
Total86

Revise Processing Data Using Streams and Lambda Expressions before you drill it

Other 1Z0-831 domains

Processing Data Using Streams and Lambda Expressions: your questions

Processing Data Using Streams and Lambda Expressions is domain 6 of the 1Z0-831 exam guide and carries 9% of the scored content — the 5th-heaviest of the 10 domains. On a 50-question paper that works out to roughly 5 questions, though Oracle does not publish an exact per-domain count and individual exam forms vary.

Source

The domain weight and topic list on this page come from the official 1Z0-831 exam guide.