Top 30 Java 8 interview questions and answers

Java 8 interview questions

In this post, we will some important interview questions specifically on Java 8. Java has changed a lot over years and Java 8 has introduced many new features which you need to know when you are preparing for Java interview. 
Here is a list of most asked Java 8 interview questions.

Table of Contents

1)  What are new features which got introduced in Java 8?

There are lots of new features which were added in Java 8. Here is the list of important features:

2) What are main advantages of using Java 8?

  • More compact code
  • Less boiler plate code
  • More readable and reusable code
  • More testable code
  • Parallel operations

3) What is lambda expression?

Lambda expression is anonymous function which have set of parameters and a lambda (->) and a function body .You can call it function without name.

Structure of Lambda Expressions

 Let see a simple example of thread execution:

You can refer to lambda expression in java for more details.

4) Can you explain the syntax of Lambda expression?

So we can divide structure of Lambda expression to three parts:
1. Argument list or parameters
Lambda expression can have zero or more arguments.
You can choose to not declare type of arguments as it can be inferred from context.
you can not declare one argument’s type and do not declare type for other argument.
When there is a single parameter, if its type is inferred, it is not mandatory to use parentheses
2. Array token (->)

3. Body

  • Body can have expression or statements.
  • If there is only one statement in body,curly brace is not needed and return type of the anonymous function is same as of  body expression
  • If there are more than one statements, then it should be in curly braces and return type of anonymous function is same as value return from code block, void if nothing is returned.

5) What are functional interfaces?

Functional interfaces are those interfaces which can have only one abstract method.It can have static method, default methods or can override Object’s class methods.

There are many functional interfaces already present in java such as Comparable, Runnable.

As we have only one method in Runnable, hence it is considered as functional interface.

You can read more about the functional interface.

6) How lambda expression and functional interfaces are related?

Lambda expressions can only be applied to abstract method of functional interface.
For example

Runnable has only one abstract method called run, so it can be used as below:

Here we are using Thread constructor which takes Runnable as parameter. As you can see we did not specify any function name here, as Runnable has only one abstract method, java will implicitly create anonymous Runnable and execute run method.
It will be as good as below code.

7) Can you create your own functional interface?

Yes, you can create your own functional interface. Java can implicitly identify functional interface but you can also annotate it with @FunctionalInterface.
Create interface named "Printable" as below

Create main class named "FunctionalIntefaceMain"

When you run above program, you will get below output:

Printing form

As you can see, since Printable has only one abstract method called print(), we were able to call it using lambda expression.

8) What is method reference in java 8?

Method reference is used refer method of functional interface. It is nothing but compact way of lambda expression.You can simply replace lambda expression with method reference.

9) What is Optional? Why and how can you use it?

Java 8 has introduced new class Called Optional. This class is basically introduced to avoid NullPointerException in java.
Optional class encapsulates optional value which is either present or not.
It is a wrapper around object and can be use to avoid NullPointerExceptions.
Let’s take a simple example

You have written below function to get first non repeated character in String.

You call above method as below.

Do you see the problem, there is no non repeating character for getNonRepeatedCharacter("SASAS") hence it will return null and we are calling c.toString(), so it will obviously throw NullPointerException.
You can use Optional to avoid this NullPointerException.
Let’s change the method to return Optional object rather than String.

When above method returned Optional, you are already aware that it can return null value too.
You can call Optional’s isPresent method to check if there is any value wrapped in Optional.

If there is no value present in Optional, it will simply print "No non repeated character found in String".

10) What are defaults methods?

Default method are those methods in interface which have body and use default keywords.Default method are introduced in Java 8 mainly because of backward compatibility.
You can refer to default method in java for more details.

11) What is the difference between Predicate and Function?

Both are functional interfaces.
Predicate<T> is single argument function and either it returns true or false.This can be used as the assignment target for a lambda expression or method reference.

