Java SE 8 Interview Questions and Answers (Part-1)

Filed Under: Interview Questions

In this post, we are going to discuss some important Java SE 8 Interview Questions with Answers. I will write one more post to discuss remaining Java SE 8 Interview Questions.

Java 8 Interview Questions

  1. Why do we need change to Java again?
  2. Java SE 8 New Features?
  3. Advantages of Java SE 8 New Features?
  4. What is Lambda Expression?
  5. What are the three parts of a Lambda Expression? What is the type of Lambda Expression?
  6. What is a Functional Interface? What is SAM Interface?
  7. Is is possible to define our own Functional Interface? What is @FunctionalInterface? What are the rules to define a Functional Interface?
  8. Is @FunctionalInterface annotation mandatory to define a Functional Interface? What is the use of @FunctionalInterface annotation? Why do we need Functional Interfaces in Java?
  9. When do we go for Java 8 Stream API? Why do we need to use Java 8 Stream API in our projects?
  10. Explain Differences between Collection API and Stream API?
  11. What is Spliterator in Java SE 8?Differences between Iterator and Spliterator in Java SE 8?
  12. What is Optional in Java 8? What is the use of Optional?Advantages of Java 8 Optional?
  13. What is Type Inference? Is Type Inference available in older versions like Java 7 and Before 7 or it is available only in Java SE 8?

Java 8 Interview Questions and Answers

In this section, we will pickup each question from previous section and answer it with in-detailed description. If you need any more information and examples, please go through previous Java SE 8 posts available in JournalDEV.

Why do we need change to Java again?

Oracle Corporation has introduced a lot of new concepts in Java SE 8 to introduce the following benefits:

  • To Utilize Current Multi-Core CPUs Efficiently
  • Recently, we can observe drastic changes in Hardware. Now-a-days, all systems are using Multi-Core CPUs (2,4,8,16-Core etc.) to deploy and run their Applications. We need new Programming Constructs in Java to utilize these Multi-Core Processors efficiently to develop Highly Concurrently and Highly Scalable applications.

  • To Utilize FP Features
  • Oracle Corporation has introduced a lot of FP(Functional Programming) concepts as part of Java SE 8 to utilize the advantages of FP.

Java SE 8 New Features?

  • Lambda Expressions
  • Functional Interfaces
  • Stream API
  • Date and Time API
  • Interface Default Methods and Static Methods
  • Spliterator
  • Method and Constructor References
  • Collections API Enhancements
  • Concurrency Utils Enhancements
  • Fork/Join Framework Enhancements
  • Internal Iteration
  • Parallel Array and Parallel Collection Operations
  • Optional
  • Type Annotations and Repeatable Annotations
  • Method Parameter Reflection
  • Base64 Encoding and Decoding
  • IO and NIO2 Enhancements
  • Nashorn JavaScript Engine
  • javac Enhancements
  • JVM Changes
  • Java 8 Compact Profiles: compact1,compact2,compact3
  • JDBC 4.2
  • JAXP 1.6
  • Java DB 10.10
  • Networking
  • Security Changes

Advantages of Java SE 8 New Features?

We can get the following benefits from Java SE 8 New Features:

  • More Concise and Readable code
  • More Reusable code
  • More Testable and Maintainable Code
  • Highly Concurrent and Highly Scalable Code
  • Write Parallel Code
  • Write Database Like Operations
  • Better Performance Applications
  • More Productive code

What is Lambda Expression?

Lambda Expression is an anonymous function which accepts a set of input parameters and returns results.

Lambda Expression is a block of code without any name, with or without parameters and with or without results. This block of code is executed on demand.

What are the three parts of a Lambda Expression? What is the type of Lambda Expression?

A Lambda Expression contains 3 parts:

  • Parameter List
  • A Lambda Expression can contain zero or one or more parameters. It is optional.

  • Lambda Arrow Operator
  • “->” is known as Lambda Arrow operator. It separates parameters list and body.

  • Lambda Expression Body

The type of “Journal Dev” is java.lang.String. The type of “true” is Boolean. In the same way, what is the type of a Lambda Expression?
The Type of a Lambda Expression is a Functional Interface.

Example:- What is the type of the following Lambda Expression?


   () -> System.out.println("Hello World");

This Lambda Expression does not have parameters and does return any results. So it’s type is “java.lang.Runnable” Functional Interface.

What is a Functional Interface? What is SAM Interface?

A Functional Interface is an interface, which contains one and only one abstract method. Functional Interface is also know as SAM Interface because it contains only one abstract method.

SAM Interface stands for Single Abstract Method Interface. Java SE 8 API has defined many Functional Interfaces.

Is is possible to define our own Functional Interface? What is @FunctionalInterface? What are the rules to define a Functional Interface?

