Top Java 8 Interview Questions and Answers

This post contains interview questions related to new features introduced in java 8 .

 

  1. What is a lambda expression ?
  2. What are advantages of lambda expression ?
  3. What is a functional interface ?
  4. What is the type of a lambda expression ?
  5. What is the target type of a lambda expression ?
  6. What is the difference between an interface with default method and an abstract class ?
  7. Are the abstract classes still useful ?
  8. What is the difference between a predicate and a function ?
  9. What is the difference between a collection and a stream ?
  10. What is the difference between intermediate and terminal operations in streams ?
  11. What is a Consumer in Java 8 ?
  12. What is a Supplier in Java 8?
  13. What is a Parallel Stream in Java 8?
  14. What is a flatMap in Java 8?

 
Interview questions topjavatutorial
 

 Q-1 What is lambda expression ?

 

Ans:

A lambda expression is a method without a name, access specifier or return value declaration, i.e an anonymous method.

 

The following model represents the general structure of a lambda expression:
(argument list) -> { body }
 
Read more about lambda expressions.

 

Q-2 : What are advantages of lambda expression ?

 

Ans:

We can pass a lambda expression as an object to a method. This reduces the overhead involved in passing an anonymous class.

We can also pass a method as a parameter to another method using lambda expressions.

 

Q-3 : What is a functional interface ?

 

Ans:

A functional interface is an interface with only one abstract method.

The main advantage of functional interface is that it can be used to refer to a lambda expression or as a method interface.

These are needed to execute a lambda expression or method.
 
Read more on Functional Interface in Java 8

 

Q-4 : What is the type of a lambda expression ?

 

Ans:

The type of a lambda expression depends on the context it is being used.

(int x,int y) -> {return x+y;}

 

Often, the type is inferred by the Java compiler.

 

Q-5 : What is the target type of a lambda expression ?

 

Ans:

The target type of a lambda expression represents a type to which the expression can be converted.

All lambda expressions are converted to a functional interface type. So, the target type is functional interface.

 

Q-6: What is the difference between an interface with default method and an abstract class ?

 

Ans:

When we add default method to an interface, it looks like an abstract class, but they are not the same.

An abstract class can have constructors, instance variables, concrete methods, but we can’t have instance variables or constructors in the interface.

An interface with single default method can be used to refer to a lambda expression, but an abstract class cannot be used to refer to lambda expressions.

 

Q-7: Are the abstract classes still useful?

 
Abstract classes can still do more in comparison to Java 8 interfaces:
 

  1. Abstract class can have a constructor. The interface has no constructors to be invoked by the descendants
  2.  

  3. Abstract classes are more structured and can hold a state.
    In comparison, Interface methods are all public, field members are all constants (final & public). You may want to restrict access privileges of methods and/or make them operate on non-constant state.
  4.  

  5. A child class can call upon the abstract class method(s) by super, while it can not do so on default interface methods.
  6.  

  7. Type clarity:

    You can only extend one class. This makes it clearer what your object is an how to use it.

 

Q-8 : What is the difference between a predicate and a function ?

 

Ans :

A predicate takes one argument and returns a boolean value.

A function takes one argument and returns an object.

Both are useful for evaluating lambda expressions.

 

Q-9 : What is the difference between a collection and a stream ?

 

Ans :

A collection contains group of objects in memory.

A stream is specific to lambda expressions. Stream contains objects taken from collection that can be easily acted on using lambda expressions.

 

Q-10 : What is the difference between intermediate and terminal operations in streams ?

 

Ans:

Intermediate operations return a stream as the result of the operation.

Terminal operations return a value as result.

 

Q-11 : What is a Consumer ?

 
Ans:
Consumer is a functional interface added in Java 8 that has a single abstract method accept().

A Consumer is used when you want to do perform an operation that takes a parameter but doesn’t return anything.

Consumer<String> print = System.out::println;
print.accept("abc");

 

Q-12 : What is a Supplier ?

 
Ans:
Supplier is a functional interface with a single abstract method get().

A Supplier is used when you want to generate instances without taking any input.

Supplier<LocalDate> s1 = LocalDate::now;
System.out.println(s1.get());

 

Q-13 : What is a Parallel Stream?

Ans:
A parallel stream uses multiple threads to process data concurrently.

We can use parallel() to create parallel stream from existing sequential stream.

Stream<String> stream = Stream.of("Germany", "England", "China","Denmark", "Brazil");
Stream<String> parallelStream = stream.parallel();

We can also use parallelStream() method to create a parallel stream from a collection :
Stream<String> parallelStream2 = Arrays.asList("Germany", "England",
                "China", "Denmark", "Brazil").parallelStream();

 

Q-14 : What is a flatMap in Java 8?

Ans:
Stream flatMap is a combination of two operations .. a map operation and a flatten operation.

To understand this, consider an example of multiple lists of elements like {a,b}, {c,d,e},.. etc.

Now, if we want to get all the elements in sequence, we can’t use a map since we have a non-flat structure here.

For this, we can use flatMap() to flatten it to a structure like {a,b,c,d,e,.. } .

In this example, we have a Stream of multiple lists of numbers and we are looking to get numbers from it in sequence.

Stream<List<Integer>> numberStream = Stream.of(
        Arrays.asList(1, 2, 3, 4), Arrays.asList(5, 6, 7, 8));
 
numberStream.flatMap(n -> n.stream())
            .filter(n -> n % 2 == 0)
            .forEach(System.out::println);

Output :

2
4
6
8
 

Next

Java 8 Interview Questions – Part 2

 

More on Java 8

 

Here is a summary of the Java8 new features for your reference :

Java 8 new features

 

Also, here are some interesting quizzes on java8 features/ lambda expression that you may like :

Java quiz 8 (lambda expression)
 
Java 8 quiz on multiple inheritance with Default Interface Method
 

© 2015 – 2023, https:. All rights reserved. On republishing this post, you must provide link to original post

3 comments

  1. […] Java8 Interview questions […]

  2. […] Java8 Interview questions […]

Leave a Reply.. code can be added in <code> </code> tags

%d bloggers like this: