[2022] Java 8 Important Interview Questions asked in MNCs

In this Page we have collected and explained Most important Interview questions related to Java 8. We have covered almost all type of interview questions related to Java 8 newly added features.

This post will help you to prepare and answers all java 8 interview questions that can be asked during your interviewer.

This Java 8 questions and answers are helpful for freshers as well as experienced who is preparing for interview as Java developer.

Let us start Preparing.

Java 8 interview questions

Q1). Tell us some newly added features in Java 8.

Ans:

Some newly added Features of Java 8 are Lambda Expression, Functional Interface, Stream API, Default Method, Method References, Date Time API, Optional Class, Nashorm, and JavaScript Engine.

Short Explanations

Lambda Expression: lambda expression is a block of code that takes parameters and returns a value. We can refer to Lambda Expression as an object.

Functional Interfaces: It is an interface that contains only one abstract method.

Method References: Method References allow us to call the Class method by its name. “::” denotes method reference.

Default method: It is a method in the interface that implementation is not required in the class implementing that interface.

Stream API: Stream API is a sequence of objects used to process Collections.

Date Time API: It is introduced to overcome old Date drawbacks like new Date Time API is thread safe.

Optional: It is a Wrapper class to check the null value.

Nashorn, JavaScript Engine: Nashorn is an improved javascript engine to replace the existing Rhino.

Q2). What is default method in Java 8?

Ans:

the default method is a method inside the interface with the keyword default. It is a type of non-abstract method.

As we know an interface can have only abstract methods without a body. And we need a separate class for the implementation of interface methods. If we add a new method then we have to add the implementation of the newly added method in the class implementing the same interface.
To overcome this issue, Java 8 has introduced the concept of default methods. In interface, a default method can have its body so we don’t need to add its implementation in the class implementing the same interface.

Example of Default Method

 

Output

125
This is default method

Q3). Can We Override Default Method in Java?

Ans: Yes, we can override the default method in Java.

Q4). What is static Interface Methods in Java 8 ?

Ans:

Java 8 provided us with a feature to define and implement static methods in interfaces.

As we know the static method belongs to the class, not to a particular object. We can call the method using the interface name preceding the method name.

Example of using the static method

 

Output

This is static Method can call by Interface

Q5). What is the lambda expression in Java 8.

Ans:

Lambda expression is a function without a name. It is also known as an anonymous function as it does not have type information by itself. Lambda expression is executed on-demand. It is used with collection to perform operations like iteration, filter, extracting data, etc. The return type of the anonymous function is the same as that of the body expression.

As lambda expressions are similar to anonymous functions, they can only be applied to a single abstract method of Functional Interface. It will infer the return type, type, and several arguments from the signature of the abstract method of functional interface.

basic structure/syntax of a lambda expression

FunctionalInterface fi = (String name) -> { 
System.out.println(name); 
return name; 
}

Lambda expression is divided into three partsb

1. List of Arguments/Params:

(String name) 

Params or Parameters are passed in () round brackets. Lambda expression can have zero, one, two, or more params. Declaring the type of parameter is optional. It can be inferred from the context. 

2. Arrow Token:

-> 

 Arrow token is a lambda arrow operator which separates the parameters from the body. It points the list of arguments to the body. 

3. Expression/Body:

System.out.println(“Hello “+name); 

return “Hello “+name; 

}

The body contains the expressions or statements. If we have more than one line then we will use {} curly braces. the return type of the anonymous function is the same as that of the body expression. 

Features of a lambda expression

  • Lambda expressions can be passed as a parameter to another method. 
  • Lambda expressions can be standalone without belonging to any class.

Example to Iterate Arraylist using Lambda Expression

 

Output

quescol
interview

Q6) What is String::valueOf expression means?

Ans:
String::valueOf is an example of a method reference. Here, String is class and valueOf is a static method of String class.

Here the valueOf() method
will take input and will return a string representation of the value.