Yes, it is possible to define our own Functional Interfaces. We use Java SE 8’s @FunctionalInterface annotation to mark an interface as Functional Interface.

We need to follow these rules to define a Functional Interface:

  • Define an interface with one and only one abstract method.
  • We cannot define more than one abstract method.
  • Use @FunctionalInterface annotation in interface definition.
  • We can define any number of other methods like Default methods, Static methods.
  • If we override java.lang.Object class’s method as an abstract method, which does not count as an abstract method.

Is @FunctionalInterface annotation mandatory to define a Functional Interface? What is the use of @FunctionalInterface annotation? Why do we need Functional Interfaces in Java?

It is not mandatory to define a Functional Interface with @FunctionalInterface annotation. If we don’t want, We can omit this annotation. However, if we use it in Functional Interface definition, Java Compiler forces to use one and only one abstract method inside that interface.

Why do we need Functional Interfaces? The type of a Java SE 8’s Lambda Expression is a Functional Interface. Whereever we use Lambda Expressions that means we are using Functional Interfaces.

When do we go for Java 8 Stream API? Why do we need to use Java 8 Stream API in our projects?

When our Java project wants to perform the following operations, it’s better to use Java 8 Stream API to get lot of benefits:

  • When we want perform Database like Operations. For instance, we want perform groupby operation, orderby operation etc.
  • When want to Perform operations Lazily.
  • When we want to write Functional Style programming.
  • When we want to perform Parallel Operations.
  • When want to use Internal Iteration
  • When we want to perform Pipelining operations.
  • When we want to achieve better performance.

Explain Differences between Collection API and Stream API?

S.No. Collection API Stream API
1. It’s available since Java 1.2 It is introduced in Java SE8
2. It is used to store Data(A set of Objects). It is used to compute data(Computation on a set of Objects).
3. We can use both Spliterator and Iterator to iterate elements. We can use forEach to performs an action for each element of this stream. We can’t use Spliterator or Iterator to iterate elements.
4. It is used to store limited number of Elements. It is used to store either Limited or Infinite Number of Elements.
5. Typically, it uses External Iteration concept to iterate Elements such as Iterator. Stream API uses internal iteration to iterate Elements, using forEach methods.
6. Collection Object is constructed Eagerly. Stream Object is constructed Lazily.
7. We add elements to Collection object only after it is computed completely. We can add elements to Stream Object without any prior computation. That means Stream objects are computed on-demand.
8. We can iterate and consume elements from a Collection Object at any number of times. We can iterate and consume elements from a Stream Object only once.

What is Spliterator in Java SE 8?Differences between Iterator and Spliterator in Java SE 8?

Spliterator stands for Splitable Iterator. It is newly introduced by Oracle Corporation as part Java SE 8.
Like Iterator and ListIterator, It is also one of the Iterator interface.

S.No. Spliterator Iterator
1. It is introduced in Java SE 8. It is available since Java 1.2.
2. Splitable Iterator Non-Splitable Iterator
3. It is used in Stream API. It is used for Collection API.
4. It uses Internal Iteration concept to iterate Streams. It uses External Iteration concept to iterate Collections.
5. We can use Spliterator to iterate Streams in Parallel and Sequential order. We can use Iterator to iterate Collections only in Sequential order.
6. We can get Spliterator by calling spliterator() method on Stream Object. We can get Iterator by calling iterator() method on Collection Object.
7. Important Method: tryAdvance() Important Methods: next(), hasNext()

What is Optional in Java 8? What is the use of Optional?Advantages of Java 8 Optional?

Optional:
Optional is a final Class introduced as part of Java SE 8. It is defined in java.util package.

It is used to represent optional values that is either exist or not exist. It can contain either one value or zero value. If it contains a value, we can get it. Otherwise, we get nothing.

It is a bounded collection that is it contains at most one element only. It is an alternative to “null” value.

Main Advantage of Optional is:

  • It is used to avoid null checks.
  • It is used to avoid “NullPointerException”.

What is Type Inference? Is Type Inference available in older versions like Java 7 and Before 7 or it is available only in Java SE 8?

Type Inference means determining the Type by compiler at compile-time.

It is not new feature in Java SE 8. It is available in Java 7 and before Java 7 too.

Before Java 7:-
Let us explore Java arrays. Define a String of Array with values as shown below:


String str[] = { "Java 7", "Java 8", "Java 9" };

Here we have assigned some String values at right side, but not defined it’s type. Java Compiler automatically infers it’s type and creates a String of Array.

Java 7:-
Oracle Corporation has introduced “Diamond Operator” new feature in Java SE 7 to avoid unnecessary Type definition in Generics.


Map<String,List<Customer>> customerInfoByCity = new HashMap<>();

