100 - Java 8 Interview Questions & Answers
100 - Java 8 Interview Questions & Answers
Answer :
There are dozens of features added to Java 8, the most significant ones are
mentioned below -
Question 2. How Will You Sort A List Of String Using Java 8 Lambda Expression?
Answer :
Answer :
Answer :
Lambda expression eliminates the need of anonymous class and gives a very simple
yet powerful functional programming capability to Java.
Answer :
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.
Answer :
Static methods
Instance methods
Constructors using new operator (TreeSet::new)
Answer :
Answer :
Answer :
It represents an operation that accepts two input arguments, and returns no result.
Answer :
Answer :
It represents an operation upon two operands of the same type, producing a result
of the same type as the operands.
Answer :
Answer :
Answer :
Answer :
Answer :
Answer :
Answer :
Answer :
It represents a supplier of double-valued results.
Answer :
Answer :
Answer :
Answer :
Answer :
Answer :
Answer :
Answer :
Answer :
It represents a supplier of int-valued results.
Answer :
Answer :
Answer :
Answer :
Answer :
Answer :
Answer :
Answer :
Answer :
It represents a function that accepts a long-valued argument and produces a double-
valued result.
Answer :
Answer :
Answer :
Answer :
Answer :
Answer :
Answer :
Answer :
Answer :
Answer :
Answer :
Answer :
Answer :
Answer :
Answer :
An interface can also have static helper methods from Java 8 onwards.
Answer :
interface Vehicle {
default void print(){
System.out.println("I am a vehicle!");
}
}
class Car implements Vehicle {
public void print(){
Vehicle.super.print();
}
}
Question 55. How Will You Call A Static Method Of An Interface In A Class?
Answer :
interface Vehicle {
static void blowHorn(){
System.out.println("Blowing horn!!!");
}
}
class Car implements Vehicle {
public void print(){
Vehicle.blowHorn();
}
}
Answer :
Answer :
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.
Question 58. What Is The Difference Between Collections And Stream In Java8 ?
Answer :
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.
Question 60. How Will You Print 10 Random Numbers Using Foreach Of Java 8?
Answer :
The following code segment shows how to print 10 random numbers using forEach.
Answer :
The 'map' method is used to map each element to its corresponding result.
Question 62. How Will You Print Unique Squares Of Numbers In Java 8?
Answer :
The following code segment prints unique squares of numbers using map.
Answer :
Question 64. How Will You Print Count Of Empty Strings In Java 8?
Answer :
The following code segment prints a count of empty strings using filter.
Answer :
Answer :
The following code segment shows how to print 10 random numbers.
Answer :
Question 68. How Will You Print 10 Random Numbers In A Sorted Order In Java 8?
Answer :
The following code segment shows how to print 10 random numbers in a sorted order.
Answer :
Answer :
Answer :
With Java 8, statistics collectors are introduced to calculate all statistics when
stream processing is being done.
Question 72. How Will You Get The Highest Number Present In A List Using Java 8?
Answer :
Following code will print the highest number present in a list.
Question 73. How Will You Get The Lowest Number Present In A List Using Java 8?
Answer :
Question 74. How Will You Get The Sum Of All Numbers Present In A List Using Java
8?
Answer :
Following code will print the sum of all numbers present in a list.
Question 75. How Will You Get The Average Of All Numbers Present In A List Using
Java 8?
Answer :
Following code will print the average of all numbers present in a list.
Answer :
Answer :
Answer :
For Nashorn engine, JAVA 8 introduces a new command line tool, jjs, to execute
javascript codes at console.
Question 79. Can You Execute Javascript Code From Java 8 Code Base?
Answer :
Answer :
Answer :
Answer :
Question 83. How Will You Get The Current Date Using Local Datetime Api Of Java8?
Answer :
Following code gets the current date using local datetime api -
Question 84. How Will You Add 1 Week To Current Date Using Local Datetime Api Of
Java8?
Answer :
Following code adds 1 week to current date using local datetime api -
Question 85. How Will You Add 1 Month To Current Date Using Local Datetime Api Of
Java8?
Answer :
Following code adds 1 month to current date using local datetime api:
Question 86. How Will You Add 1 Year To Current Date Using Local Datetime Api Of
Java8?
Answer :
Following code adds 1 year to current date using local datetime api -
Question 87. How Will You Add 10 Years To Current Date Using Local Datetime Api Of
Java8?
Answer :
Following code adds 10 years to current date using local datetime api -
Question 88. How Will You Get Next Tuesday Using Java8?
Answer :
Question 89. How Will You Get Second Saturday Of Next Month Using Java8?
Answer :
LocalDate secondSaturday =
firstInYear.with(TemporalAdjusters.nextOrSame(DayOfWeek.SATURDAY)).with(TemporalAdj
usters.next(DayOfWeek.SATURDAY));
System.out.println("Second Saturday on : " + secondSaturday);
Question 90. How Will You Get The Instant Of Current Date In Terms Of Milliseconds
Using Java8?
Answer :
Question 91. How Will You Get The Instant Of Local Date Time Using Time In Of
Milliseconds Using Java8?
Answer :
Following code gets the instant of local date time using time in of milliseconds -
Question 92. How Will You Get The Instant Of Zoned Date Time Using Time In Of
Milliseconds Using Java8?
Answer :
Following code gets the instant of zoned date time using time in of milliseconds -
Question 93. Which Class Implements A Decoder For Decoding Byte Data Using The
Base64 Encoding Scheme In Java8?
Answer :
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.
Question 94. Which Class Implements An Encoder For Encoding Byte Data Using The
Base64 Encoding Scheme In Java8?
Answer :
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.
Answer :
getDecoder() method of Base64 class returns a Base64.Decoder that decodes using the
Basic type base64 encoding scheme.
Answer :
getEncoder() method of Base64 class returns a Base64.Encoder that encodes using the
Basic type base64 encoding scheme.
Question 97. How Will You Create A Base64 Decoder That Decodes Using The Mime Type
Base64 Encoding Scheme?
Answer :
Question 98. How Will You Create A Base64 Encoder That Encodes Using The Mime Type
Base64 Encoding Scheme?
Answer :
Question 99. How Will You Create A Base64 Decoder That Decodes Using The Url And
Filename Safe Type Base64 Encoding Scheme?
Answer :
Question 100. How Will You Create A Base64 Encoder That Encodes Using The Url And
Filename Safe Type Base64 Encoding Scheme?
Answer :