Tutorial

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

Published on August 3, 2022
Default avatar

By Rambabu Posa

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

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

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 the 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 pick up each question from the previous section and answer it with an 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. Nowadays, 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 the 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 known 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 SE 8
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 unlimited number of elements. Stream API is used to process on the elements of a Collection.
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 are 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 the “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 a 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 the right side, but not defined its type. Java Compiler automatically infers its 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 the 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 the left-side and determines the type of Lambda Expression parameters a and b as Integers. That’s all 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.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Rambabu Posa

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
January 8, 2020

In Question 10 you had mentioned that We cannot use spliterator in Stream API and in Question 11 you mentioned Spliterator used in Stream API to iterator the elements. which one is correct ?

- Santosh

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    January 5, 2020

    Article is really good. Complex concepts are explained in simple words. Appreciate. It could add more value to every answer if you could add simple code example in it

    - Tejas Sawant

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      December 3, 2019

      how to know the type of lambda expression. you are saying that the functional interface for this ()->System.out.println(“Hello World”); is Runnable interface. how?

      - Nagesh kumar

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        May 29, 2018

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

        - vineet

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          May 5, 2018

          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?

          - sandy

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            January 7, 2018

            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".

            - Manish Kumar

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              October 4, 2017

              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

              - Chandrakanth

                JournalDev
                DigitalOcean Employee
                DigitalOcean Employee badge
                September 27, 2017

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

                - Gundamaiah

                  JournalDev
                  DigitalOcean Employee
                  DigitalOcean Employee badge
                  August 24, 2017

                  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.

                  - Pramod

                    JournalDev
                    DigitalOcean Employee
                    DigitalOcean Employee badge
                    August 19, 2017

                    Good one

                    - Deb

                      Try DigitalOcean for free

                      Click below to sign up and get $200 of credit to try our products over 60 days!

                      Sign up

                      Join the Tech Talk
                      Success! Thank you! Please check your email for further details.

                      Please complete your information!

                      Get our biweekly newsletter

                      Sign up for Infrastructure as a Newsletter.

                      Hollie's Hub for Good

                      Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

                      Become a contributor

                      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                      Welcome to the developer cloud

                      DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

                      Learn more
                      DigitalOcean Cloud Control Panel