Top 50 Java 8 Interview Questions
Q1. What Is Nashorn In Java8?
With Java 8, Nashorn, a far improved javascript engine is brought, to update the existing Rhino. Nashorn gives 2 to 10 instances better overall performance, because it immediately compiles the code in memory and passes the bytecode to JVM. Nashorn makes use of invokedynamics feature, delivered in Java 7 to improve performance.
Q2. What Is The Purpose Of Longsupplier Functional Interface?
It represents a supplier of lengthy-valued outcomes.
Q3. How Will You Print Count Of Empty Strings In Java eight?
The following code section prints a rely of empty strings using filter out.
List<String>strings = Arrays.AsList("abc", "", "bc", "efg", "abcd","", "jkl");
//get rely of empty string
int count number = strings.Circulation().Clear out(string −> string.IsEmpty()).Count number();
Q4. How Will You Call A Default Method Of An Interface In A Class?
Using tremendous keyword together with interface name.
Interface Vehicle
default void print()
System.Out.Println("I am a vehicle!");
magnificence Car implements Vehicle
public void print()
Vehicle.Great.Print();
Q5. How Will You Get The Highest Number Present In A List Using Java 8?
Following code will print the very best wide variety found in a listing.
List numbers = Arrays.AsList(three, 2, 2, three, 7, three, 5);
IntSummaryStatistics stats = integers.Circulation().MapToInt((x) −> x).SummaryStatistics();
System.Out.Println("Highest variety in List : " + stats.GetMax());
Q6. What Is The Purpose Of Foreach Method Of Stream In Java 8?
Stream has supplied a brand new approach 'forEach' to iterate every element of the circulate.
Q7. What Is The Purpose Of Biconsumer<t,u> Functional Interface?
It represents an operation that accepts enter arguments, and returns no end result.
Q8. What Is The Purpose Of Todoublebifunction<t,u> Functional Interface?
It represents a characteristic that accepts arguments and produces a double-valued result.
Q9. What Is The Purpose Of Booleupplier Functional Interface?
It represents a dealer of Boolean-valued outcomes.
Q10. What Is The Purpose Of Longunaryoperator Functional Interface?
It represents an operation on a single lengthy-valued operand that produces a long-valued result.
Q11. What Is The Purpose Of Binaryoperator<t> Functional Interface?
It represents an operation upon operands of the identical type, generating a end result of the identical kind because the operands.
Q12. How Will You Add 1 Year To Current Date Using Local Datetime Api Of Java8?
Following code provides 1 12 months to contemporary date the use of neighborhood datetime api −
//add 1 year to the present day date
LocalDate today = LocalDate.Now();
LocalDate nextYear = nowadays.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() approach of Base64 class returns a Base64.Decoder that decodes the use of the MIME type base64 deciphering scheme.
Q14. What Is The Purpose Of Function<t,r> 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 modern-day date the usage of nearby datetime api:
//add 1 month to the modern date
LocalDate these days = 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 phase indicates 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 phase shows the way to print 10 random numbers in a looked after order.
Random random = new Random();
random.Ints().Limit(10).Taken care of().ForEach(System.Out::println);
Q18. How Will You Print 10 Random Numbers Using Foreach Of Java eight?
The following code phase shows how to print 10 random numbers using forEach.
Random random = new Random();
random.Ints().Restriction(10).ForEach(System.Out::println);
Q19. How Will You Create A Base64 Encoder?
GetEncoder() technique of Base64 elegance returns a Base64.Encoder that encodes the usage of the Basic type base64 encoding scheme.
Q20. What Is Jjs In Java8?
For Nashorn engine, JAVA 8 introduces a brand 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 1 double-valued argument.
Q22. Explain The System.Out::println Expression?
System.Out::println method is a static approach connection with println method of out item of System elegance.
Q23. What Is The Purpose Of Longtointfunction Functional Interface?
It represents a function that accepts a protracted-valued argument and produces an int-valued end result.
Q24. What Is The Purpose Of Doubleconsumer Functional Interface?
It represents an operation that accepts a unmarried double-valued argument and returns no result.
Q25. What Is The Purpose Of Doubletointfunction Functional Interface?
It represents a feature that accepts a double-valued argument and produces an int-valued end result.
Q26. What Is Local Datetime Api In Java8?
Local − Simplified date-time API without a complexity of timezone coping with.
Q27. Can You Execute Javascript Code From Java 8 Code Base?
Yes! Using ScriptEngineManager, JavaScript code may be referred to as and interpreted in Java.
Q28. What Is The Purpose Of Tointfunction<t> Functional Interface?
It represents a characteristic that produces an int-valued end result.
Q29. What Are Collectors In Java eight?
Collectors are used to mix the end result of processing on the factors of a stream. Collectors may be used to go back a listing or a string.
List<String>strings = Arrays.AsList("abc", "", "bc", "efg", "abcd","", "jkl");
List<String> filtered = strings.Circulation().Filter(string -> !String.IsEmpty()).Gather(Collectors.ToList());
System.Out.Println("Filtered List: " + filtered);
String mergedString = strings.Movement().Filter out(string -> !String.IsEmpty()).Gather(Collectors.Joining(", "));
System.Out.Println("Merged String: " + mergedString);
Q30. How Will You Create A Base64 Decoder?
GetDecoder() technique of Base64 magnificence returns a Base64.Decoder that decodes the usage of the Basic type base64 encoding scheme.
Q31. What Is The Purpose Of Unaryoperator<t> Functional Interface?
It represents an operation on a unmarried operand that produces a result of the same kind as its operand.
Q32. What Are Default Methods?
With java 8, an interface will have default implementation of a function in interfaces.
Q33. What Is The Purpose Of Todoublefunction<t> 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() technique of Base64 elegance returns a Base64.Encoder that encodes the usage of the MIME kind base64 encoding scheme.
Q35. What Is The Purpose Of Intpredicate Functional Interface?
It represents a predicate (Boolean-valued feature) of 1 int-valued argument.
Q36. How Will You Create A Base64 Decoder That Decodes Using The Url And Filename Safe Type Base64 Encoding Scheme?
GetUrlDecoder() technique of Base64 magnificence returns a Base64.Decoder that decodes the usage of the URL and Filename safe type base64 encoding scheme.
Q37. What Is The Purpose Of Intbinaryoperator Functional Interface?
It represents an operation upon int-valued operands and produces an int-valued result.
Q38. How Will You Get The Lowest Number Present In A List Using Java eight?
Following code will print the highest range found in a list.
List numbers = Arrays.AsList(3, 2, 2, 3, 7, three, 5);
IntSummaryStatistics stats = integers.Circulation().MapToInt((x) −> x).SummaryStatistics();
System.Out.Println("Lowest variety 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 protracted-valued result.
Q40. How Will You Get Second Saturday Of Next Month Using Java8?
Following code receives 2d saturday of next month the usage of 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.Subsequent(DayOfWeek.SATURDAY));
System.Out.Println("Second Saturday on : " + secondSaturday);
Q41. What Is Optional In Java8?
Optional is a container item that's used to contain now not-null items. Optional object is used to symbolize null with absent price. This class has numerous utility techniques to facilitate code to handle values as 'to be had' or 'no longer to be had' as opposed to checking null values. It is delivered in Java eight 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 the use of Java eight lambda expression:
//type using java 8
private void sortUsingJava8(List<String> names)
Collections.Type(names, (s1, s2) -> s1.CompareTo(s2));
Q43. What Is The Purpose Of Consumer<t> Functional Interface?
It represents an operation that accepts a single input argument and returns no end result.
Q44. What Is The Purpose Of Tolongbifunction<t,u> Functional Interface?
It represents a function that accepts arguments and produces an extended-valued end result.
Q45. How Will You Print Unique Squares Of Numbers In Java eight?
The following code phase prints unique squares of numbers the usage of map.
List<Integer> numbers = Arrays.AsList(3, 2, 2, 3, 7, three, five);
//get listing of unique squares
List<Integer> squaresList = numbers.Circulate().Map( i -> i*i).Wonderful().Collect(Collectors.ToList());
Q46. What Is The Purpose Of Doubleunaryoperator Functional Interface?
It represents an operation on a unmarried double-valued operand that produces a double-valued end 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 common of all numbers found in a listing.
List numbers = Arrays.AsList(three, 2, 2, three, 7, 3, five);
IntSummaryStatistics stats = integers.Circulation().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 present day date the usage of nearby datetime api −
//Get the current date
LocalDate nowadays = LocalDate.Now();
System.Out.Println("Current date: " + nowadays);
Q50. What Are The Characteristics Of A Java eight Lambda Expression?
A lambda expression is characterised with the aid of the subsequent syntax - parameter −> expression frame
Following are the crucial traits of a lambda expression −
Optional kind statement − No need to claim the kind of a parameter. The compiler can inference the same from the fee of the parameter.
Optional parenthesis round parameter − No want to declare a single parameter in parenthesis. For a couple of parameters, parentheses are required.
Optional curly braces − No want to apply curly braces in expression frame if the body incorporates a unmarried declaration.
Optional go back key-word − The compiler robotically returns the fee if the body has a single expression to return the fee. Curly braces are required to indicate that expression returns a value.

