Java 8 Interview Questions and Answers

Hi Friends,

As we all know that now a days Java 8 is widely used and also we are moving to Java 9, 10 and so on...

But now a days its quite common that you will be asked Java 8 questions while facing interview. In fact when I take interview first I ask the person that which version of Java he/she has worked with. If his/her answer is Java 8 then here I am going with frequently asked Java 8 Interview Questions with answers.

1) What is functional interface ?

Ans - 
A functional interface is an interface that contains only one abstract method. They can have only one functionality to exhibit. A functional interface can have any number of default methods.

@FunctionalInterface annotation is used to ensure that the functional interface can’t have more than one abstract method.

Functional Interface is also know as SAM Interface because it contains only one abstract method.
SAM Interface stands for Single Abstract Method Interface.

2) What is Lambda Expressions ?

Ans - 
Lambda expressions basically express instances of functional interfaces.

lambda expressions are added in Java 8 and provide below functionalities.

1) Enable to treat functionality as a method argument, or code as data.
2) A function that can be created without belonging to any class.

3) A lambda expression can be passed around as if it was an object and executed on demand.

3) Difference between Collection API and Stream API

Ans - 

  • Collection API is in use since Java 1.2. Stream API is added to Java in version 8.
  • Collection API is used for storing data in different kinds of data structures. Stream API is used for computation of data on a large set of Objects.
  • With Collection API we can store a finite number of elements in a data structure. With Stream API, we can handle streams of data that can contain infinite number of elements.
  • Collection API constructs objects in an eager manner. Stream API creates objects in a lazy manner.
  • Most of the Collection APIs support iteration and consumption of elements multiple times. With Stream API we can consume or iterate elements only once.
4) What kind of variable you can access in an lambda expression ?

Ans - Using lambda expression, you can refer to final variable or effectively final variable (which is assigned only once). Lambda expression throws a compilation error, if a variable is assigned a value the second time.

5) What is Nashorn ?

Ans - Nashorn is the new Javascript processing engine that is newly introduce in Java 8. Till Java 7, Java was having Mozila Rhino as Javascript processing engine.

6) Difference between Iterator and Spliterator

Ans - 
  • Iterator was introduced in jdk 1.2 while Spliterator is introduced in jdk 1.8
  • Iterator is used for Collection API while Spliterator is used for Stream API
  • 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.
Grab the Java 8 Interview Preparation Course here - Java 8 Interview Preparation