Java 8 Interview Questions – Part 2

In this article, we will discuss frequently asked Java 8 Interview Questions and Answers.

You can refer the Part 1 using this link : Java8 Interview Questions : Part 1
Java 8 Interview Questions - Part 2

Java 8 Interview questions

1. What are Static Interface Methods in Java 8 ? How are static Interface methods invoked ?

JDK 8 added the ability to define one or more static methods in an interface.
Since its static, a static method defined by an interface can be called independent of any object. So, no implementation of the interface is required for calling the static method.

Here is the general syntax of accessing an Interface static method :
InterfaceName . staticMethodName

Read this article for more information on Static Interface Methods :

Java 8 : Static Interface Method


2. What is Optional in Java 8 ?

Java 8 added a new class java.util.Optional that provides a way to handle situations where a value may or may not be present.
Optional is a container object which may or may not contain a not null value.

Optional class forces us to deal with absense of a value. This helps prevent Null pointer exceptions at runtime.

For more information on Optional, refer this article :
Optional in Java 8

3. Why use Optional in Java 8 ?

We normally assign null to a variable to indicate that it doesn’t have a value.

However, this frequently results in a NullPointerException if we to try to find value by dereferencing.

To prevent this, we normally add frequent NULL checks in our code to check if a variable is not empty before we use it in our program.

Optional provides a better approach to handle such situations.


4. How can you obtain current Date/Time using Java 8 Date Time api ?

Java 8 introduced a new java.time package which contains LocalDate, LocalTime and LocalDateTime classes.

These classes can not be instantiated with new operator.

To get the current Date and Time, we can use the static now() method of LocalDate and LocalTime classes.

LocalDate date =;

LocalTime time =;

LocalDateTime dateTime =;

Refer this article for more information on Java 8 Date Time api:

Java 8 Date Time api


5. How to convert String to LocalDate, LocalDateTime in Java ?

The Java 8 LocalDate-Time API includes a parse() method, which can be used to parse a given input string using a specified format.

For example,

LocalDate newDate = LocalDate.parse("2016-08-23");
System.out.println("Parsed date : " + newDate);

Output :

Parsed date : 2016-08-23

LocalDateTime newDatetime = LocalDateTime.parse("2016-08-23T12:23:45");
System.out.println("Parsed datetime : " + newDatetime);

Output :

Parsed datetime : 2016-08-23T12:23:45

You can also use DateTimeFormatter to parse a string using custom patterns like dd/MM/yyyy or yyyy MM dd.

Refer this article for more information on these :

Java 8 : Convert String to LocalDate, LocalDateTime

6. What is the purpose of join() method introduced in Java 8 ?

Java 8 added overloaded static join methods that join multiple strings into a single string.

Refer this article for more information on the new String join() method.

String join() method in Java 8

7. Explain the Java 8 Stream map() method.

The map() method is useful to map the elements of one stream to another.

For example, the following map() operation converts each string to uppercase.

List<String> countries = Arrays.asList("Australia","Brazil", "China","Denmark","England","France","Germany"); -> name.toUpperCase())



Read more about other Stream operations here :

Java 8 Stream Operations with examples

8. How to sort a Map in Java 8 ?

We can use the Stream sorted() method for this. This method sorts the elements in ascending order and returns the sorted stream.

Refer the following article for sorting a map by key, by value or even sorting in reverse order of key/value.

How to sort a map in Java 8

9. How can we execute statements in parallel using a for loop ?

For loop(and other looping constructs) execute operations in a sequential manner. So, we can’t execute a for loop in parallel.

However, Streams facilitate parallel execution.

Here is how we can use Streams to execute instructions in parallel.

List<String> countries = new ArrayList<String>();
countries.parallelStream().forEach((country) -> {


10. What is Spliterator in Java SE 8 ?

JDK 8 introduced the new type of iterator called Spliterator.
Spliterator supports parallel programming. But, you can use Spliterator even if you don’t need parallel execution.
A Spliterator may traverse elements individually (tryAdvance()) or sequentially in bulk (forEachRemaining()).

Refer this article for more information on iterating using Spliterator, Iterator and ForEach loops :

Iterating using Iterator, ListIterator and Spliterator in Java

More on Java 8


Here is a summary of the Java8 new features for your reference :

Java 8 new features


Also, here are some interesting quizzes on java8 features that you may like :

Java quiz 8 (lambda expression)
Java 8 quiz on multiple inheritance with Default Interface Method

© 2017 – 2018, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags