Courseiva
Working with Streams and Lambda ExpressionseasyMultiple ChoiceObjective-mapped

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

A developer wants to count how many strings in a list have length greater than 5. Which is the correct implementation?

⚠ Common exam trap

Candidates often confuse `count()` with a method that accepts a predicate, or mistakenly try to map booleans to numeric values using `mapToLong`, forgetting that `mapToLong` requires a `ToLongFunction` returning a primitive `long`, not a `boolean`.

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

list.stream().filter(s -> s.length() > 5).count()

The Stream API provides the `filter` method to select elements matching a predicate, and `count` returns the number of elements in the resulting stream. This directly counts strings with length greater than 5 without needing any intermediate mapping or collection.

Answer analysis

Option-by-option breakdown

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

  • list.stream().mapToLong(s -> s.length() > 5).sum()

    Why it's wrong here

    mapToLong expects a ToLongFunction, not a predicate; this would cause a compilation error.

  • list.stream().filter(s -> s.length() > 5).count()

    Why this is correct

    Correct: filter then count gives number of elements satisfying predicate.

  • list.stream().count(s -> s.length() > 5)

    Why it's wrong here

    count() does not accept a predicate; it returns the number of elements in the stream.

  • list.stream().collect(Collectors.counting(s -> s.length() > 5))

    Why it's wrong here

    Collectors.counting() takes no argument; it counts all elements.

About these practice questions

One of 513 original 1Z0-829 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. 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.