Java SE 8 Interview Questions and Answers - 1

From OasisSoftTech.com - Knowledge Base/Java/Springframework/Microservices/Cloud-AWS/AI
Revision as of 04:10, 6 July 2018 by Rasimsen (talk | contribs)
Jump to: navigation, search

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

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?

COLLECTION APISTREAM API
It’s available since Java 1.2It is introduced in Java SE8
It is used to store Data(A set of Objects).It is used to compute data(Computation on a set of Objects).
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.
It is used to store limited number of Elements.It is used to store either Limited or Infinite Number of Elements.
Typically, it uses External Iteration concept to iterate Elements such as Iterator.Stream API uses External Iteration to iterate Elements, using forEach methods.
Collection Object is constructed Eagerly.Stream Object is constructed Lazily.
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.
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.