300+ [LATEST] Java 8 Interview Questions and Answers

Q1. What Is Nashorn In Java8?

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.

Q2. What Is The Purpose Of Longsupplier Functional Interface?

It represents a supplier of long-valued results.

Q3. How Will You Print Count Of Empty Strings In Java 8?

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

Liststrings = Arrays.asList(“abc”, “”, “bc”, “efg”, “abcd”,””, “jkl”);
//get count of empty string
int count = strings.stream().filter(string −> string.isEmpty()).count();

Q4. How Will You Call A Default Method Of An Interface In A Class?

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();                  
   }
}

Q5. How Will You Get The Highest Number Present In A List Using Java 8?

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

List 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());

Q6. What Is The Purpose Of Foreach Method Of Stream In Java 8?

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

Q7. What Is The Purpose Of Biconsumer Functional Interface?

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

Q8. What Is The Purpose Of Todoublebifunction Functional Interface?

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

Q9. What Is The Purpose Of Booleupplier Functional Interface?

It represents a supplier of Boolean-valued results.

Q10. What Is The Purpose Of Longunaryoperator Functional Interface?

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

Q11. What Is The Purpose Of Binaryoperator Functional Interface?

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

Q12. How Will You Add 1 Year To Current Date Using Local Datetime Api Of Java8?

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);

Q13. How Will You Create A Base64 Decoder That Decodes Using The Mime Type Base64 Encoding Scheme?

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

Q14. What Is The Purpose Of Function Functional Interface?

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

Q15. How Will You Add 1 Month To Current Date Using Local Datetime Api Of Java8?

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);

Q16. How Will You Print 10 Random Numbers In Java 8?

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

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

Q17. How Will You Print 10 Random Numbers In A Sorted Order In Java 8?

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);

Q18. How Will You Print 10 Random Numbers Using Foreach Of Java 8?

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);

Q19. How Will You Create A Base64 Encoder?

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

Q20. What Is Jjs In Java8?

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

Q21. What Is The Purpose Of Doublepredicate Functional Interface?

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

Q22. Explain The System.out::println Expression?

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

Q23. What Is The Purpose Of Longtointfunction Functional Interface?

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

Q24. What Is The Purpose Of Doubleconsumer Functional Interface?

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

Q25. What Is The Purpose Of Doubletointfunction Functional Interface?

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

Q26. What Is Local Datetime Api In Java8?

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

Q27. Can You Execute Javascript Code From Java 8 Code Base?

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

Q28. What Is The Purpose Of Tointfunction Functional Interface?

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

Q29. What Are Collectors In Java 8?

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.

Liststrings = Arrays.asList(“abc”, “”, “bc”, “efg”, “abcd”,””, “jkl”);
List 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);

Q30. How Will You Create A Base64 Decoder?

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

Q31. What Is The Purpose Of Unaryoperator Functional Interface?

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

Q32. What Are Default Methods?

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

Q33. What Is The Purpose Of Todoublefunction Functional Interface?

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

Q34. How Will You Create A Base64 Encoder That Encodes Using The Mime Type Base64 Encoding Scheme?

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

Q35. What Is The Purpose Of Intpredicate Functional Interface?

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

Q36. How Will You Create A Base64 Decoder That Decodes Using The Url And Filename Safe 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.

Q37. What Is The Purpose Of Intbinaryoperator Functional Interface?

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

Q38. How Will You Get The Lowest Number Present In A List Using Java 8?

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

List 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());

Q39. What Is The Purpose Of Longbinaryoperator Functional Interface?

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

Q40. How Will You Get Second Saturday Of Next Month Using Java8?

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);

Q41. What Is Optional In Java8?

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.

Q42. How Will You Sort A List Of String Using Java 8 Lambda Expression?

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

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

Q43. What Is The Purpose Of Consumer Functional Interface?

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

Q44. What Is The Purpose Of Tolongbifunction Functional Interface?

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

Q45. How Will You Print Unique Squares Of Numbers In Java 8?

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

List numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);

//get list of unique squares

List squaresList = numbers.stream().map( i -> i*i).distinct().collect(Collectors.toList());

Q46. What Is The Purpose Of Doubleunaryoperator Functional Interface?

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

Q47. What Is The Purpose Of Doublesupplier Functional Interface?

It represents a supplier of double-valued results.

Q48. How Will You Get The Average Of All Numbers Present In A List Using Java 8?

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

List 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());

Q49. How Will You Get The Current Date Using Local Datetime Api Of Java8?

Following code gets the current date using local datetime api −

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

Q50. What Are The Characteristics Of A Java 8 Lambda Expression?

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.