java_interview_questions

Java 8 Interview Questions and Answers

Q. What are the important features of Java 8 release?

↥ back to top

Q. Can you declare an interface method static?

Java 8 interface changes include static methods and default methods in interfaces. Prior to Java 8, we could have only method declarations in the interfaces. But from Java 8, we can have default methods and static methods in the interfaces.

Q. What is a lambda? What is the structure and features of using a lambda expression?

A lambda is a set of instructions that can be separated into a separate variable and then repeatedly called in various places of the program.

The basis of the lambda expression is the lambda operator , which represents the arrow ->. This operator divides the lambda expression into two parts: the left side contains a list of expression parameters, and the right actually represents the body of the lambda expression, where all actions are performed.

The lambda expression is not executed by itself, but forms the implementation of the method defined in the functional interface. It is important that the functional interface should contain only one single method without implementation.

interface  Operationable {
     int  calculate ( int  x , int  y );
}

public  static  void main ( String [] args) {
    Operationable operation = (x, y) - > x + y;     
    int result = operation.calculate ( 10 , 20 );
    System.out.println (result); // 30 
}

In fact, lambda expressions are in some way a shorthand form of internal anonymous classes that were previously used in Java.

public static void main ( String [] args) { Printable printer = s - > System.out.println(s); printer.print(“Hello, world”); }

// _ Block lambda - expressions_ are surrounded by curly braces . The modular lambda - expressions can be used inside nested blocks, loops, design the if ` switch statement ‘, create variables, and so on . d . If you block a lambda - expression must return a value, it explicitly applies `statement return statement ‘ :

Operationable operation = ( int x, int y) - > {
if (y == 0 ) { return 0 ; } else { return x / y; } };

