Courseiva
Working with Streams and Lambda ExpressionseasyMultiple ChoiceObjective-mapped

1Z0-829 Working with Streams and Lambda Expressions Practice Question

Exhibit

Refer to the exhibit.

Error log:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	The method flatMap(Function<? super String,? extends Stream<? extends String>>) in the type Stream<String> is not applicable for the arguments (String::chars)

Code snippet:
List<String> words = List.of("hello", "world");
List<Character> letters = words.stream()
    .flatMap(String::chars)
    .mapToObj(c -> (char) c)
    .collect(Collectors.toList());

The code fails to compile. What is the reason?

⚠ Common exam trap

Candidates often assume `String::chars` returns a `Stream<Character>` because it deals with characters, but it actually returns an `IntStream`, leading to a type mismatch when used with `flatMap`.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

The flatMap method requires a function that returns a Stream, but String::chars returns an IntStream.

The `chars()` method on `String` returns an `IntStream`, not a `Stream<Character>`. The `flatMap` method on a `Stream<String>` expects a function that returns a `Stream<?>`, but `String::chars` returns an `IntStream`, which is a primitive stream and not a subtype of `Stream<?>`. This type mismatch causes a compilation error.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • The collect method is not applicable for a Stream<Character>.

    Why it's wrong here

    Collectors.toList() works fine on Stream<Character>.

  • The method reference String::chars is not a valid method reference.

    Why it's wrong here

    String::chars is a valid method reference; it returns an IntStream.

  • The chars() method returns a Stream<Character>, but flatMap requires a function that returns a Stream<Integer>.

    Why it's wrong here

    chars() returns IntStream, not Stream<Character>.

  • The flatMap method requires a function that returns a Stream, but String::chars returns an IntStream.

    Why this is correct

    flatMap expects a Function returning a Stream<? extends R>, but chars() returns IntStream, which is not a Stream.

About these practice questions

Courseiva writes every 1Z0-829 question from scratch — 513 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This 1Z0-829 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-829 exam.