Here we have not defined Type information at right side, simply defined Java SE 7’s Diamond Operator “”.

Java SE 8:-
Oracle Corporation has enhanced this Type Inference concept a lot in Java SE 8. We use this concept to define Lambda Expressions, Functions, Method References etc.


ToIntBiFunction<Integer,Integer> add = (a,b) -> a + b;

Here Java Compiler observes the type definition available at left-side and determines the type of Lambda Expression parameters a and b as Integers.

That’s it about Java 8 Interview Questions.

I have discussed some Java SE 8 Interview Questions in this post and will discuss some more Java SE 8 Interview Questions in my coming posts.

Please drop me a comment if you like my post or have any issues/suggestions.

Comments

  1. vineet says:

    can anybody explain why Integer add=(a,b)-> a+b; this will not complie?
    ToIntBiFunction add = (a,b) -> a + b; this will compile successfully

    1. Anup Singh says:

      You can not do it because type of Lambda expression is not an Integer but a functional interface.. So the code should be Functional_Interface var = Lambda Expression; I hope it helps. Lambda expression can be passed as parameter only where a method expects a functional interface as input. I hope it help.

  2. sandy says:

    3. It is used in Stream API.-> you mentioned this point in differances between iterator and spliteratior
    We can’t use Spliterator or Iterator to iterate elements.-> you mentioned this point in diff between collection api and stream api which one is correct are we using spliterator in Stream api?

  3. Manish Kumar says:

    This is about Type of Lambda, I think Lambda does not have any type, until It get assigned to variable. As we can see
    () -> System.out.println(“Hello World”); can be assigned to any functional interface which have
    public abstract void xxx() method. It is not specific to “java.lang.Runnable”.

  4. Chandrakanth says:

    Hi

    Very good points to note on.

    But , I think @FunctionlInterface is mandatory to define Functional Interface. The last point in the answer of this question states the definition of the same.

    Thank You

    1. Manish Kumar says:

      @FunctionlInterface enforces interface to have only one abstract method so it is mandatory for functional interface,
      But the point is Lambda can be assigned to any interface which have single abstract method, So the lambda can be used with even those interfaces which is not marked as functional.

  5. Gundamaiah says:

    Nice Questions.

    There is one typo in the answer while comparing Spliterator and Iterator. You mentioned Spilterator instead of Iterator in 5th point.

    1. Pankaj says:

      Thanks for noticing the typo error, I have corrected it.

  6. Pramod says:

    small mistake

    while mentioning differences between stream and collection . You have mentioned We can use both Spliterator and Iterator to iterate elements.. in both places. Please correct it.

    1. Pankaj says:

      Thanks for pointing it out, i have fixed it.

      1. Prashant Jain says:

        Also there is mistake which says StreamAPI uses “External iteration”, it uses internal iteration using splititerator , fix that as well.

        1. Pankaj says:

          Thanks for pointing it out, I have fixed the typo error.

  7. Rohi says:

    some mistake in content … for stream you have mentioned …It uses External Iteration to iterate Elements.

    1. Tanaya says:

      I agree , stream uses internal iteration, not external

      1. Pankaj says:

        Thanks friends for the comments, I rechecked it and corrected the error. Thanks Again.

  8. Indiver says:

    Very helpful!!
    Keep it up

  9. Uma says:

    Nice one Sir, Keep posting!

  10. vishal says:

    Nice information, In some mins get to know new concepts of java8 has introduced.

  11. Ruwan says:

    It’s a great introduction. Thanks

  12. Viswanathan says:

    Nice article to know Java 8 features

  13. srao says:

    Very Good. I like the way you present the questions and Answers straight and clear,

  14. symbion says:

    Hi there,

    I like your site very much, lot to learn! Just one question:

    Isn’t Stream API the one uses Internal Iteration? In Part2, your first property of Internal Iteration is that it is intruduced in Java 8, so Collections are supposed to iterate Externally, right?

    Thx

  15. Rattan says:

    Nice article, much useful.

    Just one thing I noticed though. Below would not compile :
    Integer add = (a,b) -> a + b;

    Because add must be of type functional interface.

    1. Soumen says:

      Very Nice Article.

      As mentioned earlier
      Integer add=(a,b)-> a+b;
      will not compile.

      Instead of Integer add, it should be ToIntBiFunction add as ToIntBiFunction functional interface takes two integer parameter.

      ToIntBiFunction add = (a,b) -> a + b;
      System.out.println(add.applyAsInt(new Integer(10),new Integer(10)));

      Thanks!!!

      1. Rambabu says:

        Thanks good catch. Updated

  16. Rajib says:

    Good !!!!!!!!!!!!!!!!!

  17. sushil says:

    Good One

    1. Rajat says:

      it’s nice..thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages