Java 8 Interview Questions


Advertisements


Dear readers, these Java 8 Interview Questions have been designed specially to get you acquainted with the nature of questions you may encounter during your interview for the subject of Java 8 Language. As per my experience good interviewers hardly plan to ask any particular question during your interview, normally questions start with some basic concept of the subject and later they continue based on further discussion and what you answer −

There are dozens of features added to Java 8, the most significant ones are mentioned below −

  • Lambda expression − Adds functional processing capability to Java.

  • Method references − Referencing functions by their names instead of invoking them directly. Using functions as parameter.

  • Default method − Interface to have default method implementation.

  • New tools − New compiler tools and utilities are added like 'jdeps' to figure out dependencies.

  • Stream API − New stream API to facilitate pipeline processing.

  • Date Time API − Improved date time API.

  • Optional − Emphasis on best practices to handle null values properly.

  • Nashorn, JavaScript Engine − A Java-based engine to execute JavaScript code.

Along with these new featuers, lots of feature enhancements are done under-the-hood, at both compiler and JVM level.

Following code sorts a list of string using Java 8 lambda expression:

//sort using java 8
private void sortUsingJava8(List<String> names){
  Collections.sort(names, (s1, s2) -> s1.compareTo(s2));
}

A lambda expression is characterized by the following syntax -

parameter −> expression body

Following are the important characteristics of a lambda expression −

  • Optional type declaration − No need to declare the type of a parameter. The compiler can inference the same from the value of the parameter.

  • Optional parenthesis around parameter − No need to declare a single parameter in parenthesis. For multiple parameters, parentheses are required.

  • Optional curly braces − No need to use curly braces in expression body if the body contains a single statement.

  • Optional return keyword − The compiler automatically returns the value if the body has a single expression to return the value. Curly braces are required to indicate that expression returns a value.

Lambda expressions are used primarily to define inline implementation of a functional interface, i.e., an interface with a single method only. In the above example, we've used various types of lambda expressions to define the operation method of MathOperation interface. Then we have defined the implementation of sayMessage of GreetingService.

Lambda expression eliminates the need of anonymous class and gives a very simple yet powerful functional programming capability to Java.

Using lambda expression, you can refer to final variable or effectively final variable (which is assigned only once). Lambda expression throws a compilation error, if a variable is assigned a value the second time.

Method references help to point to methods by their names. A method reference is described using :: (double colon) symbol. A method reference can be used to point the following types of methods −

  • Static methods

  • Instance methods

  • Constructors using new operator (TreeSet::new)

System.out::println method is a static method reference to println method of out object of System class.

Functional interfaces have a single functionality to exhibit. For example, a Comparable interface with a single method 'compareTo' is used for comparison purpose. Java 8 has defined a lot of functional interfaces to be used extensively in lambda expressions.

It represents an operation that accepts two input arguments, and returns no result.

It represents a function that accepts two arguments and produces a result.

It represents an operation upon two operands of the same type, producing a result of the same type as the operands.

It represents a predicate (Boolean-valued function) of two arguments.

It represents a supplier of Boolean-valued results.

It represents an operation that accepts a single input argument and returns no result.

It represents an operation upon two double-valued operands and producing a double-valued result.

It represents an operation that accepts a single double-valued argument and returns no result.

It represents a function that accepts a double-valued argument and produces a result.

It represents a predicate (Boolean-valued function) of one double-valued argument.

It represents a supplier of double-valued results.

It represents a function that accepts a double-valued argument and produces an int-valued result.

It represents a function that accepts a double-valued argument and produces a long-valued result.

It represents an operation on a single double-valued operand that produces a double-valued result.

It represents a function that accepts one argument and produces a result.

It represents an operation upon two int-valued operands and produces an int-valued result.

It represents an operation that accepts a single int-valued argument and returns no result.

It represents a function that accepts an int-valued argument and produces a result.

It represents a predicate (Boolean-valued function) of one int-valued argument.

It represents a supplier of int-valued results.

It represents a function that accepts an int-valued argument and produces a double-valued result.

It represents a function that accepts an int-valued argument and produces a long-valued result.

It represents an operation on a single int-valued operand that produces an int-valued result.

It represents an operation upon two long-valued operands and produces a long-valued result.

It represents an operation that accepts a single long-valued argument and returns no result.

It represents a function that accepts a long-valued argument and produces a result.

It represents a predicate (Boolean-valued function) of one long-valued argument.

It represents a supplier of long-valued results.

It represents a function that accepts a long-valued argument and produces a double-valued result.