Program to demonstrate String::valueOf

Output

java 8 interview questions

Here int value is converted into String using valueOf() method.

Q7). What is optional, and what is it best used for?

Ans :

In Java 8, Optional is a newly introduced class in java.util package to eliminate too many null checks. Optional comes with some built-in functions like isPresent(),ofNullable(), empty(), of() etc. 

Optional Example Java 8

 

Output

java 8 interview questions

 

Q8) What is Stream in Java 8?

Ans:

Stream API is introduced newly in Java 8 to process the Collection object.

Stream is not a data structure or it does not store any data, It just provides a convenient way to operate with data source and make its processing faster.

There are two methods to generate a Stream.

1). stream() − Returns a sequential stream considering collection as its source.

2). parallelStream() − Returns a parallel Stream considering collection as its source.

There are two types of operation performed on Stream

1). Intermediate operation : it contains methods like map(), filter() and Sorted().

2). Terminal Operation : It contains methods like contains(), forEach() and reduce().

Example of Stream API

 

 Output

java 8 stream interview question

Q9) What data source a Stream can Process?

Ans:

A Stream can process the following data:

  • A collection
  • An Array
  • An I/O channel

 

Q10) Tell me some Intermediate operations of Stream API?

Ans:

  • Filter(Predicate<T>) – This method takes the condition and returns only the satisfying condition.
  • map(Funtion<T, R>) – This method returns a new Stream as a return value. It is used when we want to convert stream X into stream Y.
  • distinct() – distinct() method returns a stream that consist distinct elements in a stream. 
  • limit(long maxsize) – It is a terminal operation that returns a stream of elements in a given size.
  • skip(long start) – It discards the element till given in the skip function.

Q11) What new feature added in new Date and Time API in Java 8?

Ans:
  • In Java 8, the new Data Time API is Thread-safe.
  • New Java 8 API have Domain-driven design.
  • It allows developers to work with different calendaring systems like Japan or Thailand or India without extra burden.
  • All packages are based on the ISO-8601 calendar system

Q12) What is LocalDate in Java 8?

Ans:
In Java 8, LocalDate is an immutable date-time object that is used to represent a date. The default format of LocalDate is year-month-day.
Some important function of LocalDate are getDayOfMonth(), getDayOfWeek(), getMonth(), getYear() etc.
Example of LocalDate Program
 

java 8 local date interview question

Q13). What is LocalTime in Java 8?

Ans:
In Java 8, LocalTime is an immutable date-time object that is used to represent a time. The default format of LocalTime is an hour-minute-second.
Some important function of LocalTime are plusHours(), minusHours(), plusMinutes(), minusMinutes() etc.
 
Example of LocalTime Program

Output

java 8 localTime interview question

Q14) What is LocalDateTime example in Java 8?

Ans:

In Java 8, LocalDateTime is an immutable date-time object that is used to represent a date-time. Default format of 

LocalDateTime is year-month-day-hour-minute-second.

Some important function of LocalDateTime are plusDays(),minusDays(), now() etc.

Example of Java 8 LocalDateTime

Output

java 8 local date time interview question

Q15). What is the difference between predicate and function?

Ans:
  • A predicate can take one parameter that represents the input type or return type. The function takes two parameters one represents input type and the second represents return type.
  • The predicate has one test() function which means “test this function” Whereas the function has one apply() function means “apply this function”.
  • Predicate returns only Boolean value but Function can return any value.
  • If we want to perform some conditional checks we go with Predicates. But if we want to perform some operation, we should go with Function.

Predicate Example Java Program

Output

java 8 predicate interview question

Function Example Java Program

 

function example java interview question

Q16). Difference between findFirst() and findAny()?

Ans:

  • findFirst() returns the first element from List that meeting the given criteria, while
  • findAny() function returns any element from list that will meet the criteria. This  feature is very useful when we work with a parallel stream.

Example to demonstrate findFirst() and findAny()

 

 Output

