Prepare Interview

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

Related differences

Java 7 vs Java 8Java 8 vs Java 9

Ques 6. How lambda expression and functional interfaces are related?

Lambda expressions can only be applied to abstract method of functional interface.
For example:
Runnable has only one abstract method called run, so it can be used as below:
// Using lambda expression
Thread t1=new Thread(
  ()->System.out.println("In Run method")
);

Here we are using Thread constructor which takes Runnable as 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.
Thread t1=new Thread(new Runnable() { 
   @Override
   public void run() {
      System.out.println("In Run method");
   }
});

Is it helpful? Add Comment View Comments
 

Ques 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 interface named "Readable" as below:
public interface Readable {
    void read();
    default void readBook()
    {
        System.out.println("Reading the book.");
    }
}

Create main class named "MyFunctionalInteface":
public class MyFunctionalInteface {
    public static void main(String[] args)
    {
        MyFunctionalInteface myFuncInterface = new MyFunctionalInteface();
        myFuncInterface.readMyBook(() -> System.out.println("Reading my book"));
    }
    public void readMyBook(Readable p)
    {
        p.read();
    }
}

When you run above program, you will get below output:
Reading my book

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

Is it helpful? Add Comment View Comments
 

Ques 8. What is method reference in java 8?

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

Is it helpful? Add Comment View Comments
 

Ques 9. 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 10. 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
 

Most helpful rated by users:

©2024 WithoutBook