It represents a function that accepts a long-valued argument and produces an int-valued result.

It represents an operation on a single long-valued operand that produces a long-valued result.

It represents an operation that accepts an object-valued and a double-valued argument, and returns no result.

It represents an operation that accepts an object-valued and an int-valued argument, and returns no result.

It represents an operation that accepts an object-valued and a long-valued argument, and returns no result.

It represents a predicate (Boolean-valued function) of one argument.

It represents a supplier of results.

It represents a function that accepts two arguments and produces a double-valued result.

It represents a function that produces a double-valued result.

It represents a function that accepts two arguments and produces an int-valued result.

It represents a function that produces an int-valued result.

It represents a function that accepts two arguments and produces a long-valued result.

It represents a function that produces a long-valued result.

It represents an operation on a single operand that produces a result of the same type as its operand.

With java 8, an interface can have default implementation of a function in interfaces.

An interface can also have static helper methods from Java 8 onwards.

public interface vehicle {
   default void print(){
      System.out.println("I am a vehicle!");
   }
 
   static void blowHorn(){
      System.out.println("Blowing horn!!!");
   }
}

Using super keyword along with interface name.

interface Vehicle {
   default void print(){
      System.out.println("I am a vehicle!");
   }
}
class Car implements Vehicle {
   public void print(){
      Vehicle.super.print();                  
   }
}

Using name of the interface.

interface Vehicle {
   static void blowHorn(){
      System.out.println("Blowing horn!!!");
   }
}
class Car implements Vehicle {
   public void print(){
      Vehicle.blowHorn();                  
   }
}

Stream represents a sequence of objects from a source, which supports aggregate operations.

Most of the stream operations return stream itself so that their result can be pipelined. These operations are called intermediate operations and their function is to take input, process them, and return output to the target. collect() method is a terminal operation which is normally present at the end of the pipelining operation to mark the end of the stream.

Stream operations do the iterations internally over the source elements provided, in contrast to Collections where explicit iteration is required.

Stream has provided a new method 'forEach' to iterate each element of the stream.

The following code segment shows how to print 10 random numbers using forEach.

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

The 'map' method is used to map each element to its corresponding result.

The following code segment prints unique squares of numbers using map.

List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
//get list of unique squares
List<Integer> squaresList = numbers.stream().map( i -> i*i).distinct().collect(Collectors.toList());

The 'filter' method is used to eliminate elements based on a criteria.

The following code segment prints a count of empty strings using filter.

List<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
//get count of empty string
int count = strings.stream().filter(string −> string.isEmpty()).count();

The 'limit' method is used to reduce the size of the stream.

The following code segment shows how to print 10 random numbers.

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

The 'sorted' method is used to sort the stream.

The following code segment shows how to print 10 random numbers in a sorted order.

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

parallelStream is the alternative of stream for parallel processing. Take a look at the following code segment that prints a count of empty strings using parallelStream.

List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
//get count of empty string
int count = strings.parallelStream().filter(string −> string.isEmpty()).count();
//It is very easy to switch between sequential and parallel streams.

Collectors are used to combine the result of processing on the elements of a stream. Collectors can be used to return a list or a string.

List<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
System.out.println("Filtered List: " + filtered);
String mergedString = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining(", "));
System.out.println("Merged String: " + mergedString);

With Java 8, statistics collectors are introduced to calculate all statistics when stream processing is being done.

Following code will print the highest number present in a list.

List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
IntSummaryStatistics stats = integers.stream().mapToInt((x) −> x).summaryStatistics();
System.out.println("Highest number in List : " + stats.getMax());

Following code will print the highest number present in a list.

List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
IntSummaryStatistics stats = integers.stream().mapToInt((x) −> x).summaryStatistics();
System.out.println("Lowest number in List : " + stats.getMin());

Following code will print the sum of all numbers present in a list.

List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
IntSummaryStatistics stats = integers.stream().mapToInt((x) −> x).summaryStatistics();
System.out.println("Sum of all numbers : " + stats.getSum());

Following code will print the average of all numbers present in a list.

List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
IntSummaryStatistics stats = integers.stream().mapToInt((x) −> x).summaryStatistics();
System.out.println("Average of all numbers : " + stats.getAverage());

Optional is a container object which is used to contain not-null objects. Optional object is used to represent null with absent value. This class has various utility methods to facilitate code to handle values as 'available' or 'not available' instead of checking null values. It is introduced in Java 8 and is similar to what Optional is in Guava.

