Java 8 Interview Questions and Answers
The Best LIVE Mock Interview - You should go through before Interview
Test your skills through the online practice test: Java 8 Quiz Online Practice Test
Freshers / Beginner level questions & answers
Ques 1. What are 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:
- Lambda Expression
- Stream API
- Default methods in the interface
- Functional Interface
- Optional
- Method references
- Date API
- Nashorn, JavaScript Engine
Is it helpful?
Add Comment
View Comments
Ques 2. Can you explain the syntax of Lambda expression?
So we can divide structure of Lambda expression to three parts:
- Arguments
- Array Token
- Statements
1. Argument list or parameters
Lambda expression can have zero or more arguments. First part before '->' is called as argument list or parameters.
()->{System.out.println("Hello")}; //Without argument, will print hello
(int a)->{System.out.println(a)} //; One argument, will print value of a
(int a,int b)-> {a+b};//two argument, will return sum of these two integers
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.
Is it helpful?
Add Comment
View Comments
Ques 3. What are functional interfaces?
- Functional interfaces are those interfaces which can have only one abstract method. It can have 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.
Is it helpful?
Add Comment
View Comments
Ques 4. What Is a Default Method and When Do We Use It?
A default method is a method with an implementation – which can be found in an interface.
We can use a default method to add a new functionality to an interface while maintaining backward compatibility with classes that are already implementing the interface:
public interface CarBehaviour {
public void move();
default void peep() {
System.out.println("peep!");
}
}
Usually, when a new abstract method is added to an interface, all implementing classes will break until they implement the new abstract method. In Java 8, this problem has been solved by the use of default method.
For example, Collection interface does not have forEach method declaration. Thus, adding such method would simply break the whole collections API.
Java 8 introduces default method so that Collection interface can have a default implementation of forEach method without requiring the classes implementing this interface to implement the same.
Is it helpful?
Add Comment
View Comments
Ques 5. What is Optional and how can we use it?
Optional is a container object used to contain not-null objects. Optional object is used to represent null with absent value. This class has various utility methods to facilitate code to handle values as ‘available’ or ‘not available’ instead of checking null values.
Following is the declaration for java.util.Optional<T> class:
public final class Optional<T> extends Object
Coding Example:
import java.util.Optional;
public class OptionalTester {
public static void main(String args[]) {
OptionalTester optionalTester = new OptionalTester();
Integer value1 = null;
Integer value2 = new Integer(10);
//Optional.ofNullable - allows passed parameter to be null.
Optional<Integer> a = Optional.ofNullable(value1);
//Optional.of - throws NullPointerException if passed parameter is null
Optional<Integer> b = Optional.of(value2);
System.out.println(optionalTester.sum(a,b));
}
public Integer sum(Optional<Integer> a, Optional<Integer> b) {
//Optional.isPresent - checks the value is present or not
System.out.println("First parameter is present: " + a.isPresent());
System.out.println("Second parameter is present: " + b.isPresent());
//Optional.orElse - returns the value if present otherwise returns
//the default value passed.
Integer value1 = a.orElse(new Integer(0));
//Optional.get - gets the value, value should be present
Integer value2 = b.get();
return value1 + value2;
}
}
It should produce the following output:
First parameter is present: false
Second parameter is present: true
10
Is it helpful?
Add Comment
View Comments
Ques 6. 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 context of observer. It denotes current date and time in context of Observer.
Is it helpful?
Add Comment
View Comments
Ques 7. How will you get current date and time using Java 8 Date and TIme API?
You can simply use now() method of LocalDate to get today’s date.
LocalDate currentDate = LocalDate.now();
System.out.println(currentDate);
Output:
2017-09-09
You can use now() method of LocalTime to get current time.
LocalTime currentTime = LocalTime.now();
System.out.println(currentTime);
Output:
23:17:51.817
Is it helpful?
Add Comment
View Comments
Most helpful rated by users:
- What are new features which got introduced in Java 8?
- What are main advantages of using Java 8?
- Can you explain the syntax of Lambda expression?
- What are functional interfaces?
- What Is a Default Method and When Do We Use It?