Prepare Interview

Exams Attended

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Java 8 Interview Questions and Answers

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:

Related differences

Java 7 vs Java 8Java 8 vs Java 9

Related interview subjects

JUnit interview questions and answers - Total 24 questions
Spring Framework interview questions and answers - Total 53 questions
Java Design Patterns interview questions and answers - Total 15 questions
Core Java interview questions and answers - Total 306 questions
Tomcat interview questions and answers - Total 16 questions
Apache Wicket interview questions and answers - Total 26 questions
Java Applet interview questions and answers - Total 29 questions
JAXB interview questions and answers - Total 18 questions
JMS interview questions and answers - Total 64 questions
Log4j interview questions and answers - Total 35 questions
Struts interview questions and answers - Total 84 questions
RMI interview questions and answers - Total 31 questions
Apache Camel interview questions and answers - Total 20 questions
JDBC interview questions and answers - Total 27 questions
Java 11 interview questions and answers - Total 24 questions
JPA interview questions and answers - Total 41 questions
EJB interview questions and answers - Total 80 questions
GWT interview questions and answers - Total 27 questions
Kotlin interview questions and answers - Total 30 questions
Glassfish interview questions and answers - Total 8 questions
Google Gson interview questions and answers - Total 8 questions
JSP interview questions and answers - Total 49 questions
J2EE interview questions and answers - Total 25 questions
Apache Tapestry interview questions and answers - Total 9 questions
Java Swing interview questions and answers - Total 27 questions
Java Mail interview questions and answers - Total 27 questions
Hibernate interview questions and answers - Total 52 questions
JSF interview questions and answers - Total 24 questions
Java 8 interview questions and answers - Total 30 questions
Java 15 interview questions and answers - Total 16 questions
JBoss interview questions and answers - Total 14 questions
Web Services interview questions and answers - Total 10 questions
RichFaces interview questions and answers - Total 26 questions
Servlets interview questions and answers - Total 34 questions
Java Beans interview questions and answers - Total 57 questions
Spring Boot interview questions and answers - Total 50 questions
©2023 WithoutBook