With Java 8, Nashorn, a much improved javascript engine is introduced, to replace the existing Rhino. Nashorn provides 2 to 10 times better performance, as it directly compiles the code in memory and passes the bytecode to JVM. Nashorn uses invokedynamics feature, introduced in Java 7 to improve performance.

For Nashorn engine, JAVA 8 introduces a new command line tool, jjs, to execute javascript codes at console.

Yes! Using ScriptEngineManager, JavaScript code can be called and interpreted in Java.

Local − Simplified date-time API with no complexity of timezone handling.

Zoned − Specialized date-time API to deal with various timezones.

java.time.temporal.ChronoUnit enum is added in Java 8 to replace the integer values used in old API to represent day, month, etc.

Following code gets the current date using local datetime api −

//Get the current date
LocalDate today = LocalDate.now();
System.out.println("Current date: " + today);

Following code adds 1 week to current date using local datetime api −

//add 1 week to the current date
LocalDate today = LocalDate.now();
LocalDate nextWeek = today.plus(1, ChronoUnit.WEEKS);
System.out.println("Next week: " + nextWeek);

Following code adds 1 month to current date using local datetime api:

//add 1 month to the current date
LocalDate today = LocalDate.now();
LocalDate nextMonth = today.plus(1, ChronoUnit.MONTHS);
System.out.println("Next month: " + nextMonth);

Following code adds 1 year to current date using local datetime api −

//add 1 year to the current date
LocalDate today = LocalDate.now();
LocalDate nextYear = today.plus(1, ChronoUnit.YEARS);
System.out.println("Next year: " + nextYear);

Following code adds 10 years to current date using local datetime api −

//add 10 years to the current date
LocalDate today = LocalDate.now();
LocalDate nextDecade = today.plus(1, ChronoUnit.DECADES);
System.out.println("Date after ten year: " + nextDecade);

Following code gets next tuesday using java8 −

//get the next tuesday
LocalDate today = LocalDate.now();
LocalDate nextTuesday = today.with(TemporalAdjusters.next(DayOfWeek.TUESDAY));
System.out.println("Next Tuesday on : " + nextTuesday);

Following code gets second saturday of next month using java8 −

//get the second saturday of next month
LocalDate firstInYear = LocalDate.of(date1.getYear(),date1.getMonth(), 1);
LocalDate secondSaturday = firstInYear.with(TemporalAdjusters.nextOrSame(DayOfWeek.SATURDAY)).with(TemporalAdjusters.next(DayOfWeek.SATURDAY));
System.out.println("Second Saturday on : " + secondSaturday);

Following code gets the instant of current date in terms of milliseconds −

//Get the instant of current date in terms of milliseconds
Instant now = currentDate.toInstant();

Following code gets the instant of local date time using time in of milliseconds −

Instant now = currentDate.toInstant();
ZoneId currentZone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(now, currentZone);
System.out.println("Local date: " + localDateTime);

Following code gets the instant of zoned date time using time in of milliseconds −

Instant now = currentDate.toInstant();
ZoneId currentZone = ZoneId.systemDefault();
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(now, currentZone);
System.out.println("Zoned date: " + zonedDateTime);

static class Base64.Decoder − This class implements a decoder for decoding byte data using the Base64 encoding scheme as specified in RFC 4648 and RFC 2045.

static class Base64.Encoder − This class implements an encoder for encoding byte data using the Base64 encoding scheme as specified in RFC 4648 and RFC 2045.

getDecoder() method of Base64 class returns a Base64.Decoder that decodes using the Basic type base64 encoding scheme.

getEncoder() method of Base64 class returns a Base64.Encoder that encodes using the Basic type base64 encoding scheme.

getMimeDecoder() method of Base64 class returns a Base64.Decoder that decodes using the MIME type base64 decoding scheme.

getMimeEncoder() method of Base64 class returns a Base64.Encoder that encodes using the MIME type base64 encoding scheme.

getUrlDecoder() method of Base64 class returns a Base64.Decoder that decodes using the URL and Filename safe type base64 encoding scheme.

getUrlEncoder() method of Base64 class returns a Base64.Encoder that encodes using the URL and Filename safe type base64 encoding scheme.

What is Next?

Further you can go through your past assignments you have done with the subject and make sure you are able to speak confidently on them. If you are fresher then interviewer does not expect you will answer very complex questions, rather you have to make your basics concepts very strong.

Second it really doesn't matter much if you could not answer few questions but it matters that whatever you answered, you must have answered with confidence. So just feel confident during your interview. We at tutorialspoint wish you best luck to have a good interviewer and all the very best for your future endeavor. Cheers :-)



Advertisements