Java 8 Most Frequently Asked Interview Questions and Answers

I have already shared core java interview questions and answers. Java 8 was released in 2014. Now it  has been 4 years since the release of java8. Job market is filled with java8 interview questions.  Today I will be sharing 15 most frequently asked  java8 interview questions and answers. I have already share the java 8 features. Please bookmark this page as I keep adding more question to this post.

Java 8 Interview Questions and Answers

Q1. What are java 8 new features ?

This is the most asked interview question on Java 8. Many of the java developers do not work on java 8. Below are the java 8 features:

1. Functional Interface : Each functional interface has a single abstract method, called the functional method, implementation can be provided using the lambda expressions.
2. Lambda Expressions : It is a feature derived from the functional programming. It is a function that does not belong to any class.
3. Optional : Instead of using null values Optional class is used for representing Optional values.
4. Stream api
5. Spliterator 
6. Method References
7. New Date and Time API.

You can find the important java 8 features here.

Q2 What is the difference between Collection API and Stream API?

Differences between Collection API and Stream API are as follow :

1. Collection API was introduced in jdk 1.2 while Stream API is introduced in jdk 1.8
2. Collection objects are created eagerly while Stream API objects are created lazily.
3. Iterate and Consume elements at any number of times for Collection object while iterate and consume elements only one time for Stream object.

Q3 What is Lambda Expression ?

According to Oracle docs,

Lambda expressions are the method without name i.e Anonymous method. In other words, Lambda expression is a function that can be passed around and referenced as an object.

For example:

Structure of Lambda Expression

A lambda expression consists of three parts :

a. Parameter List
b. Lambda symbol ->
c. Expression

(Parameter List) ->{expression;} 

Q4. What is the difference between PermGenSpace and MetaSpace?

In jdk 8 onwards PermGenSpace is removed. Earlier PermGenSpace is used for storing the metadata. Metadata means storing the information about classes like bytecodes, names and JIT information.
Java classes metadata now stored in native heap and this space is called MetaSpace. Metaspace grows automatically by default and will be garbage collected.

So the major difference between PermGenSpace and MetaSpace is that PermGenSpace was fixed in size and did not grow automatically, but MetaSpace does not have any size constraints.

For more information about the PermGenSpace and Metaspace in Java 8 you can check here .

Q5 What is Optional class in java?

Java SE 8 introduces new class in util package i.e java.util.Optional . In a nutshell, you can view Optional as a single value container which either contains the value or not (then it is called as empty).
It is used to avoid NullPointerException. This Optional class concept is inspired from Haskell and Scala.

Example of Optional Class

a. Here is an Empty Optional

Optional<Soundcard>  sc = Optional.empty();

b. Here is an Optional with non-null value

Soundcard soundcard = new Soundcard();

Optional<Soundcard> sc = Optional.of(soundcard);

The Optional class have various utilities method such as isPresent() which help coders to avoid making use of null value checks.

 Q6 What is Functional Interface in Java 8?

In simple words, Functional interface has exactly one abstract method. A compile time error is thrown if an interface declaration is annotated with @FunctionalInterface  but is not, in fact, a functional interface.

Example of Functional Interface

Already there are many functional interfaces in java. For example, Comparable and Runnable.
Functional interface does not count default methods.

Q7 What is a default method in Java 8 ? When to use it ? 

Default method is also known as defender methods or virtual extension methods. It is a  non abstract method i.e have body, which can be declared inside interface.
Default method is introduced in Java 8 for backward compatibility. That is if you add a new abstract method to the interface, all the implementing classes shall break. Implementing classes need to implement the added abstract method. This problem is solved by default method of java 8.

Q8 What is the difference between Iterator and Spliterator ?

Difference between Iterator and Spliterator  are as follow:

1. Introduction : Iterator was introduced in jdk 1.2 while Spliterator  is introduced in jdk 1.8
2. Use in API  : Iterator is used for Collection API while Spliterator is used for Stream API
3. Parallel programming : Iterator can be used for iterating the elements in Collection in sequential order while  Spliterator can be used for iterating the Stream elements in parallel or sequential order.
4. Universal Iterator : Iteartor is universal iterator while Spliterator is not a universal iterator.

Q9 What is the difference and similarities between Function and Predicate in java 8?


1. Return Type : Function returns an Object and it is a single argument function.
Predicate return type is boolean (i.e true or false) and it is also a single argument function.


1. Both are functional interfaces i.e both contain single abstract method.

Q10 What is the difference between Internal iteration and External iteration?

Java 8 has introduced the new concept "internal iteration". Prior to java 8 there is only external iteration. Let's dive into the differences between internal iteration and external iteration.

Availability : Internal iteration is added in jdk 8 while external iteration is there before jdk 8.

Iteration behavior : Internal iterator iterating an Aggregated Object elements like Collections,Arrays internally.
External iterator iterating an Aggregated Object elements externally.

Approach :  Internal iterator follows functional programming  approach that is declarative style.
Meanwhile, External iterator follows OOP approach i.e imperative style.

Q11 What is method reference in java 8?

Method reference is represented by using double colon operator "::".  Lambda expressions are used to create method anonymously. Sometimes, the sole purpose of lambda expressions is to call existing methods.

Here is the syntax of method reference :

Object :: nameOfTheMethod

According to Oracle docs,  Method references are compact, easy-to-read lambda expressions for methods that already have a name.

Q12 What does String::valueOf expression means?

It is a reference to a static method i.e valueOf method of String class.

Q13 Will the following code compile ?

public interface JavaHungry<A, B, C> {

    public C apply(A a, B b);
    default void printString() {

Yes , the above code will compile. The above code follows the functional interface specification of allowing only single abstract method. Default method printString() does not count as the abstract method.

Q14 What is Nashorn in java 8?

Nashorn is the latest javascript engine released with java8 . Before jdk 8, the javascript engine were based on Mozilla Rhino.

It provides better compliance with ECMA normalized javascript specification and better runtime performance.

Q15 What are the issues of the old Date and Time API's.  Can you tell about the new Date and Time API's in java 8?

Prior to java 8 , Old Date and Time API's are there. Lets find out what are the issues with them:

Performance :  Java 8 API's are better in term of performance than older Date and Time API's.

Standards : Java 8 Date and Time API  comply with ISO standard, meanwhile, older java 8 Date and Time API were hard to understand and poorly designed.

Thread-safe : Most frequently used java.util.Date is mutable and not thread safe. New Java 8 Date and Time API are thread-safe.

LocalDateTime, LocalDate and LocalTime are few of the latest core API classes of java 8.

Please mention in the comments if you know any other java 8 interview questions . 

About The Author

Subham Mittal has worked in Oracle for 3 years .
For more java articles ,Click here to Subscribe JavaHungry