findfirst and findany java interview question

Q17). Tell us the steps to create Functional Interface.

Ans: To Create Functional Interface Java 8 Provided @FunctionalInterface Annotation.
After adding annotation on Interface, It can have only one abstract method and can have multiple static and default methods.

Java Program to create Functional Interface

Output

functional interface java interview question

 Functional Interface Program to calculate Cube 

Output

functional interface cube interview question

Q18). What is Method Reference in Java 8?

Ans: 

Java 8 has added a new feature Method Reference. Method Reference refer the method of functional interface.

Example to demonstrate Method Reference:

If our Lambda Expression is written as below

str -> System.out.println(str)

Method Reference will be like

System.out::println

“::” is the symbol that used in method reference.

Java Program to Demonstrate Method Reference

Output

method reference java interview question

Q19). What is the differences between limit and skip?

Ans:
  • Limit() method is used to return the stream of a given size. Suppose if we have written limit(10) it means total 10 output will be returned only.
  • skip() method is used to return stream after skipping the index starting from 0 to given index. Skip(5) means skip element from starting(0) to 5 elements i.e. index 4.

Java program to demonstrate Limit() 

Output

limit program interview question

Java Program to demonstrate skip()

 

Output

skip java interview question

Q20). Difference between Intermediate and Terminal Operations in Stream

Ans:

Generally, Stream operations are either Terminal or Intermediate operations. Intermediate Operations return the Stream to perform other operations on it.

  • Intermediate Operations process data when there is a Terminal operation carried out.
  • Some intermediate operations are map(), filter(), distinct(), sorted(), limit(), skip().
  • As an Intermediate return stream value, Terminal Operation returns non-stream values like primitive, object, collection, or It will return nothing.
  • There might be multiple intermediate operations, but a stream can have only one terminal operation.
  • Some Terminal Operation are forEach(), toArray(), reduce(), collect(), min(), max(), count(), anyMatch(), allMatch(), noneMatch(), findFirst(), findAny() etc.

Example of Intermediate & Terminal Operation

Output

intermediate and terminal java operation

Q21). What is Nashorn in Java 8?

Ans:

In Java 8,  Nashorn  is added as new default JavaScript engine for the JVM.

Q22). Difference Between Map and flatMap Stream Operation?

Ans:

map() and flatMap() are used when we have to map the elements of a collection to a certain function and then we need to return the stream which contains the updated results.

Map() produces one output for each input in the stream, whereas the flatMap() produces zero or more output for each input.

One-to-one mapping occurs in a map() but flatmap() allows one to many mapping.

Example of map()

Output

map() stream java interview question

Example of flatMap()

 

Output

flatmap stream java 8 interview question

Q23). What is MetaSpace in Java 8?

Ans:

MetaSpace is replaced with old PermGen memory space. It is used to how efficiently store the class.

Difference between MetaSpace with PermGen is, MetaSpace is able to increase their size dynamically.

Q24). Tell me something about StringJoiner Class in Java 8.

Ans:

StringJoiner is a new class added in Java 8 under java.util package.

It is used to join multiple strings separated by delimiters along with providing prefix and suffix to them.

Program to demonstrate StringJoiner

 

Output

stringjonier program java 8 interview question

Q25). Write a Program to add any prefix and suffix to the String in Java 8?

Ans:

To write a program in java to add prefix and suffix in a given String we will use StringJoiner newly added in Java 8.

We will add “[“ and “]” as a prefix and suffix.

 

Output

stringjoiner java 8 interview questions

Q7). Print ten random numbers using forEach?

Ans:

Below is the program to generate 10 random number using forEach. Here we are using Random class to generate Random number.

 

Output

program to print 10 random number

Q8). How you can print the highest number in Array?

Ans:
Output
java 8 code based interview question

Q9). Print Current date, time and date time using Java 8 Date and Time API.

Ans:

 

Output

java 8 interview question