TOP 20+ Java 8 Features Interview Questions

Java 8 Insterview Questions

Top 20+ Java 8 Features Interview Questions and Answer. Common Asking Java interview questions Introduction of Java 8.


Introduction to Java 8 Interview Questions And Answer

Java 8 is a replacement platform that is newly released with new libraries and classes. Mostly these features target having a cleaner and compact code. 

The new features added during this version is listed as Lambda Expressions, method references, optional, functional interface, and default methods, Nashorn, Stream, and Date API. the requirement for change in Java was to utilize Current Multi-Core CPUs Efficiently and to utilize FP Features.

Features of java 8

  • Java 8 was released on 18th march 2014
  • Functional interfaces and Lambda expression.
  • Stream API
  • forEach() Method in Iterable interface
  • Default and static method in interface
  • Collection API enhancement
  • Concurrency API enhancement
  • JO API enhancement
  • Java Date and Time API enhacement
  • Functional interfaces and Lambda expression.
  • Interface with one abstract method only known as functional interface.
  • It can have any number of default and static method.
  • Method from object class can be declared also in functional interface.
  • Functional interface also know as Single Abstract Method Interfaces or SAM Interfaces.
  • The Lambda expressions are used to provide the implementation of a functional interface.
  • Lambda expressions in java is like a function and class file will not be created for them.

Stream API in Java 8 Interview 

java util stream API added in Java 8 which has method like filter, map, sorted, forEach and reduce etc. to work with source which can be aa Collection, IO Operation or Arrays who provides input to a Stream.

Operation performed in stream does not modify its original source.

Majority of Stream API method are having arguments as functional interfaces, so lambda expressions has very vast scope here.

Collection, Concurrency, Time and IO APl enhancement

In Java 8 Time API has been added which provides thread safety and internationalization.

Other than forEach method and stream API method like forEachRemaining, removelf, spliterator, replaceAll, compute and merge method.

Few new methods like compute, forEach,
forEachEntry, forEachKey, forEachValue, merge, reduce and search methods.
Files.

List(Path dirPath), Files. lines(Path path), File. find()and BufferedReader. lines() are added in IO package.

Java 8 Interview Questions 


What are the new features of java 8

  • Default and static methods in Interfaces.
  • Lambda Expressions
  • Functional Interface
  • forEach() method
  • Stream API
  • LocalDate, LocalTime, LocalDateTime


Why default method introduced in interfaces?

  • Before Java8, interfaces contains only abstract method. All methods are by default public and abstract.

  • With Java 8, default method is introduced.
  • This helps in achieving addition of a new method in interfaces without need to change existing classes.

What are the features of Java 8, you have used in your project?

  • LocalDate, Stream Api, Lambda Expression, forEach(), @FunctionalInterface.

What is the advantage of Using new Date Api?

  • Before Java 8, we do not have support for timezone. We have to programmatically change the time-zone.
  • Java 8, introduced support for timezone.
  • Important classes most frequently used:
  • LocalDate, LocalDate Time, Zoned Date Time

Other classes are :
  • Clock, LocalTime, Duration

How we can iterate the list and print each element ?

List<lnteger> list = new ArrayList<>0;
           // list: 1,2,3,4,5,6,7,8,9,10,11]
for (Integer elem : list){
            System.out.println(elem);
}
list.forEach( elem->{ System.out.printin (elem)});

What is identifiers lambda expression?

  • Lambda expressions are, basically functions with no name and identifiers.
  • Promotes functional programming.
  • It helps in expressing instances of single method class more compactly.

Do you know what are the various forms of writing lambda expressions?

  • 0-> expression
  • (parameters)-> expression
  • (parameters)-> { multiple statements}

Can you print User details through lambda expression?

List<User> list = new ArrayList<>();
/*1001 Users are added to list*/
list.forEach( user->{
                  System.out.printin('user id " +   User.getld0 );
                  System.out.printin("user name:"+User.getName();
@Data
          Userf{
                        private String id;
                        private String name;
          }

What do you understand by @Functional Interfacee annotation in java 8?

  • @Functional Interface, if used, will force the compiler to check whether the given interface has single-abstract method or not.
  • If not, compiler will throw error "Unexpectedd @Functionalinterface annotation"

How do you create a custom annotation in java?

  • @Retention(Retention Policy.RUNTIME)
  • @Target(ElementType.Type)

public @interface ByteProgramming {
                       String username() default "Byte Programming";
}
@ByteProgramming(username= "Byte")

How to avoid NullPointer exception in java 8?

  • Java 8 provides concept of Optionals.
  • Optionals can be used to avoid NulliPointer Exception

             Ex: String value = null;
            
  • OptionaleString> value = Optional.empty0;

What do you mean by stream?

  • Streams are just sequence of data from a source, With Java8, we can do manipulation on data using stream api.
  • Example: Youtube is a streaming platform

Can you tell me difference between Collection API and Stream API?

Collection APl:
  • Elements are not computed on demand basis.
  • Indexed Access possible.
  • Iteration was external.
  • Access of elements was sequential.

Stream APl:
  • Elements are computed on demand basis.
  • Indexed access not possible.
  • Iteration is internal.
  • Access of elements could be sequential as well as parallel.
  • An operation on stream does not change its original source.

What are the functional interfaces you have used in your project, introduced with java 8?

Functional interfaces like Function, Consumer, Supplier, Predicate
All these are present as part of. java.util.function package

What is stream pipelining?


  • Chaining of stream operations.
  • Stream operations are either Terminal or Intermediate operations.
  • Intermediate operations returns instance of stream.
  • At end, there must be terminal operations to end the chaining.

Can you show all employees from the Iist, having age greater than 30 with java 8?

@Data
public class Employee {
                  
              // 3000 employees are greater.
            // than 30 age out of 10000
                  
           private long id;
           private String name;
           private long age;
}
list.stream0filter( empl-> empl.getAge0> 30) forEach( empl->{
            System.out.printin(empl);
          
});

Post a Comment

0 Comments