15+ Best Java 8 Interview Questions & Answers

If you are looking for Java 8 interview questions and answers, look no further. We've compiled the absolute best Java 8 interview questions to help you with your upcoming interview. Preparation is key when it comes to technical interviews. You will be asked questions as it pertains to your knowledge of specific programming languages. Below are some of the best questions which should prepare you.

Background On Java 8

Java 8 is a version of Java. Java was designed by James Gosling but has been managed by Oracle Corporation every since. Java is still a highly used programming language even though more web programming languages like React, Node.js, and others have gained in popularity in recent years. Java is most commonly used in native applications.

15 Java 8 Interview Questions & Answers

Table of Contents


1. List the new features that have been introduced into Java 8.

Date API, Stream API, the JavaScript Engine Nashorn, death methods, a functional interface, Optional special wrapper class, method references, and Lambda expressions were all added to Java 8. Along with these features, many previous features have been improved upon.

2. Describe what a method reference is and provide code examples.

A method reference in Java 8 is a type of construct used for referencing a method without citing it. This is used for treating methods like Lambda expressions. Method references are only able to reduce the verbosity of lambdas.

For example, the code (o) -> o.toString(); can become Object::toString(); through method reference. A method reference can also be pinpointed through two colons that separate the class or object name and the method name, such as String::new;.

3. What is jjs?

In Java 8 specifically, jjs is a new executable command line tool used to submit JavaScript code into the console.

4. Describe what a stream is and how it is different from a collection.

A stream is simply an iterator that is used to accept a new set of actions in order to apply on each of the elements the action set contains. The stream essentially represents a sequence of different objects from one source, such as a collection, which contains aggregate operations. The stream was designed to help improve collection processing and make it simpler in execution. The stream API is also more fluent and makes pipelining easier, as seen here:

5. What does “Optional” mean and how can it be used in Java 8?

Optional is one of the new classes to be added to Java 9. It references an additional or optional value. It serves as a wrapper around a particular object and can be seen as a container of either no element or one element. It also has a unique Optional.empty() value, rather than a wrapped null value. This function is usually used to remove NullPointerException. Overall, the basic purpose of Optional is to be a sort of return type of methods that would otherwise return null. It reduces the need to write additional code for this purpose.

6. What does the code String::valueOf mean?

This is an expression that is a static method reference to valueOf under String class.

7. List and describe some functional interfaces inside the standard library.

There are many functional interfaces within the java.util.function package, but a few common ones are as follows.

Function, which takes a single argument and returns a result. Consumer, which takes a single argument and returns no result, representing a side effect. Supplier, which takes a non-argument and returns a single result. Predicate, which takes a single argument and returns a boolean. BiFunction, which takes two arguments and returns a single result. UnaryOperator, which is not unlike a Function, which takes a single argument and returns a result of the same type. BinaryOperator, which is not unlike a BiFunction, which takes two arguments and returns a single result. The two arguments and the result are all of the same types under BinaryOperator.

8. Describe a functional interface and include some rules for defining one.

A functional interface can be defined as an interface that has only one abstract method, not including default methods. A Lambda expression can be used in place of this type of interface. Functional interfaces allow for the generation of target types needed for Lambda expressions as well as method references. The arguments of these types of expressions will match the expressions of a single abstract method.

For example, we can look at the Runnable interface and see it as a function reference. Rather than using this:

One can use this as a functional interface:

Thread thread = new Thread(() -> System.out.println("Sample Expression"));

Functional interfaces are also usually noted by @FunctionalInterface.

9. Describe default method and when it should be used.

A default method is a type of method in Java 8 that has an implementation within an interface. A developer can use a default method to create additional new functionalities to an interface, all while allowing for backward compatibility within a class that is already implementing the interface.

We can look at the following code to see how functionalities are implemented:

When a brand new abstract method is implemented into an interface, the implementing classes will all break until the new abstract method is added. In Java 8, the default method was added as a solution to this issue.

10. Look at the following code and answer if it can or cannot compile. If it can, describe why it can.

The answer is yes, it can compile. The reason it can compile is because it follows a functional interface specification that results in defining just a single abstract method. The count method is a default method that doesn’t necessarily increase the abstract method’s count.

11. Describe the difference between terminal and intermediate operations.

The operations within the stream are consolidated into different pipelines in order to process the stream. Those operations are either terminal or intermediate.

Terminal operations destroy the pipeline and initiate stream processes. The stream itself is passed through the intermediate operations during a terminal operation call. forEach, reduce, Collect and sum are a few terminal operations.

Intermediate operations are operations that return streams and allow for additional operations on a stream. These operations are lazy, meaning they don’t process the stream itself at the call site and can only process data when a terminal operation is executed. Intermediate operations include flatMap, map, filter, etc.

12. Describe what a Lambda expression is and what exactly it is used for in Java 8.

A Lambda expression, to put it simply, is a type of function the can be referenced and moved around as a form of an object. These types of expressions allow for a more functional style processing in Java 8 and also facilitate better writing of condensed and simplified code. Lambda expressions are a great replacement for anonymous classes as method arguments. Their main use is to notate inline implementations of different functional interfaces.

13. What are the characteristics and syntax of a Lambda expression?

A Lambda expression contains two elements, which are parameter and expressions. They are usually separated as params -> expressions.

When setting the parameters on the left side of the Lambda, one doesn’t need to declare their types because the compiler can determine them from their values. So int param -> … and param ->… are both valid. This is known as optional type declaration. When only one parameter is declared, one doesn’t need to place it in parentheses. This determines param -> … and (param) -> … are both valid. However, when more than a single parameter is declared, parentheses are required. This is known as optional parentheses. When the expression only has one statement, there is no need for curly braces. This means that param – > statement and param – > {statement;} are both valid. However, curly braces are required when there is more than a single statement. This is known as optional curly braces. When the expression returns a single value and it is wrapped inside of curly braces, then one doesn’t need a return statement. That means (a, b) – > {return a+b;} and (a, b) – > {a+b;} are equally valid. This is known as optional return statement.

All Lambda expressions can have all or some of these characteristics.

14. What is Nashorn?

In Java 8, Nashorn is a new JavaScript processing engine for the platform. Up until JDK 7, Hava’s platform used Mozilla Rhino as a JavaScript processing engine. Nashorn is an improvement because it allows for better compliance with the ECMA normalized JavaScript specification as well as better performance in runtime.

15. What is the difference between map and flatMap in stream operations?

A flatMap operation in Optional would return a String type, while a map operation would return an Optional < String > type due to the difference in the signature. A map operation wraps return values inside the ordinal type and flatMap does not do this.

author: patrick algrim
About the author

Patrick Algrim is an experienced executive who has spent a number of years in Silicon Valley hiring and coaching some of the world’s most valuable technology teams. Patrick has been a source for Human Resources and career related insights for Forbes, Glassdoor, Entrepreneur, Recruiter.com, SparkHire, and many more.


Help us by spreading the word