Java 8 Interview Questions - Tutorialspoint
Java 8 Interview Questions - Tutorialspoint
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.
How will you sort a list of string using Java 8 lambda expression?
https://www.tutorialspoint.com/java8/java8_interview_questions.htm 1/16
2/24/2021 Java 8 Interview Questions - Tutorialspoint
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
https://www.tutorialspoint.com/java8/java8_interview_questions.htm 2/16
2/24/2021 Java 8 Interview Questions - Tutorialspoint
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 an operation upon two operands of the same type, producing a result of the same
type as the operands.
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.
https://www.tutorialspoint.com/java8/java8_interview_questions.htm 3/16
2/24/2021 Java 8 Interview Questions - Tutorialspoint
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 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.
https://www.tutorialspoint.com/java8/java8_interview_questions.htm 4/16
2/24/2021 Java 8 Interview Questions - Tutorialspoint
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 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 double-valued result.
https://www.tutorialspoint.com/java8/java8_interview_questions.htm 5/16
2/24/2021 Java 8 Interview Questions - Tutorialspoint
It represents a function that accepts a long-valued argument and produces an int-valued 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 function that accepts two arguments and produces a double-valued result.
It represents a function that accepts two arguments and produces an int-valued result.
https://www.tutorialspoint.com/java8/java8_interview_questions.htm 6/16
2/24/2021 Java 8 Interview Questions - Tutorialspoint
It represents a function that accepts two arguments and produces a long-valued result.
It represents an operation on a single operand that produces a result of the same type as its
operand.
An interface can also have static helper methods from Java 8 onwards.
interface Vehicle {
default void print() {
System.out.println("I am a vehicle!");
}
https://www.tutorialspoint.com/java8/java8_interview_questions.htm 7/16
2/24/2021 Java 8 Interview Questions - Tutorialspoint
}
class Car implements Vehicle {
public void print() {
Vehicle.super.print();
}
}
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.
https://www.tutorialspoint.com/java8/java8_interview_questions.htm 8/16
2/24/2021 Java 8 Interview Questions - Tutorialspoint
The following code segment shows how to print 10 random numbers using forEach.
The 'map' method is used to map each element to its corresponding result.
The following code segment prints unique squares of numbers using map.
The following code segment prints a count of empty strings using filter.
The following code segment shows how to print 10 random numbers in a sorted order.
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.
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.
With Java 8, statistics collectors are introduced to calculate all statistics when stream processing is
being done.
How will you get the highest number present in a list using Java 8?
How will you get the lowest number present in a list using Java 8?
How will you get the sum of all numbers present in a list using Java 8?
Following code will print the sum of all numbers present in a list.
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.
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.
java.time.temporal.ChronoUnit enum is added in Java 8 to replace the integer values used in old
API to represent day, month, etc.
How will you get the current date using local datetime api of java8?
Following code gets the current date using local datetime api −
How will you add 1 week to current date using local datetime api of java8?
Following code adds 1 week to current date using local datetime api −
https://www.tutorialspoint.com/java8/java8_interview_questions.htm 12/16
2/24/2021 Java 8 Interview Questions - Tutorialspoint
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:
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 −
How will you add 10 years to current date using local datetime api of java8?
Following code adds 10 years to current date using local datetime api −
How will you get second saturday of next month using java8?
https://www.tutorialspoint.com/java8/java8_interview_questions.htm 13/16
2/24/2021 Java 8 Interview Questions - Tutorialspoint
How will you get the instant of current date in terms of milliseconds using
java8?
How will you get the instant of local date time using time in of milliseconds
using java8?
Following code gets the instant of local date time using time in of milliseconds −
How will you get the instant of zoned date time using time in of milliseconds
using java8?
Following code gets the instant of zoned date time using time in of milliseconds −
Which class implements a decoder for decoding byte data using the Base64
encoding scheme in Java8?
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.
https://www.tutorialspoint.com/java8/java8_interview_questions.htm 14/16
2/24/2021 Java 8 Interview Questions - Tutorialspoint
Which class implements an encoder for encoding byte data using the
Base64 encoding scheme in Java8?
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.
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.
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.
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.
How will you create a Base64 encoder that encodes 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?
https://www.tutorialspoint.com/java8/java8_interview_questions.htm 15/16
2/24/2021 Java 8 Interview Questions - Tutorialspoint
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 :-)
https://www.tutorialspoint.com/java8/java8_interview_questions.htm 16/16