java 8 interview questions

 Core Java 8 Features

====================

What are the main features introduced in Java 8 ?

Lambda Expressions

Functional Interfaces

Stream API

Default and Static Methods in Interfaces

java.time (New Date and Time API)

Optional class

Method references and constructor references


Lambda and Functional Interfaces

What is a functional interface? Can you create a custom one?

Interface with a single abstract method (SAM).

Use @FunctionalInterface annotation.


Difference between Predicate, Function, and Consumer interfaces?

Predicate<T> – returns boolean

Function<T,R> – returns a result

Consumer<T> – consumes and returns nothing


How does lambda expression improve code readability or performance?


Streams and Collections

What is the difference between map() and flatMap() in Streams?

map() transforms each element.

flatMap() flattens nested streams.

How does Stream API help in functional-style programming?

Explain lazy evaluation in streams with examples.

What are terminal vs intermediate operations in streams?

How do you filter a list of objects based on multiple conditions using Stream API?

How to collect a stream result into a map or list?

Using Collectors.toList(), toSet(), toMap(), etc.


Optional Class

Why is Optional introduced in Java 8?

To avoid NullPointerException.

Difference between orElse(), orElseGet(), and orElseThrow()?


Default and Static Methods in Interfaces

Why are default methods introduced in interfaces?

For backward compatibility in interfaces.

Can an interface have both default and static methods?

What happens if two interfaces have the same default method?


Method References & Constructor References

What is the syntax of method reference? When should you use it?

ClassName::methodName

Simplifies lambda expressions.


Advanced/Tricky Questions

How does Java 8 deal with internal vs external iteration?

Explain stream parallel processing (parallelStream()) and when to use it.

Can you write a multi-level grouping collector using Stream API?

What is the difference between findFirst() and findAny()?


Popular posts from this blog

Java Input Output Streams

Java 8 Interview Programs