Function<T,R> is also single argument function but it returns an Object.Here T denotes type of input to the function and R denotes type of Result.

This can also be used as the assignment target for a lambda expression or method reference.

12) Are you aware of Date and Time API introduced in Java 8? What the issues with Old Date and time API?

Issues with old Date and TIme API:

Thread Safety: You might be already aware that java.util.Date is mutable and not thread safe. Even java.text.SimpleDateFormat is also not Thread-Safe. New Java 8 date and time APIs are thread safe.

Performance: Java 8 ‘s new APIs are better in performance than old Java APIs.

More Readable: Old APIs such Calendar and Date are poorly designed and hard to understand. Java 8 Date and Time APIs are easy to understand and comply with ISO standards.

13) Can you provide some APIs of Java 8 Date and TIme?

LocalDate, LocalTime, and LocalDateTime are the Core API classes for Java 8. As the name suggests, these classes are local to context of observer. It denotes current date and time in context of Observer.

14) How will you get current date and time using Java 8 Date and TIme API?

You can simply use now method of LocalDate to get today’s date.

It will give you output in below format:


You can use now method of LocalTime to get current time.

It will give you output in below format:


15) Do we have PermGen in Java 8? Are you aware of MetaSpace?

Until Java 7, JVM used an area called PermGen to store classes. It got removed in Java 8 and replaced by MetaSpace.
Major advantage of MetaSpace over permgen:
PermGen was fixed in term of mazimum size and can not grow dynamically but Metaspace can grow dynamically and do not have any size constraint.

Next 7 questions will be based on below class.

16) Given a list of employees, you need to filter all the employee whose age is greater than 20 and print the employee names.(Java 8 APIs only)

You can simply do it using below statement.

Complete main program for above logic.

17) Given the list of employees, count number of employees with age 25?

You can use combination of filter and count to find this.

18) Given the list of employees, find the employee with name “Mary”.

It is again very simple logic, change the main function in above class as following.

19)Given a list of employee, find maximum age of employee?

It is again very simple logic, change the main function in above class as following.

20) Given a list of employees, sort all the employee on the basis of age? Use java 8 APIs only

You can simply use sort method of list to sort the list of employees.

21) Join the all employee names with “,” using java 8?



Employees are: John,Martin,Mary,Stephan,Gary

22) Given the list of employee, group them by employee name?

You can use Collections.groupBy() to group list of employees by employee name.


Name: John ==>[Employee Name: John age: 21, Employee Name: John age: 26] Name: Martin ==>[Employee Name: Martin age: 19] Name: Mary ==>[Employee Name: Mary age: 31, Employee Name: Mary age: 18]

23) Difference between Intermediate and terminal operations in Stream?

Java 8 Stream supports both intermediate and terminal operation.

Intermediate operations are lazy in nature and do not get executed immediately. Terminal operations are not lazy, they are executed as soon as encountered. Intermediate operation is memorized and is called when terminal operation is called.

All Intermediate operations return stream as it just transforms stream into another and terminal operation don’t.

Example of Intermediate operations are:

limit(long n)
skip(long n)

Example of terminal operations are :


24) Given the list of numbers, remove the duplicate elements from the list.

You can simply use stream and then collect it to set using Collections.toSet() method.

You can use distinct as well to avoid duplicates as following.
change main method of above program as below.

25) Difference between Stream’s findFirst() and findAny()?

findFirst will always return the first element from the stream whereas findAny is allowed to choose any element from the stream.
findFirst has deterministic behavior whereas findAny is nondeterministic behavior.

26) Given a list of numbers, square them and filter the numbers which are greater 10000 and then find average of them.( Java 8 APIs only)

You can use the map function to square the number and then filter to avoid numbers which are less than 10000.We will use average as terminating function in this case.



27) What is use of Optional in Java 8?

Java 8 optional can be used to avoid NullPointerException.You can read about the detailed tutorial.

What is predicate function interface?

