LearnFrenzy
Sign in Take a free test

Java Programming · Topic 22 of 28

Streams API

  • 4 exercises
  • 60 questions
  • ~60 min
  • Answers explained

Interview Questions

Streams API · Study material

Java Streams API – Interview Questions

1. What is a stream?

A sequence of elements supporting functional-style aggregate operations; it does not store data or modify its source.

2. Collection vs Stream?

A collection stores elements; a stream computes over them lazily, once, and can be infinite.

3. Intermediate vs terminal operations?

Intermediate operations return a stream and are lazy; terminal operations produce a result and trigger execution.

4. map vs flatMap?

map produces one output per input; flatMap maps each input to a stream and flattens them.

5. What is lazy evaluation in streams?

Intermediate operations do nothing until a terminal operation runs; then elements are processed one by one through the pipeline.

6. What are short-circuiting operations?

Operations that can finish without examining every element: limit, findFirst, findAny, anyMatch, allMatch, noneMatch, takeWhile.

7. Can a stream be reused?

No; a second terminal operation throws IllegalStateException.

8. What does reduce do?

Combines elements into one value using an identity and an associative accumulator (e.g. sum, max, concatenation).

9. findFirst vs findAny?

findFirst respects encounter order; findAny may return any element and is faster in parallel streams.

10. What is the difference between peek and forEach?

peek is an intermediate operation for debugging; forEach is terminal.

11. What do groupingBy and partitioningBy do?

groupingBy builds a Map by a classifier; partitioningBy builds a Map<Boolean, …> by a predicate.

12. How do you convert a stream to a map?

collect(Collectors.toMap(keyFn, valueFn[, mergeFn[, mapFactory]])).

13. What happens with duplicate keys in toMap?

IllegalStateException unless a merge function is supplied.

14. Why use IntStream?

To avoid boxing and to get numeric operations like sum, average and range.

15. What is Stream.iterate vs Stream.generate?

iterate builds each element from the previous one; generate calls a Supplier each time. Both can be infinite.

16. Stream.toList vs Collectors.toList?

Stream.toList returns an unmodifiable list; Collectors.toList guarantees nothing about mutability.

17. What are parallel streams?

Streams whose work is split across threads using the common ForkJoinPool.

18. When are parallel streams a bad idea?

Small data, I/O-bound work, ordering requirements, or shared mutable state.

19. Does a stream modify its source?

No; it produces new results (though the source must not be modified during the pipeline).

20. What is Collectors.teeing?

A Java 12 collector that feeds elements to two collectors and merges their results.

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About Java Programming

Java is one of the most used languages in the world – for enterprise software, Android apps, banking systems and backend services. This section covers core Java from syntax and data types to OOP, strings, arrays, exceptions, collections and multithreading, with clear explanations and practice questions for every topic.

Why it matters

  • Java is the main language in service-company placement tests and many product companies
  • OOP, String, collections and exception questions appear in nearly every Java interview
  • Strong core Java is the base for Spring Boot, Android and backend development
  • Java questions are common in university exams and certification tests

Who it is for

  • School students (Class 11–12 / ICSE computer applications) learning Java
  • College students (B.Tech, BCA, MCA) preparing for exams, labs and projects
  • Freshers preparing for placements, Java developer roles and technical interviews
  • Developers revising core Java before an interview

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs that train you to spot bugs quickly
  • Complete programs with solutions and real output, verified on a compiler

Asked in

  • Campus placements
  • Java developer interviews
  • Service company tests
  • University exams
  • Certification exams
  • Government IT exams

How to use this section

  1. Read the study material of a topic and run the examples yourself.
  2. Practise the four exercises – MCQs, output, errors, programs – and read every explanation.
  3. Test yourself with the timed online test, then revise the topics you got wrong.

Search LearnFrenzy

Esc

Quick links