* Passing a lambda expression as a method parameter
```java
interface  Condition {
     boolean  isAppropriate ( int  n );
}

private  static  int sum ( int [] numbers, Condition condition) {
     int result =  0 ;
    for ( int i : numbers) {
         if (condition.isAppropriate(i)) {
            result + = i;
        }
    }
    return result;
}

public  static  void main ( String [] args) {
     System.out.println(sum ( new  int [] { 0 , 1 , 0 , 3 , 0 , 5 , 0 , 7 , 0 , 9 }, (n) - > n ! =  0 ));
} 
↥ back to top

Q. What variables do lambda expressions have access to?

Access to external scope variables from a lambda expression is very similar to access from anonymous objects.

The default methods of the implemented functional interface are not allowed to be accessed inside the lambda expression.

Q. How to sort a list of strings using a lambda expression?

public  static  List < String > sort ( List < String > list) {
    Collections.sort(list, (a, b) -> a.compareTo(b));
    return list;
}

Q. What is a method reference?

If the method existing in the class already does everything that is necessary, then you can use the method reference mechanism (method reference) to directly pass this method. The result will be exactly the same as in the case of defining a lambda expression that calls this method.

private  interface  Measurable {
    public  int  length ( String  string );
}

public  static  void main ( String [] args) {
    Measurable a =  String::length;
    System.out.println(a.length("abc"));
}

Method references are potentially more efficient than using lambda expressions. In addition, they provide the compiler with better information about the type, and if you can choose between using a reference to an existing method and using a lambda expression, you should always use a method reference.

Q. What types of method references do you know?

Q. Explain the expression System.out::println

The specified expression illustrates passing a reference to a static method of a println()class System.out.

Q. What is a Functional Interface?

A functional interface is an interface that defines only one abstract method.

To accurately determine the interface as functional, an annotation has been added @FunctionalInterface that works on the principle of @Override. It will designate a plan and will not allow to define the second abstract method in the interface.

An interface can include as many default methods as you like while remaining functional, because default methods are not abstract.

Q. What is StringJoiner?

The class is StringJoinerused to create a sequence of strings separated by a separator with the ability to append a prefix and suffix to the resulting string:

StringJoiner joiner =  new  StringJoiner ( " . " , " Prefix- " , " -suffix " );
for ( String s :  " Hello the brave world " . split ( "  " )) {
    , joiner, . add (s);
}
System.out.println(joiner); // prefix-Hello.the.brave.world-suffix

Q. What are defaultinterface methods?

Java 8 allows you to add non-abstract method implementations to an interface using the keyword default:

interface  Example {
    int  process ( int  a );
    default void  show () {
        System.out.println("default show ()");
    }
}
↥ back to top

Q. How to call default interface method in a class that implements this interface?

Using the keyword superalong with the interface name:

interface  Paper {
    default void  show () {
        System.out.println(" default show ()");
    }
}

class  License  implements  Paper {
     public  void  show () {
        Paper.super.show();
    }
}

Q. What is static interface method?

Static interface methods are similar to default methods, except that there is no way to override them in classes that implement the interface.

Q. How to call static interface method?

Using the interface name:

interface  Paper {
     static  void  show () {
         System.out.println( " static show () " );
    }
}

class  License {
     public  void  showPaper () {
         Paper.show ();
    }
}

Q. What is Optional

An optional value Optionalis a container for an object that may or may not contain a value null. Such a wrapper is a convenient means of prevention NullPointerException, as has some higher-order functions, eliminating the need for repeating if null/notNullchecks:

Optional < String > optional =  Optional . of ( " hello " );

optional.isPresent(); // true 
optional.ifPresent(s ->  System.out.println(s . length ())); // 5 
optional.get(); // "hello" 
optional.orElse( " ops ... " ); // "hello"

Q. What is Stream?

An interface java.util.Stream is a sequence of elements on which various operations can be performed.

Operations on streams can be either intermediate (intermediate) or final (terminal) . Final operations return a result of a certain type, and intermediate operations return the same stream. Thus, you can build chains of several operations on the same stream.

A stream can have any number of calls to intermediate operations and the last call to the final operation. At the same time, all intermediate operations are performed lazily and until the final operation is called, no actions actually happen (similar to creating an object Threador Runnable, without a call start()).

Streams are created based on sources of some, for example, classes from java.util.Collection.

Associative arrays (maps), for example HashMap, are not supported.

Operations on streams can be performed both sequentially and in parallel.

Streams cannot be reused. As soon as some final operation has been called, the flow is closed.

In addition to the universal object, there are special types of streams to work with primitive data types int, longand double: IntStream, LongStreamand DoubleStream. These primitive streams work just like regular object streams, but with the following differences:

↥ back to top

Q. What are the ways to create a stream?

Q. What is the method collect()for streams for?

A method collect()is the final operation that is used to represent the result as a collection or some other data structure.

collect()accepts an input that contains four stages:

In Java 8, the class Collectors implements several common collectors:

There is also the possibility of creating your own collector through Collector.of():

Collector < String , a List < String > , a List < String > > toList =  Collector.of (
    ArrayList :: new ,
    List :: add,
    (l1, l2) -> {l1 . addAll (l2); return l1; }
);
↥ back to top

Q. Why do streams use forEach()and forEachOrdered() methods?

Q. What are map(), mapToInt(), mapToDouble() and mapToLong() methods in Stream?

The method map()is an intermediate operation, which transforms each element of the stream in a specified way.

mapToInt(), mapToDouble(), mapToLong()- analogues map(), returns the corresponding numerical stream (ie the stream of numerical primitives):

Stream 
    .of ( " 12 " , " 22 " , " 4 " , " 444 " , " 123 " )
    .mapToInt ( Integer :: parseInt)
    .toArray (); // [12, 22, 4, 444, 123]

Q. What is the purpose of filter() method in streams?

The method filter() is an intermediate operation receiving a predicate that filters all elements, returning only those that match the condition.

Q. What is the use of limit() method in streams?

The method limit()is an intermediate operation, which allows you to limit the selection to a certain number of first elements.

Q. What is the use of sorted() method in streams?

The method sorted()is an intermediate operation, which allows you to sort the values ​​either in natural order or by setting Comparator.

The order of the elements in the original collection remains untouched - sorted()it just creates its sorted representation.

Q. What streamers designed methods flatMap(), flatMapToInt(), flatMapToDouble(), flatMapToLong()?

The method flatMap() is similar to map, but can create several from one element. Thus, each object will be converted to zero, one or more other objects supported by the stream. The most obvious way to use this operation is to convert container elements using functions that return containers.

Stream 
    .of ( " Hello " , " world! " )
    .flatMap ((p) ->  Arrays.stream (p . split ( " , " )))
    .toArray ( String [] :: new ); // ["H", "e", "l", "l", "o", "w", "o", "r", "l", "d", "!"]

flatMapToInt(), flatMapToDouble(), flatMapToLong()- are analogues flatMap(), returns the corresponding numerical stream.

↥ back to top

Q. Tell us about parallel processing in Java 8?

Streams can be sequential and parallel. Operations on sequential streams are performed in one processor thread, on parallel streams - using several processor threads. Parallel streams use the shared stream ForkJoinPoolthrough the static ForkJoinPool.commonPool()method. In this case, if the environment is not multi-core, then the stream will be executed as sequential. In fact, the use of parallel streams is reduced to the fact that the data in the streams will be divided into parts, each part is processed on a separate processor core, and in the end these parts are connected, and final operations are performed on them.

You can also use the parallelStream()interface method to create a parallel stream from the collection Collection.

To make a regular sequential stream parallel, you must call the Streammethod on the object parallel(). The method isParallel()allows you to find out if the stream is parallel.

Using, methods parallel()and sequential()it is possible to determine which operations can be parallel, and which only sequential. You can also make a parallel stream from any sequential stream and vice versa:

collection
  .stream ()
  .peek ( ... ) // operation is sequential
  .parallel ()
  .map ( ... ) // the operation can be performed in parallel,
  .sequential ()
  .reduce ( ... ) // operation is sequential again

As a rule, elements are transferred to the stream in the same order in which they are defined in the data source. When working with parallel streams, the system preserves the sequence of elements. An exception is a method forEach()that can output elements in random order. And in order to maintain the order, it is necessary to apply the method forEachOrdered().

↥ back to top

Q. What intermediate methods of working with streams do you know?

For numerical streams, an additional method is available mapToObj()that converts the numerical stream back to the object stream.

Q. How to display 10 random numbers using forEach()?

( new  Random ())
    .ints ()
    .limit ( 10 )
    .forEach ( System . out :: println);

Q. How can I display unique squares of numbers using the method map()?

Stream 
    .of ( 1 , 2 , 3 , 2 , 1 )
    .map (s -> s * s)
    .distinct ()
    .collect ( Collectors . toList ())
    .forEach ( System . out :: println);

Q. How to display the number of empty lines using the method filter()?

System.out.println (
     Stream 
        .of ( " Hello " , " " , " , " , " world " , " ! " )
        .filter ( String :: isEmpty)
        .count ());

Q. How to display 10 random numbers in ascending order?

( new  Random ())
    .ints ()
    .limit ( 10 )
    .sorted ()
    .forEach ( System . out :: println);

Q. How to find the maximum number in a set?

Stream 
    .of ( 5 , 3 , 4 , 55 , 2 )
    .mapToInt (a -> a)
    .max ()
    .getAsInt (); // 55
↥ back to top

Q. How to find the minimum number in a set?

Stream 
    .of ( 5 , 3 , 4 , 55 , 2 )
    .mapToInt (a -> a)
    .min ()
    .getAsInt (); // 2

Q. How to get the sum of all numbers in a set?

Stream 
    .of( 5 , 3 , 4 , 55 , 2 )
    .mapToInt()
    .sum(); // 69

Q. How to get the average of all numbers?

Stream 
    .of ( 5 , 3 , 4 , 55 , 2 )
    .mapToInt (a -> a)
    .average ()
    .getAsDouble (); // 13.8

Q. What additional methods for working with associative arrays (maps) appeared in Java 8?

↥ back to top

Q. What is LocalDateTime?

LocalDateTimecombines together LocaleDateand LocalTimecontains the date and time in the calendar system ISO-8601 without reference to the time zone. Time is stored accurate to the nanosecond. It contains many convenient methods such as plusMinutes, plusHours, isAfter, toSecondOfDay, etc.

Q. What is ZonedDateTime?

java.time.ZonedDateTime- an analogue java.util.Calendar, a class with the most complete amount of information about the temporary context in the calendar system ISO-8601. It includes a time zone, therefore, this class carries out all operations with time shifts taking into account it.

Q. How to get current date using Date Time API from Java 8?

LocalDate as.now();

Q. How to add 1 week, 1 month, 1 year, 10 years to the current date using the Date Time API?

LocalDate as.now ().plusWeeks ( 1 );
LocalDate as.now ().plusMonths ( 1 );
LocalDate as.now ().plusYears ( 1 );
LocalDate as.now ().plus ( 1 , ChronoUnit.DECADES );

Q. How to get the next Tuesday using the Date Time API?

LocalDate as.now().with( TemporalAdjusters.next ( DayOfWeek.TUESDAY ));

Q. How to get the current time accurate to milliseconds using the Date Time API?

new  Date ().toInstant ();

Q. How to get the second Saturday of the current month using the Date Time API?

LocalDate 
    .of ( LocalDate.Now ().GetYear (), LocalDate.Now ().GetMonth (), 1 )
    .with ( TemporalAdjusters.nextOrSame ( DayOfWeek.SATURDAY ))
    .with ( TemporalAdjusters.next ( DayOfWeek.SATURDAY ));
↥ back to top

Q. How to get the current time in local time accurate to milliseconds using the Date Time API?

LocalDateTime.ofInstant ( new  Date().toInstant(), ZoneId.systemDefault());

Q. How to determine repeatable annotation?

To define a repeatable annotation, you must create a container annotation for the list of repeatable annotations and designate a repeatable meta annotation @Repeatable:

@interface  Schedulers {
    Scheduler [] value ();
}

@Repeatable ( Schedulers . Class)
 @interface  Scheduler {
    String  birthday () default "Jan 8 2000";
 }

Q. What is Nashorn?

Nashorn is a JavaScript engine developed in Java by Oracle. Designed to provide the ability to embed JavaScript code in Java applications. Compared to Rhino , which is supported by the Mozilla Foundation, Nashorn provides 2 to 10 times better performance, as it compiles code and transfers bytecode to the Java virtual machine directly in memory. Nashorn can compile JavaScript code and generate Java classes that are loaded with a special loader. It is also possible to call Java code directly from JavaScript.

Q. What is jjs?

jjs - This is a command line utility that allows you to execute JavaScript programs directly in the console.

Q. What class appeared in Java 8 for encoding / decoding data?

Base64- a thread-safe class that implements a data encoder and decoder using a base64 encoding scheme according to RFC 4648 and RFC 2045 .

Base64 contains 6 basic methods:

getEncoder() / getDecoder()- returns a base64 encoder / decoder conforming to the RFC 4648 standard ; getUrlEncoder()/ getUrlDecoder()- returns URL-safe base64 encoder / decoder conforming to RFC 4648 standard ; getMimeEncoder() / getMimeDecoder()- returns a MIME encoder / decoder conforming to RFC 2045 .

↥ back to top

Q. How to create a Base64 encoder and decoder?

// Encode 
String b64 =  Base64.getEncoder().encodeToString ( " input " . getBytes ( " utf-8 " )); // aW5wdXQ == 
// Decode 
new  String ( Base64.getDecoder().decode ( " aW5wdXQ == " ), " utf-8 " ); // input

Q. What are the functional interfaces Function<T,R>, DoubleFunction<R>, IntFunction<R> and LongFunction<R>?

Function<T, R>- the interface with which a function is implemented that receives an instance of the class T and returns an instance of the class at the output R.

Default methods can be used to build call chains ( compose, andThen).

Function < String , Integer > toInteger =  Integer :: valueOf;
Function < String , String > backToString = toInteger.andThen ( String :: valueOf);
backToString.apply("123");     // "123"

Q. What are the functional interfaces UnaryOperator<T>, DoubleUnaryOperator, IntUnaryOperatorand LongUnaryOperator?

UnaryOperator<T>(unary operator) takes an object of type as a parameter T, performs operations on them and returns the result of operations in the form of an object of type T:

UnaryOperator < Integer > operator = x - > x * x;
System.out.println(operator.apply ( 5 )); // 25

Q. What are the functional interfaces BinaryOperator<T>, DoubleBinaryOperator, IntBinaryOperatorand LongBinaryOperator?

BinaryOperator<T>(binary operator) - an interface through which a function is implemented that receives two instances of the class Tand returns an instance of the class at the output T.

BinaryOperator < Integer > operator = (a, b) -> a + b;
System.out.println(operator.apply ( 1 , 2 )); // 3
↥ back to top

Q. What are the functional interfaces Predicate<T>, DoublePredicate, IntPredicateand LongPredicate?

Predicate<T>(predicate) - the interface with which a function is implemented that receives an instance of the class as input Tand returns the type value at the output boolean.

The interface contains a variety of methods by default, allow to build complex conditions ( and, or, negate).

Predicate < String > predicate = (s) -> s.length () >  0 ;
predicate.test("foo"); // true 
predicate.negate().test("foo"); // false

Q. What are the functional interfaces Consumer<T>, DoubleConsumer, IntConsumerand LongConsumer?

Consumer<T>(consumer) - the interface through which a function is implemented that receives an instance of the class as an input T, performs some action with it, and returns nothing.

Consumer<String> hello = (name) ->  System.out.println( " Hello, "  + name);
hello.accept( " world " );

Q. What are the functional interfaces Supplier<T>, BooleanSupplier, DoubleSupplier, IntSupplierand LongSupplier?

Supplier<T>(provider) - the interface through which a function is implemented that takes nothing to the input, but returns the result of the class to the output T;

Supplier < LocalDateTime > now =  LocalDateTime::now;
now.get();

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

Q. Explain Differences between Collection API and Stream API?

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

Q. What is Optional in Java 8? What is the use of Optional?

Q. 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?

Q. What is differences between Functional Programming and Object-Oriented Programming?

↥ back to top