Java 8 interview questions and answers — Java2Blog

Arpit Mandliya
Javarevisited
Published in
6 min readSep 10, 2017

In this post, we will some important interview questions specifically on Java 8. Java has changed a lot over years and Java 8 has introduced many new features which you need to know when you are preparing for Java interview. Here is a list of most asked Java 8 interview questions.

Here is a list of most asked Java 8 interview questions.

1) What are the new features which got introduced in Java 8?

There are lots of new features which were added in Java 8. Here is the list of important features:

2) What are the main advantages of using Java 8?

  • More compact code
  • Less boiler plate code
  • More readable and reusable code
  • More testable code
  • Parallel operations

3) What is lambda expression?

Lambda expression is an anonymous function which has a set of parameters and a lambda (->) and a function body. You can call it to function without a name.

Structure of Lambda Expressions

Let see a simple example of thread execution:

You can refer to lambda expression in java for more details.

4) Can you explain the syntax of Lambda expression?

So we can divide the structure of Lambda expression into three parts:

1. Argument list or parameters

Lambda expression can have zero or more arguments.

You can choose to not declare a type of arguments as it can be inferred from context.

you can not declare one argument’s type and do not declare a type for other arguments.

When there is a single parameter, if its type is inferred, it is not mandatory to use parentheses

2. Array token (->)

3. Body

  • Body can have expression or statements.
  • If there is only one statement in body, curly brace is not needed and return type of the anonymous function is same as of body expression
  • If there are more than one statements, then it should be in curly braces and return type of anonymous function is same as value return from code block, void if nothing is returned.

5) What are functional interfaces?

Functional interfaces are those interfaces which can have only one abstract method. It can have a static method, default methods or can override Object’s class methods.

There are many functional interfaces already present in Java such as Comparable, Runnable.

As we have only one method in Runnable, hence it is considered as functional interface.

You can read more about the functional interface.

6) How lambda expression and functional interfaces are related?

Lambda expressions can only be applied to the abstract method of functional interface.
For example

Runnable has only one abstract method called run, so it can be used as below:

Here we are using Thread constructor which takes Runnable as a parameter. As you can see we did not specify any function name here, as Runnable has only one abstract method, java will implicitly create anonymous Runnable and execute run method.
It will be as good as below code.

7) Can you create your own functional interface?

Yes, you can create your own functional interface. Java can implicitly identify functional interface but you can also annotate it with @FunctionalInterface.
Example:
Create an interface named “Printable” as below

Create the main class named “FunctionalIntefaceMain”

When you run the above program, you will get below output:

As you can see, since Printable has only one abstract method called print(), we were able to call it using a lambda expression.

8) What is method reference in java 8?

Method reference is used refer method of functional interface. It is nothing but a compact way of the lambda expression. You can simply replace lambda expression with method reference.
Syntax:
class::methodname

9) What is Optional? Why and how can you use it?

Java 8 has introduced a new class Called Optional. This class is basically introduced to avoid NullPointerException in Java.
The optional class encapsulates optional value which is either present or not.
It is a wrapper around the object and can be used to avoid NullPointerExceptions.
Let’s take a simple example

You have written below function to get first non repeated character in String.

You call the above method as below.

Do you see the problem, there is no non-repeating character for getNonRepeatedCharacter(“SASAS”) hence it will return null and we are calling c.toString, so it will obviously throw NullPointerException.
You can use Optional to avoid this NullPointerException.
Let’s change the method to return Optional object rather than String.

When the above method returned Optional, you are already aware that it can return null value too.
You can call Optional’s isPresent method to check if there is any value wrapped in Optional.

If there is no value present in Optional, it will simply print “No non repeated character found in String”.

10) What are the defaults methods?

The default method is those methods in the interface which have a body and use default keywords. The default method is introduced in Java 8 mainly because of backward compatibility.
You can refer to the default method in Java for more details.

11) What is the difference between Predicate and Function?

Both are functional interfaces.
Predicate<T> is a single argument function and either it returns true or false. This can be used as the assignment target for a lambda expression or method reference.

Function<T, R> is also a single argument function but it returns an Object. Here T denotes a type of input to the function and R denotes a type of Result.

This can also be used as the assignment target for a lambda expression or method reference.

12) Are you aware of the Date and Time API introduced in Java 8? What the issues with Old Date and time API?

Issues with old Date and TIme API:

Thread Safety: You might be already aware that java.util.The date is mutable and not thread-safe. Even java.text.SimpleDateFormat is also not Thread-Safe. New Java 8 date and time APIs are thread-safe.

Performance: Java 8 ‘s new APIs are better in performance than old Java APIs.

More Readable: Old APIs such as Calendar and Date are poorly designed and hard to understand. Java 8 Date and Time APIs are easy to understand and comply with ISO standards.

13) Can you provide some APIs of Java 8 Date and TIme?

LocalDate, LocalTime, and LocalDateTime are the Core API classes for Java 8. As the name suggests, these classes are local to the context of the observer. It denotes current date and time in the context of Observer.

14) How will you get the current date and time using Java 8 Date and Time API?

You can simply use now method of LocalDate to get today’s date.

It will give you output in below format:

You can use now method of LocalTime to get the current time.

It will give you output in below format:

15) Do we have PermGen in Java 8? Are you aware of MetaSpace?

Until Java 7, JVM used an area called PermGen to store classes. It got removed in Java 8 and replaced by MetaSpace.
The major advantage of MetaSpace over permgen:
PermGen was fixed in term of maximum size and cannot grow dynamically but Metaspace can grow dynamically and do not have any size constraint.

Originally published at https://java2blog.com on September 10, 2017.

--

--