Predicate is single argument function which returns true or false. It has test method which returns boolean.
When we are using filter in above example, we are actually passing Predicate functional interface to it.

29) What is consumer function interface?

Consumer is single argument functional interface which does not return any value.
When we are using foreach in above example, we are actually passing Consumer functional interface to it.

30) What is supplier function interface?

Supplier is function interface which does not take any parameter but returns the value using get method.

You may also like other interview questions:

  1. Top 100+ Java coding interview questions
  2. Core java interview questions
  3. Java Collections interview questions
  4. Java Multithreading interview questions
  5. Java String interview questions
  6. OOPs interview questions in java
  7. Exceptional handling interview questions in java
  8. Java Serialization interview questions in java
  9. Method overloading and overriding interview questions
  10. Spring interview questions
  11. Hibernate interview questions
  12. Spring boot interview questions
  13. web services interview questions
  14. restful web services interview questions

Was this post helpful?


You may also like:

Related Posts

  • Unable to obtain LocalDateTime from TemporalAccessor
    20 February

    [Fixed] Unable to obtain LocalDateTime from TemporalAccessor

    Table of ContentsUnable to obtain LocalDateTime from TemporalAccessor : ReasonUnable to obtain LocalDateTime from TemporalAccessor : FixLocalDate’s parse() method with atStartOfDay()Use LocalDate instead of LocalDateTime In this article, we will see how to fix Unable to obtain LocalDateTime from TemporalAccessor in Java 8. Unable to obtain LocalDateTime from TemporalAccessor : Reason You will generally get […]

  • Java LocalDate to Instant
    17 February

    Convert LocalDate to Instant in Java

    Table of ContentsJava LocalDate to InstantUsing toInstant() wth ZoneIdUsing toInstant() with ZoneOffset In this article, we will see how to convert LocalDate to Instant in Java. Java LocalDate to Instant Instant class provides an instantaneous point in time. When you want to convert LocalDate to Instant, you need to provide time zone. Using toInstant() wth […]

  • Java Instant to LocalDate
    17 February

    Convert Instant to LocalDate in Java

    Table of ContentsUsing ofInstant method [ Java 9+]Using ZoneDateTime’s toLocalDate() [Java 8] In this article, we will see how to convert Instant to LocalDate in java. Using ofInstant method [ Java 9+] Java 9 has introduced static method ofInstant() method in LocalDate class. It takes Instant and ZoneId as input and returns LocalDate object. [crayon-63da646d8b9fc281921114/] […]

  • Convert String to LocalDateTime in Java
    17 February

    Convert String to LocalDateTime in Java

    Table of ContentsJava String to LocalDateTimeConvert String to LocalDateTime with custom format In this article, we will see how to convert String to LocalDateTime in Java. LocalDateTime class was introduced in Java 8. LocalDateTime represents local date and time without timezone information. It is represented in ISO 8601 format (yyyy-MM-ddTHH:mm:ss) by default. Java String to […]

  • Java LocalDateTIme to String
    16 February

    Format LocalDateTime to String in Java

    Table of ContentsJava LocalDateTime To StringConvert LocalDateTime to Time Zone ISO8601 StringParse String to LocalDateTime In this article, we will see how to format LocalDateTime to String in java. Java LocalDateTime To String To format LocalDateTime to String, we can create DateTimeFormatter and pass it to LocalDateTime’s format() method. [crayon-63da646d85349322320056/] Here are steps: Get LocalDateTime […]

  • Find duplicate elements in the Stream
    17 October

    Java 8 – Find duplicate elements in Stream

    Table of ContentsIntroductionUsing distinct()Using Collections.frequency()Using Collectors.toSet()Using Collectors.toMap()Using Collectors.groupingBy()Conclusion Introduction When working with a collection of elements in Java, it is very common to have duplicate elements, and Java provides different APIs that we can use to solve the problem. Java 8 Stream provides the functionality to perform aggregate operations on a collection, and one of […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.