SlideShare a Scribd company logo
10
Most read
11
Most read
16
Most read
Top 50 Java 8 Interview Questions and Answers
Lambda expressions
A way to characterize and pass around blocks of code as if they were objects, which permits for more concise,
functional-style code.
Functional interfacing
Java 8 is a known language for every Java developer. The latest release contains new Java Features,
enhancements, and bug fixes to improve efficiency in developing and running Java programs. So, acquiring
knowledge about Java 8 is now a need for every Java developer. Learning Java 8 can significantly contribute to
your success in the Java developer journey.
Hence In this Java Tutorial, we will explore the top 50 Java 8 Interview Questions and Answers. These questions
cover both standard and advanced commonly encountered topics during interviews. By thoroughly reading and
understanding these questions, you’ll be well-prepared for your Java interview. Let’s start with Java 8 Interview
Questions & Answers for Beginners.
Java 8 Interview Questions & Answers for Beginners
1. What are the features of Java 8?
Interfacing has precisely one unique method, which allows for behavior parameterization and the capacity to
pass behavior as a method argument.
Streams
A modern API for processing collections of information that allows for operations such as filtering, mapping, and
decreasing to be performed in a more functional and clear way.
Date and time API
A modern API for working with date and time, which replaces the legacy java.util.Date and java.util.Calendar
classes.
Concurrent Accumulators
A set of classes planned for utilization with parallel streams, which permit for the effective amassing
Lambda expressions can be utilized to define functional interfacing, which are interfacing that have a single
abstract method. The java.util.function package in Java 8 incorporates a few functional interfacing such as
Customer, Work, Predicate, and Provider. Lambda expressions can also be passed to methods or utilized as
arguments for functional interfacing. For case, the forEach method of the java.util.stream.Stream class takes
a Consumer functional interface as an argument, allowing us to pass in a lambda expression to perform a
particular action on each component
within the stream.
Lambda expressions can also be used with other features of Java 8 such as streams and the new date and
time API to perform operations such as filtering, mapping, and decreasing collections of information, in a
more functional and clear way. It's worth noticing that, in spite of the fact that lambda expressions can offer
assistance to make your code more concise and readable, they can also make it more difficult to get it if they
are not used accurately. It's critical to utilize them in a way that produces the code simple to understand.
Lambda expressions are nothing but a new feature that allows programmers to write more concise, functional-
style code.
It is composed of three parts:
A list of parameters (or none) is enclosed in parentheses or brackets.
The “arrow” token ->
The body of the lambda expression can be a single expression or a block of code.
2. In Java 8, What are lambda expressions and their use?
Uses of lambda expressions
Syntax of simple lambda expression
(int x, int y) -> {return x + y; }
5. What are Java 8 significant advantages?
3. What are different kinds of Method References?
4. In which programming paradigm did Java 8 fall?
It is Compact, readable, and reusable code.
It has Less boilerplate code.
It can do Parallel operations and execution.
It can be ported across operating systems.
It has High stability.
MetaSpace:
static methods in interfaces
It has a Stable environment.
Static methods, which contain method implementation are owned by the interface and are invoked using the
name of the interface, it is suitable for defining the utility methods and cannot be overridden.
Java 8 stores the MetaData of classes in local memory called 'MetaSpace'. It is not a contiguous Heap Memory
and hence can be developed dynamically which makes a difference to overcome the size limitations. This moves
forward the garbage collection, auto-tuning, and de-allocation of metadata.
PremGen:
PreGem stands forPermanent-Generation, MetaData data of classes was stored in PremGen memory type before
Java 8. PremGen is settled in size and cannot be dynamically resized. It was a contiguous Java Heap Memory.
7. What are static methods in Interfaces?
6. What is MetaSpace in Java 8? How does it differ from PermGen?
Example:
interface DemoInterface {
// static method
static void hello()
{
System.out.println("Hello, Welcome to Scholarhat");
Output
8. What are some standard Java pre-defined functional interfaces?
Try it Yourself >>
Runnable: used to execute the instances of a class over another thread with no arguments and no return
value.
Callable: used to execute the instances of a class over another thread with no arguments and it either returns
a value or throws an exception.
Comparator: used to sort different objects in a user-defined order
Comparable: used to sort objects in the natural sort order
}
}
}
}
Hello, Welcome to Scholarhat
Hello, Override Method here
// Implementing interface method
@Override
public void overrideMethod(String str)
{
System.out.println(str);
}
// Public and abstract method of Interface
void overrideMethod(String str);
// Implementation Class
public class InterfaceEx implements DemoInterface {
// Calling the static method of interface
DemoInterface.hello();
public static void main(String[] args)
{
InterfaceEx interfaceEx= new InterfaceEx();
// Calling the abstract method of interface
interfaceEx.overrideMethod("Hello, Override Method here");
14. What Is JJS?
In Java 8, jjs is the new executable or command line tool we use to execute Javascript code at the console.
13. What Is Nashorn in Java8?
15. What Is Stream Pipelining in Java 8?
11. What Is the Meaning of String::Valueof Expression?
String::Valueof Expression( ) is a static method reference to the valueOf method of the String class.
10. How does a lambda expression relate to a functional interface?
12. Explain Some of the Functional Interfaces in the Standard Library
9. What are the various categories of pre-defined function interfaces?
1. Function: To transform arguments in returnable value.
2. Predicate: To perform a test and return a Boolean value.
3. Consumer: Accept arguments but do not return any values.
4. Supplier: Do not accept any arguments but return a value.
5. Operator: Perform a reduction-type operation that accepts the same input types.
Function – takes one argument and returns a result Consumer – takes one argument and returns no result
(represents a side effect) Supplier – takes no arguments and returns a result Predicate – takes one argument
and returns a boolean BiFunction – takes two arguments and returns a result BinaryOperator – is similar to a
BiFunction, taking two arguments and returning a result. The two arguments and the result are all of the same
type.
Stream pipelining is the concept of chaining operations together. We do this by splitting the operations that
can happen on a stream into two categories: intermediate operations and terminal operations.
Each intermediate operation returns an instance of Stream itself when it runs. Therefore, we can set up an
arbitrary number of intermediate operations to process data, forming a processing pipeline.
It is a type of function without a name. It may or may not have its results and parameters. It is also known as
an anonymous function as it does not have type information by itself. It is executed on demand. It is
advantageous in iterating, filtering, and extracting data from a collection. As lambda expressions are similar
to anonymous functions, they can only be applied to the single abstract method of Functional Interface. It will
infer the return type, type, and several arguments from the signature of the abstract method of the functional
interface.
Nashorn is the new Javascript processing engine for the Java platform that shipped with Java 8. Until JDK 7,
the Java platform used Mozilla Rhino for the same purpose, as a Javascript processing engine.
Nashorn provides better compliance with the ECMA-normalized JavaScript specification and better runtime
performance than its predecessor.
For executing lazy operations
To perform database operations
Use for internal iterations.
For writing functional-style programming
You can use it for using pipeline operations.
It helps in creating applications much faster and in an easier way.
It provides a very stable ambient for the developers.
Concise, reusable, and easy-to-comprehend codes.
Improved and effective support.
Easy to port across various operating systems.
Minimum boilerplate codes.
Functional Interfaces are interfaces with only one abstract method.
Due to this, it is also known as the Single Abstract Method (SAM) interface.
There must then be a terminal operation that returns a final value and terminates the pipeline.
A predicate is a functional interface that typically receives arguments and retrieves a Boolean value. You can
use it to apply the filter for a collection of objects.
On the other hand, the consumer is referred to as an in-build functional interface found in Java.util.function
package. You can use it to consume any object, and it takes the input value and gives out nothing.
The peek() method helps support debugging, where one wants to notice the elements as they tend to flow from a
specific point in a pipeline. It is a representation of our observation of how each element passes.
A collection is an in-memory database that records all the values according to the current data structure. So,
before you add it to the collection, it’s important to compute each of them. Whereas a stream is a visually
fixed data structure where we can compute the elements according to our needs.
16. Specify the advantages of using Java 8
21. What are functional or SAM interfaces?
19. Why is the peek () method used in Java 8?
18. What are predicate and consumer in Java 8?
17. What is a collection, and how is it different from a stream?
20. Which situation is most suitable for using stream API in Java 8?
Syntax:
Java 8 Interview Questions and Answers for Intermediate
public Object peek()
It is also known as a functional interface because it wraps a function as an interface.
These interfaces can have any number of default, static, and overridden methods.
If declaring Functional Interfaces @FunctionalInterface annotation is optional to use.
If this annotation is used for interfaces with more than one abstract method, it will generate a compiler error.
Type Inference assists the compiler in identifying or recognizing the types of arguments by just having an
overview of the corresponding declaration and method invocation.
As shown in the above code, It will also extend the abstract method of the Parent Interface. Hence it will have
more than one abstract method And will give a compiler error.
No, we can not extend or inherit a functional interface into another interface also it can not be be achievable
through abstract methods as it will void the rule of one abstract method per functional interface.
A method in the interface that has a predefined body is known as the default method. It uses the keyword
default. default methods were introduced in Java 8 to have 'Backward Compatibility in case JDK modifies any
interfaces. In case a new abstract method is added to the interface, all classes implementing the interface
will break and will have to implement the new method. With default methods, there will not be any impact on
the interface implementing classes.
Default methods can be overridden if needed in the implementation. Also, it does not qualify as synchronized
or final.
23. What is the function of Type Inference?
24. What is the default method, and why is it required?
22. Can we extend the functional interface to another interface?
Example:
@FunctionalInterface // Annotation is optional
public interface Foo() {
// Default Method - Optional can be 0 or more
public default String Scholarhat() {
return "Welcome to Scholarhat";
interface Student{
public int studenttMethod();
}
@FunctionalInterface // This cannot be FunctionalInterface
interface Child extends Parent {
public int childMethod();
}
27. In Java 8, what is Method Reference?
Method reference is a compact way of referring to a method of functional interface. It is used to refer to a method
without invoking it. :: (double colon) is used for describing the method reference. The syntax is
class::methodName
25. What is the basic structure/syntax of a lambda expression?
26. What are the types and common ways to use lambda expressions?
A lambda expression does not have any specific type by itself. A lambda expression receives type once it is
assigned to a functional interface. That same lambda expression can be assigned to different functional
interface types and can have a different type.
A body can have expressions or statements. {} curly braces are only required when there is more than one line. In
one statement, the return type is the same as the return type of the statement. In other cases, the return type is
either inferred by the return keyword or void if nothing is returned.
A lambda expression can be divided into three distinct parts as below: 1. List of Arguments/Params: (String
name) A list of params is passed in () round brackets. It can have zero or more parameters. Declaring the
type of parameter is optional and can be inferred for the context. 2. Arrow Token: -> Arrow token is known as
the lambda arrow operator. It is used to separate the parameters from the body, or it points the list of
arguments to the body. 3. Expression/Body:
1. s -> s.isEmpty() :
2. Predicate<String> stringPredicate = s -> s.isEmpty();
3. Predicate<List> listPredicate = s -> s.isEmpty();
4. Function<String, Boolean> func = s -> s.isEmpty();
5. Consumer<String> stringConsumer = s -> s.isEmpty();
Example:
}
// Single Abstract Method
public void bar();
}
FunctionalInterface fi = (String name) -> {
System.out.println("HelloScholarhat "+name);
return "HelloScholarhat "+name;
}
{
System.out.println("HelloScholarhat "+name);
return "HelloScholarhat "+name;
}
28. What is an Optional class?
Optional is a container type which may or may not contain value i.e. zero(null) or one(not-null) value. It is part of
java.util package. There are pre-defined methods like isPresent(), which returns true if the value is present or
else false and the method get(), which will return the value if it is present.
29. What are the main components of a Stream?
The components of the stream are:
30. What are the sources of data objects a Stream can process?
A Stream can process the following data:
A collection of an Array.
An I/O channel or an input device.
A data source
Set of Intermediate Operations to process the data source
Single Terminal Operation that produces the result
Integer::parseInt(str)  method reference
str -> Integer.ParseInt(str);  equivalent lambda
static Optional changeCase(String word) {
if (name != null && word.startsWith("A")) {
return Optional.of(word.toUpperCase());
}
e l s e {
r e t u r n O p t i o n a l . o f N u l l a b l e ( w o r d ) ; / / s o m e S t r i n g c a n b e n
}
}
Kick-starts the Stream pipeline.
used to collect the processed Stream data.
Process the stream elements.
Typically transforms a stream into another stream.
Are lazy, i.e., not executed till a terminal operation is invoked.
Does internal iteration of all source elements.
Any number of operations can be chained in the processing pipeline.
Operations are applied as per the defined order.
Intermediate operations are mostly lambda functions.
A reactive source (e.g., comments on social media or tweets/re-tweets)
A stream generator function or a static factory.
To complete some of the intermediate operations, some state is to be maintained, and such intermediate
operations are called stateful intermediate operations.
Parallel execution of these types of operations is complex.
For Eg: sorted() , distinct() , limit() , skip() etc.
Sending data elements to further steps in the pipeline stops till all the data is sorted for sorted() and stream
data elements are stored in temporary data structures.
Filter(Predicate<T>) - Allows selective processing of Stream elements. It returns elements that are satisfying
the supplied condition by the predicate.
map(Funtion<T, R>) - Returns a new Stream, transforming each of the elements by applying the supplied
mapper function.= sorted() - Sorts the input elements and then passes them to the next stage.
distinct() - Only pass on elements to the next stage, not passed yet.
limit(long maxsize) - Limit the stream size to maxsize.
skip(long start) - Skip the initial elements till the start.
peek(Consumer) - Apply a consumer without modification to the stream.
flatMap(mapper) - Transform each element to a stream of its constituent elements and flatten all the streams
into a single stream
32. What are Terminal Operations?
31. What are Intermediate Operations
35. What is the difference between findFirst() and findAny()?
33. What are the most commonly used Intermediate operations?
34. What is the stateful intermediate operation? Give some examples of
stateful intermediate operations.
int count = Stream.of(1, 2, 3, 4, 5)
.filter(i -> i <4) // Intermediate Operation filter
.count(); // Terminal Operation count
Java 8 Interview Questions and Answers for Experienced
36. How are Collections different from Stream?
37. Explain with example, LocalDate, LocalTime, and LocalDateTime APIs.
LocalDate
LocalTime
Date with no time component
Default format - yyyy-MM-dd (2020-02-20)
LocalDate today = LocalDate.now(); // gives today’s date
LocalDate date = LocalDate.of(2011, 12, 30); //(year, month, date)
Time with no date with nanosecond precision
Default format - hh:mm:ss: zzz (12:06:03.015) nanosecond is optional
LocalTime now = LocalTime.now(); // gives time now
LocalTime aTime2 = LocalTime.of(18, 20, 30); // (hours, min, sec)
Holds both Date and Time
Default format - yyyy-MM-dd-HH-mm-ss.zzz (2020-02-20T12:06:03.015)
LocalDateTime timestamp = LocalDateTime.now(); // gives timestamp now
//(year, month, date, hours, min, sec)
LocalDateTime dt1 = LocalDateTime.of(2011, 12, 30, 18, 20, 30);
The skip (long) is an intermediate operation that retrieves the leftover elements after eliminating the initial n
elements of a specific stream.
You can employ lambda expression to implement the abstract method of the functional interface in Java 8.
Below is a coding example of the same:
Example:
LocalDateTime
38. What is the use of JJS in Java 8?
40.Explain the skip(long) using an example.
39. How is the Functional Interface created in Java 8 in detail?
JAVA>jjs
jjs> print("Hello, Java 8 - Welcome to Scholarhat!")
Hello, Java 8 -Welcome to Scholarhat!!
jjs> quit()
>>
import java.util.function.Consumer;
public class FunctionalInterfaceExample {
}
public static void main(String[] args) {
Consumer printer = System.out::println;
printer.accept("Hello, Welcome to scholahat!");
}
import java.util.Arrays;
import java.util.List;
public class SkipExample {
public static void main(String[] args) {
List names = Arrays.asList("Alice", "Bob", "Charlie", "Dave", "Eve");
Output
42. What is the difference between limit and skip?
The limit() method is used to return the Stream of the specified size. For Example, If you have mentioned limit(5),
then the number of output elements would be 5.
41. Is there anything wrong with the following code? Will it compile or give any
specific error?
Try it Yourself >>
Yes. The code will compile because it follows the functional interface specification of defining only a single
abstract method. The second method, printString(), is a default method that does not count as an abstract
method.
Charlie
Dave
Eve
import java.util.stream.Stream;
public class Java8 {
@FunctionalInterface
public interface Test<A, B, C> {
public C apply(A a, B b);
default void printString() {
}
System.out.println("Scholarhat");
}
}
// Create a stream from the names list
names.stream()
// Skip the first two elements of the stream
.skip(2)
// Print the remaining elements to the console
.forEach(System.out::println);
}
Whereas, the skip() method is used to skip the element.
Let’s consider the following example. In the output, the elements are 6, 7, and 8 which means it has skipped the
elements till the 6th index (starting from 1).
The below program is written with the help of the new API introduced in Java 8. We have made use of LocalDate,
LocalTime, and LocalDateTime API to get the current date and time. In the first and second print statement, we
have retrieved the current date and time from the system clock with the time-zone set as default. In the third
print
statement, we have used LocalDateTime API which will print both date and time.
43.How will you get the current date and time using Java 8 Date and Time API?
public static void main(String[] args) {
Stream.of(0,1,2,3,4,5,6,7,8)
.limit(5)
/*limit is set to 5, hence it will print the
numbers starting from 0 to 4
*/
.forEach(num->System.out.print("n"+num));
}
}
import java.util.stream.Stream;
public class Java8 {
public static void main(String[] args) {
}
Stream.of(0,1,2,3,4,5,6,7,8)
.skip(6)
/*
It will skip till 6th index. Hence 7th, 8th and 9th
index elements will be printed
*/
.forEach(num->System.out.print("n"+num));
}
class Java8 {
public static void main(String[] args) {
System.out.println("Current Local Date: " + java.time.LocalDate.now());
//Used LocalDate API to get the date
System.out.println("Current Local Time: " + java.time.LocalTime.now());
//Used LocalTime API to get the time
System.out.println("Current Local Date and Time: " + java.time.LocalDateTime.now());
Syntax
Output
44.What is the purpose of the limit() method in Java 8?
The Stream.limit() method specifies the limit of the elements. The size that you specify in the limit(X), it will return
the Stream of the size of ‘X’. It is a method of java.util.stream.Stream
45.Write a program to print 5 random numbers using forEach in Java 8.
The below program generates 5 random numbers with the help of forEach in Java 8. You can set the limit variable
to any number depending on how many random numbers you want to generate.
Try it Yourself >>
Try it Yourself >>
limit(X)
}
//Used LocalDateTime API to get both date and time
}
Current Local Date: 2024-05-03
Current Local Time: 14:16:35.547225
Current Local Date and Time: 2024-05-03T14:16:35.547937
import java.util.Random;
class Java8 {
public static void main(String[] args) {
Random random = new Random();
random.ints().limit(5).forEach(System.out::println);
/* limit is set to 5 which means only 5 numbers will be printed
with the help of terminal operation forEach
*/
}
}
Output
Example:
map() Vs. flatMap()
If we compare about both we should use map() when we want to perform a simple transformation on each
element in the stream, and flatMap() when we want to produce a stream that contains elements from
multiple sources. Suppose you have a list of lists of integers, and you want to produce a stream of all the
integers in the lists. You could use the map() operation to produce a stream of lists, and then use the
flatMap() operation to produce a stream of integers.
47. What is the difference between Iterator and Spliterator?
46. What is the difference between Stream’s findFirst() and findAny()?.
As the name suggests, the findFirst() method is used to find the first element from the stream whereas the
findAny() method is used to find any element from the stream. The findFirst() is predestinarianism in nature
whereas the findAny() is non-deterministic. In programming, Deterministic means the output is based on
the input or initial state of the system.You can carry out parallel processing.
48. What is the Difference Between Map and flatMap Stream Operation?
Map Stream operation gives one output value per input value whereas flatMap Stream operation gives zero or
more output value per input value. While flatMap Stream operation is used for more complex Stream operation.
Iterator
It was introduced in Java version 1.2
It is used for Collection API.
Spliterator
It was introduced in Java SE 8
It is used for Stream API.
Some of the iterate methods are next() and hasNext() which are
used to iterate elements.
We need to call the iterator() method on the Collection Object.
Spliterator method is tryAdvance().
We need to call the spliterator() method on
Stream Object.
Iterates in Parallel and sequential order.
Iterates only in sequential order.
-1646831910
1354901963
-931542791
1540023264
-2021335902
Output
49. Write down a Java 8 program that can find a Stream’s minimum and
maximum number.
3
4
5
6
7
8
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import java.util.Arrays;
import java.util.stream.IntStream;
public class MinMaxExample {
public static void main(String[] args) {
int[] numbers = {9, 3, 8, 1, 5, 7, 2, 6, 4};
int min = IntStream.of(numbers).min().getAsInt();
int max = IntStream.of(numbers).max().getAsInt();
System.out.println("Minimum number: " + min);
System.out.println("Maximum number: " + max);
}
public class MapFlatMap {
}
public static void main(String[] args) {
List> listOfLists = Arrays.asList(Arrays.asList(3, 4, 5), Arrays.asList(6, 7, 8));
Stream> listStream = listOfLists.stream().map(list -> list);
Stream intStream = listStream.flatMap(list -> list.stream());
System.out.println(intStream);
intStream.forEach(System.out::println);
}
Try it Yourself >>
Output
1
2
3
4
5
Try it Yourself >>
Read More
Top 50 Java Interview Questions and Answers
Top 50 Java MCQ Questions
Java Developer Salary Guide in India
Java Full Stack Developer Salary
Java Multithreading Interview Questions and Answers 2024
So, here we have covered the mostly asked Java8 Questions from basic to advanced for all interested
candidates. For a complete understanding of Java refer to our Java Certification Program.
JJS is a command-line tool used to execute JavaScript code at the console. In Java 8, JJS is the new executable
which is a JavaScript engine.
In Java 8, a new feature was introduced to store classes. The area where all the classes that are stored in Java 8
are called MetaSpace. MetaSpace has replaced the PermGen.
Till Java 7, PermGen was used by Java Virtual Machine to store the classes. Since MetaSpace is dynamic as it
can grow dynamically and it does not have any size limitation, Java 8 replaced PermGen with MetaSpace.
Brush up on object-oriented programming (OOP) principles, such as inheritance, polymorphism, encapsulation,
and abstraction.
50. What is JJS?
Q1. How to prepare for a Java interview for 8 years experience?
Conclusion:
FAQs
}
Minimum number: 1
Maximum number: 9
Q3. What is flatMap in Java 8?
Q2. What is method reference in Java 8 interview questions?
It is a method that, when applied to a stream of values, maps each value to some required output value.
It allows developers to refer to methods or constructors using a concise syntax, improving code readability and
reducing boilerplate code

More Related Content

Similar to Java 8 Interview Questions and Answers PDF By ScholarHat.pdf (20)

PPTX
Insight into java 1.8, OOP VS FP
Syed Awais Mazhar Bukhari
 
DOCX
Colloquium Report
Mohammad Faizan
 
PDF
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
New York City College of Technology Computer Systems Technology Colloquium
 
PDF
Example Of Import Java
Melody Rios
 
PDF
Java 8-revealed
Hamed Hatami
 
PDF
Java 8 Overview
Nicola Pedot
 
PPTX
Java 8
Sudipta K Paik
 
PPTX
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Emiel Paasschens
 
PPTX
New Features of JAVA SE8
Dinesh Pathak
 
PDF
Automatic Migration of Legacy Java Method Implementations to Interfaces
Raffi Khatchadourian
 
PPTX
Java_Interview Qns
ManikandanRamanujam
 
PPT
14274730 (1).ppt
aptechaligarh
 
PPTX
Java-Development-A-Comprehensive-Guide.pptx
ak7209566972
 
PDF
Java Interview Questions for 10+ Year Experienced PDF By ScholarHat
Scholarhat
 
PDF
Lambdas And Streams in JDK8
Simon Ritter
 
PDF
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
JAXLondon2014
 
PPTX
Reactive Programming on Android - RxAndroid - RxJava
Ali Muzaffar
 
PPTX
Java 8 - An Overview
Indrajit Das
 
PDF
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Raffi Khatchadourian
 
DOC
Spatial approximate string search Doc
Sudha Hari Tech Solution Pvt ltd
 
Insight into java 1.8, OOP VS FP
Syed Awais Mazhar Bukhari
 
Colloquium Report
Mohammad Faizan
 
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
New York City College of Technology Computer Systems Technology Colloquium
 
Example Of Import Java
Melody Rios
 
Java 8-revealed
Hamed Hatami
 
Java 8 Overview
Nicola Pedot
 
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Emiel Paasschens
 
New Features of JAVA SE8
Dinesh Pathak
 
Automatic Migration of Legacy Java Method Implementations to Interfaces
Raffi Khatchadourian
 
Java_Interview Qns
ManikandanRamanujam
 
14274730 (1).ppt
aptechaligarh
 
Java-Development-A-Comprehensive-Guide.pptx
ak7209566972
 
Java Interview Questions for 10+ Year Experienced PDF By ScholarHat
Scholarhat
 
Lambdas And Streams in JDK8
Simon Ritter
 
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
JAXLondon2014
 
Reactive Programming on Android - RxAndroid - RxJava
Ali Muzaffar
 
Java 8 - An Overview
Indrajit Das
 
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Raffi Khatchadourian
 
Spatial approximate string search Doc
Sudha Hari Tech Solution Pvt ltd
 

More from Scholarhat (20)

PDF
React Redux Interview Questions PDF By ScholarHat
Scholarhat
 
PDF
React Redux Interview Questions PDF By ScholarHat
Scholarhat
 
PDF
React Router Interview Questions PDF By ScholarHat
Scholarhat
 
PDF
JavaScript Array Interview Questions PDF By ScholarHat
Scholarhat
 
PDF
Java Interview Questions PDF By ScholarHat
Scholarhat
 
PDF
Infosys Angular Interview Questions PDF By ScholarHat
Scholarhat
 
PDF
DBMS Interview Questions PDF By ScholarHat
Scholarhat
 
PDF
API Testing Interview Questions PDF By ScholarHat
Scholarhat
 
PDF
System Design Interview Questions PDF By ScholarHat
Scholarhat
 
PDF
Python Viva Interview Questions PDF By ScholarHat
Scholarhat
 
PDF
Linux Interview Questions PDF By ScholarHat
Scholarhat
 
PDF
Kubernetes Interview Questions PDF By ScholarHat
Scholarhat
 
PDF
Collections in Java Interview Questions PDF By ScholarHat
Scholarhat
 
PDF
CI CD Pipeline Interview Questions PDF By ScholarHat
Scholarhat
 
PDF
Azure DevOps Interview Questions PDF By ScholarHat
Scholarhat
 
PDF
TypeScript Interview Questions PDF By ScholarHat
Scholarhat
 
PDF
UIUX Interview Questions PDF By ScholarHat
Scholarhat
 
PDF
Python Interview Questions PDF By ScholarHat
Scholarhat
 
PDF
OOPS JavaScript Interview Questions PDF By ScholarHat
Scholarhat
 
PDF
Git Interview Questions PDF By ScholarHat
Scholarhat
 
React Redux Interview Questions PDF By ScholarHat
Scholarhat
 
React Redux Interview Questions PDF By ScholarHat
Scholarhat
 
React Router Interview Questions PDF By ScholarHat
Scholarhat
 
JavaScript Array Interview Questions PDF By ScholarHat
Scholarhat
 
Java Interview Questions PDF By ScholarHat
Scholarhat
 
Infosys Angular Interview Questions PDF By ScholarHat
Scholarhat
 
DBMS Interview Questions PDF By ScholarHat
Scholarhat
 
API Testing Interview Questions PDF By ScholarHat
Scholarhat
 
System Design Interview Questions PDF By ScholarHat
Scholarhat
 
Python Viva Interview Questions PDF By ScholarHat
Scholarhat
 
Linux Interview Questions PDF By ScholarHat
Scholarhat
 
Kubernetes Interview Questions PDF By ScholarHat
Scholarhat
 
Collections in Java Interview Questions PDF By ScholarHat
Scholarhat
 
CI CD Pipeline Interview Questions PDF By ScholarHat
Scholarhat
 
Azure DevOps Interview Questions PDF By ScholarHat
Scholarhat
 
TypeScript Interview Questions PDF By ScholarHat
Scholarhat
 
UIUX Interview Questions PDF By ScholarHat
Scholarhat
 
Python Interview Questions PDF By ScholarHat
Scholarhat
 
OOPS JavaScript Interview Questions PDF By ScholarHat
Scholarhat
 
Git Interview Questions PDF By ScholarHat
Scholarhat
 
Ad

Recently uploaded (20)

PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PPTX
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PPTX
Introduction to Indian Writing in English
Trushali Dodiya
 
PDF
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
PPTX
Controller Request and Response in Odoo18
Celine George
 
PDF
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PPTX
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
Introduction to Indian Writing in English
Trushali Dodiya
 
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
Controller Request and Response in Odoo18
Celine George
 
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
Ad

Java 8 Interview Questions and Answers PDF By ScholarHat.pdf

  • 1. Top 50 Java 8 Interview Questions and Answers Lambda expressions A way to characterize and pass around blocks of code as if they were objects, which permits for more concise, functional-style code. Functional interfacing Java 8 is a known language for every Java developer. The latest release contains new Java Features, enhancements, and bug fixes to improve efficiency in developing and running Java programs. So, acquiring knowledge about Java 8 is now a need for every Java developer. Learning Java 8 can significantly contribute to your success in the Java developer journey. Hence In this Java Tutorial, we will explore the top 50 Java 8 Interview Questions and Answers. These questions cover both standard and advanced commonly encountered topics during interviews. By thoroughly reading and understanding these questions, you’ll be well-prepared for your Java interview. Let’s start with Java 8 Interview Questions & Answers for Beginners. Java 8 Interview Questions & Answers for Beginners 1. What are the features of Java 8?
  • 2. Interfacing has precisely one unique method, which allows for behavior parameterization and the capacity to pass behavior as a method argument. Streams A modern API for processing collections of information that allows for operations such as filtering, mapping, and decreasing to be performed in a more functional and clear way. Date and time API A modern API for working with date and time, which replaces the legacy java.util.Date and java.util.Calendar classes. Concurrent Accumulators A set of classes planned for utilization with parallel streams, which permit for the effective amassing Lambda expressions can be utilized to define functional interfacing, which are interfacing that have a single abstract method. The java.util.function package in Java 8 incorporates a few functional interfacing such as Customer, Work, Predicate, and Provider. Lambda expressions can also be passed to methods or utilized as arguments for functional interfacing. For case, the forEach method of the java.util.stream.Stream class takes a Consumer functional interface as an argument, allowing us to pass in a lambda expression to perform a particular action on each component within the stream. Lambda expressions can also be used with other features of Java 8 such as streams and the new date and time API to perform operations such as filtering, mapping, and decreasing collections of information, in a more functional and clear way. It's worth noticing that, in spite of the fact that lambda expressions can offer assistance to make your code more concise and readable, they can also make it more difficult to get it if they are not used accurately. It's critical to utilize them in a way that produces the code simple to understand. Lambda expressions are nothing but a new feature that allows programmers to write more concise, functional- style code. It is composed of three parts: A list of parameters (or none) is enclosed in parentheses or brackets. The “arrow” token -> The body of the lambda expression can be a single expression or a block of code. 2. In Java 8, What are lambda expressions and their use? Uses of lambda expressions Syntax of simple lambda expression (int x, int y) -> {return x + y; }
  • 3. 5. What are Java 8 significant advantages? 3. What are different kinds of Method References? 4. In which programming paradigm did Java 8 fall? It is Compact, readable, and reusable code. It has Less boilerplate code. It can do Parallel operations and execution. It can be ported across operating systems. It has High stability.
  • 4. MetaSpace: static methods in interfaces It has a Stable environment. Static methods, which contain method implementation are owned by the interface and are invoked using the name of the interface, it is suitable for defining the utility methods and cannot be overridden. Java 8 stores the MetaData of classes in local memory called 'MetaSpace'. It is not a contiguous Heap Memory and hence can be developed dynamically which makes a difference to overcome the size limitations. This moves forward the garbage collection, auto-tuning, and de-allocation of metadata. PremGen: PreGem stands forPermanent-Generation, MetaData data of classes was stored in PremGen memory type before Java 8. PremGen is settled in size and cannot be dynamically resized. It was a contiguous Java Heap Memory. 7. What are static methods in Interfaces? 6. What is MetaSpace in Java 8? How does it differ from PermGen? Example: interface DemoInterface { // static method static void hello() { System.out.println("Hello, Welcome to Scholarhat");
  • 5. Output 8. What are some standard Java pre-defined functional interfaces? Try it Yourself >> Runnable: used to execute the instances of a class over another thread with no arguments and no return value. Callable: used to execute the instances of a class over another thread with no arguments and it either returns a value or throws an exception. Comparator: used to sort different objects in a user-defined order Comparable: used to sort objects in the natural sort order } } } } Hello, Welcome to Scholarhat Hello, Override Method here // Implementing interface method @Override public void overrideMethod(String str) { System.out.println(str); } // Public and abstract method of Interface void overrideMethod(String str); // Implementation Class public class InterfaceEx implements DemoInterface { // Calling the static method of interface DemoInterface.hello(); public static void main(String[] args) { InterfaceEx interfaceEx= new InterfaceEx(); // Calling the abstract method of interface interfaceEx.overrideMethod("Hello, Override Method here");
  • 6. 14. What Is JJS? In Java 8, jjs is the new executable or command line tool we use to execute Javascript code at the console. 13. What Is Nashorn in Java8? 15. What Is Stream Pipelining in Java 8? 11. What Is the Meaning of String::Valueof Expression? String::Valueof Expression( ) is a static method reference to the valueOf method of the String class. 10. How does a lambda expression relate to a functional interface? 12. Explain Some of the Functional Interfaces in the Standard Library 9. What are the various categories of pre-defined function interfaces? 1. Function: To transform arguments in returnable value. 2. Predicate: To perform a test and return a Boolean value. 3. Consumer: Accept arguments but do not return any values. 4. Supplier: Do not accept any arguments but return a value. 5. Operator: Perform a reduction-type operation that accepts the same input types. Function – takes one argument and returns a result Consumer – takes one argument and returns no result (represents a side effect) Supplier – takes no arguments and returns a result Predicate – takes one argument and returns a boolean BiFunction – takes two arguments and returns a result BinaryOperator – is similar to a BiFunction, taking two arguments and returning a result. The two arguments and the result are all of the same type. Stream pipelining is the concept of chaining operations together. We do this by splitting the operations that can happen on a stream into two categories: intermediate operations and terminal operations. Each intermediate operation returns an instance of Stream itself when it runs. Therefore, we can set up an arbitrary number of intermediate operations to process data, forming a processing pipeline. It is a type of function without a name. It may or may not have its results and parameters. It is also known as an anonymous function as it does not have type information by itself. It is executed on demand. It is advantageous in iterating, filtering, and extracting data from a collection. As lambda expressions are similar to anonymous functions, they can only be applied to the single abstract method of Functional Interface. It will infer the return type, type, and several arguments from the signature of the abstract method of the functional interface. Nashorn is the new Javascript processing engine for the Java platform that shipped with Java 8. Until JDK 7, the Java platform used Mozilla Rhino for the same purpose, as a Javascript processing engine. Nashorn provides better compliance with the ECMA-normalized JavaScript specification and better runtime performance than its predecessor.
  • 7. For executing lazy operations To perform database operations Use for internal iterations. For writing functional-style programming You can use it for using pipeline operations. It helps in creating applications much faster and in an easier way. It provides a very stable ambient for the developers. Concise, reusable, and easy-to-comprehend codes. Improved and effective support. Easy to port across various operating systems. Minimum boilerplate codes. Functional Interfaces are interfaces with only one abstract method. Due to this, it is also known as the Single Abstract Method (SAM) interface. There must then be a terminal operation that returns a final value and terminates the pipeline. A predicate is a functional interface that typically receives arguments and retrieves a Boolean value. You can use it to apply the filter for a collection of objects. On the other hand, the consumer is referred to as an in-build functional interface found in Java.util.function package. You can use it to consume any object, and it takes the input value and gives out nothing. The peek() method helps support debugging, where one wants to notice the elements as they tend to flow from a specific point in a pipeline. It is a representation of our observation of how each element passes. A collection is an in-memory database that records all the values according to the current data structure. So, before you add it to the collection, it’s important to compute each of them. Whereas a stream is a visually fixed data structure where we can compute the elements according to our needs. 16. Specify the advantages of using Java 8 21. What are functional or SAM interfaces? 19. Why is the peek () method used in Java 8? 18. What are predicate and consumer in Java 8? 17. What is a collection, and how is it different from a stream? 20. Which situation is most suitable for using stream API in Java 8? Syntax: Java 8 Interview Questions and Answers for Intermediate public Object peek()
  • 8. It is also known as a functional interface because it wraps a function as an interface. These interfaces can have any number of default, static, and overridden methods. If declaring Functional Interfaces @FunctionalInterface annotation is optional to use. If this annotation is used for interfaces with more than one abstract method, it will generate a compiler error. Type Inference assists the compiler in identifying or recognizing the types of arguments by just having an overview of the corresponding declaration and method invocation. As shown in the above code, It will also extend the abstract method of the Parent Interface. Hence it will have more than one abstract method And will give a compiler error. No, we can not extend or inherit a functional interface into another interface also it can not be be achievable through abstract methods as it will void the rule of one abstract method per functional interface. A method in the interface that has a predefined body is known as the default method. It uses the keyword default. default methods were introduced in Java 8 to have 'Backward Compatibility in case JDK modifies any interfaces. In case a new abstract method is added to the interface, all classes implementing the interface will break and will have to implement the new method. With default methods, there will not be any impact on the interface implementing classes. Default methods can be overridden if needed in the implementation. Also, it does not qualify as synchronized or final. 23. What is the function of Type Inference? 24. What is the default method, and why is it required? 22. Can we extend the functional interface to another interface? Example: @FunctionalInterface // Annotation is optional public interface Foo() { // Default Method - Optional can be 0 or more public default String Scholarhat() { return "Welcome to Scholarhat"; interface Student{ public int studenttMethod(); } @FunctionalInterface // This cannot be FunctionalInterface interface Child extends Parent { public int childMethod(); }
  • 9. 27. In Java 8, what is Method Reference? Method reference is a compact way of referring to a method of functional interface. It is used to refer to a method without invoking it. :: (double colon) is used for describing the method reference. The syntax is class::methodName 25. What is the basic structure/syntax of a lambda expression? 26. What are the types and common ways to use lambda expressions? A lambda expression does not have any specific type by itself. A lambda expression receives type once it is assigned to a functional interface. That same lambda expression can be assigned to different functional interface types and can have a different type. A body can have expressions or statements. {} curly braces are only required when there is more than one line. In one statement, the return type is the same as the return type of the statement. In other cases, the return type is either inferred by the return keyword or void if nothing is returned. A lambda expression can be divided into three distinct parts as below: 1. List of Arguments/Params: (String name) A list of params is passed in () round brackets. It can have zero or more parameters. Declaring the type of parameter is optional and can be inferred for the context. 2. Arrow Token: -> Arrow token is known as the lambda arrow operator. It is used to separate the parameters from the body, or it points the list of arguments to the body. 3. Expression/Body: 1. s -> s.isEmpty() : 2. Predicate<String> stringPredicate = s -> s.isEmpty(); 3. Predicate<List> listPredicate = s -> s.isEmpty(); 4. Function<String, Boolean> func = s -> s.isEmpty(); 5. Consumer<String> stringConsumer = s -> s.isEmpty(); Example: } // Single Abstract Method public void bar(); } FunctionalInterface fi = (String name) -> { System.out.println("HelloScholarhat "+name); return "HelloScholarhat "+name; } { System.out.println("HelloScholarhat "+name); return "HelloScholarhat "+name; }
  • 10. 28. What is an Optional class? Optional is a container type which may or may not contain value i.e. zero(null) or one(not-null) value. It is part of java.util package. There are pre-defined methods like isPresent(), which returns true if the value is present or else false and the method get(), which will return the value if it is present. 29. What are the main components of a Stream? The components of the stream are: 30. What are the sources of data objects a Stream can process? A Stream can process the following data: A collection of an Array. An I/O channel or an input device. A data source Set of Intermediate Operations to process the data source Single Terminal Operation that produces the result Integer::parseInt(str) method reference str -> Integer.ParseInt(str); equivalent lambda static Optional changeCase(String word) { if (name != null && word.startsWith("A")) { return Optional.of(word.toUpperCase()); } e l s e { r e t u r n O p t i o n a l . o f N u l l a b l e ( w o r d ) ; / / s o m e S t r i n g c a n b e n } }
  • 11. Kick-starts the Stream pipeline. used to collect the processed Stream data. Process the stream elements. Typically transforms a stream into another stream. Are lazy, i.e., not executed till a terminal operation is invoked. Does internal iteration of all source elements. Any number of operations can be chained in the processing pipeline. Operations are applied as per the defined order. Intermediate operations are mostly lambda functions. A reactive source (e.g., comments on social media or tweets/re-tweets) A stream generator function or a static factory. To complete some of the intermediate operations, some state is to be maintained, and such intermediate operations are called stateful intermediate operations. Parallel execution of these types of operations is complex. For Eg: sorted() , distinct() , limit() , skip() etc. Sending data elements to further steps in the pipeline stops till all the data is sorted for sorted() and stream data elements are stored in temporary data structures. Filter(Predicate<T>) - Allows selective processing of Stream elements. It returns elements that are satisfying the supplied condition by the predicate. map(Funtion<T, R>) - Returns a new Stream, transforming each of the elements by applying the supplied mapper function.= sorted() - Sorts the input elements and then passes them to the next stage. distinct() - Only pass on elements to the next stage, not passed yet. limit(long maxsize) - Limit the stream size to maxsize. skip(long start) - Skip the initial elements till the start. peek(Consumer) - Apply a consumer without modification to the stream. flatMap(mapper) - Transform each element to a stream of its constituent elements and flatten all the streams into a single stream 32. What are Terminal Operations? 31. What are Intermediate Operations 35. What is the difference between findFirst() and findAny()? 33. What are the most commonly used Intermediate operations? 34. What is the stateful intermediate operation? Give some examples of stateful intermediate operations. int count = Stream.of(1, 2, 3, 4, 5) .filter(i -> i <4) // Intermediate Operation filter .count(); // Terminal Operation count
  • 12. Java 8 Interview Questions and Answers for Experienced 36. How are Collections different from Stream? 37. Explain with example, LocalDate, LocalTime, and LocalDateTime APIs. LocalDate LocalTime Date with no time component Default format - yyyy-MM-dd (2020-02-20) LocalDate today = LocalDate.now(); // gives today’s date LocalDate date = LocalDate.of(2011, 12, 30); //(year, month, date)
  • 13. Time with no date with nanosecond precision Default format - hh:mm:ss: zzz (12:06:03.015) nanosecond is optional LocalTime now = LocalTime.now(); // gives time now LocalTime aTime2 = LocalTime.of(18, 20, 30); // (hours, min, sec) Holds both Date and Time Default format - yyyy-MM-dd-HH-mm-ss.zzz (2020-02-20T12:06:03.015) LocalDateTime timestamp = LocalDateTime.now(); // gives timestamp now //(year, month, date, hours, min, sec) LocalDateTime dt1 = LocalDateTime.of(2011, 12, 30, 18, 20, 30); The skip (long) is an intermediate operation that retrieves the leftover elements after eliminating the initial n elements of a specific stream. You can employ lambda expression to implement the abstract method of the functional interface in Java 8. Below is a coding example of the same: Example: LocalDateTime 38. What is the use of JJS in Java 8? 40.Explain the skip(long) using an example. 39. How is the Functional Interface created in Java 8 in detail? JAVA>jjs jjs> print("Hello, Java 8 - Welcome to Scholarhat!") Hello, Java 8 -Welcome to Scholarhat!! jjs> quit() >> import java.util.function.Consumer; public class FunctionalInterfaceExample { } public static void main(String[] args) { Consumer printer = System.out::println; printer.accept("Hello, Welcome to scholahat!"); } import java.util.Arrays; import java.util.List; public class SkipExample { public static void main(String[] args) { List names = Arrays.asList("Alice", "Bob", "Charlie", "Dave", "Eve");
  • 14. Output 42. What is the difference between limit and skip? The limit() method is used to return the Stream of the specified size. For Example, If you have mentioned limit(5), then the number of output elements would be 5. 41. Is there anything wrong with the following code? Will it compile or give any specific error? Try it Yourself >> Yes. The code will compile because it follows the functional interface specification of defining only a single abstract method. The second method, printString(), is a default method that does not count as an abstract method. Charlie Dave Eve import java.util.stream.Stream; public class Java8 { @FunctionalInterface public interface Test<A, B, C> { public C apply(A a, B b); default void printString() { } System.out.println("Scholarhat"); } } // Create a stream from the names list names.stream() // Skip the first two elements of the stream .skip(2) // Print the remaining elements to the console .forEach(System.out::println); }
  • 15. Whereas, the skip() method is used to skip the element. Let’s consider the following example. In the output, the elements are 6, 7, and 8 which means it has skipped the elements till the 6th index (starting from 1). The below program is written with the help of the new API introduced in Java 8. We have made use of LocalDate, LocalTime, and LocalDateTime API to get the current date and time. In the first and second print statement, we have retrieved the current date and time from the system clock with the time-zone set as default. In the third print statement, we have used LocalDateTime API which will print both date and time. 43.How will you get the current date and time using Java 8 Date and Time API? public static void main(String[] args) { Stream.of(0,1,2,3,4,5,6,7,8) .limit(5) /*limit is set to 5, hence it will print the numbers starting from 0 to 4 */ .forEach(num->System.out.print("n"+num)); } } import java.util.stream.Stream; public class Java8 { public static void main(String[] args) { } Stream.of(0,1,2,3,4,5,6,7,8) .skip(6) /* It will skip till 6th index. Hence 7th, 8th and 9th index elements will be printed */ .forEach(num->System.out.print("n"+num)); } class Java8 { public static void main(String[] args) { System.out.println("Current Local Date: " + java.time.LocalDate.now()); //Used LocalDate API to get the date System.out.println("Current Local Time: " + java.time.LocalTime.now()); //Used LocalTime API to get the time System.out.println("Current Local Date and Time: " + java.time.LocalDateTime.now());
  • 16. Syntax Output 44.What is the purpose of the limit() method in Java 8? The Stream.limit() method specifies the limit of the elements. The size that you specify in the limit(X), it will return the Stream of the size of ‘X’. It is a method of java.util.stream.Stream 45.Write a program to print 5 random numbers using forEach in Java 8. The below program generates 5 random numbers with the help of forEach in Java 8. You can set the limit variable to any number depending on how many random numbers you want to generate. Try it Yourself >> Try it Yourself >> limit(X) } //Used LocalDateTime API to get both date and time } Current Local Date: 2024-05-03 Current Local Time: 14:16:35.547225 Current Local Date and Time: 2024-05-03T14:16:35.547937 import java.util.Random; class Java8 { public static void main(String[] args) { Random random = new Random(); random.ints().limit(5).forEach(System.out::println); /* limit is set to 5 which means only 5 numbers will be printed with the help of terminal operation forEach */ } }
  • 17. Output Example: map() Vs. flatMap() If we compare about both we should use map() when we want to perform a simple transformation on each element in the stream, and flatMap() when we want to produce a stream that contains elements from multiple sources. Suppose you have a list of lists of integers, and you want to produce a stream of all the integers in the lists. You could use the map() operation to produce a stream of lists, and then use the flatMap() operation to produce a stream of integers. 47. What is the difference between Iterator and Spliterator? 46. What is the difference between Stream’s findFirst() and findAny()?. As the name suggests, the findFirst() method is used to find the first element from the stream whereas the findAny() method is used to find any element from the stream. The findFirst() is predestinarianism in nature whereas the findAny() is non-deterministic. In programming, Deterministic means the output is based on the input or initial state of the system.You can carry out parallel processing. 48. What is the Difference Between Map and flatMap Stream Operation? Map Stream operation gives one output value per input value whereas flatMap Stream operation gives zero or more output value per input value. While flatMap Stream operation is used for more complex Stream operation. Iterator It was introduced in Java version 1.2 It is used for Collection API. Spliterator It was introduced in Java SE 8 It is used for Stream API. Some of the iterate methods are next() and hasNext() which are used to iterate elements. We need to call the iterator() method on the Collection Object. Spliterator method is tryAdvance(). We need to call the spliterator() method on Stream Object. Iterates in Parallel and sequential order. Iterates only in sequential order. -1646831910 1354901963 -931542791 1540023264 -2021335902
  • 18. Output 49. Write down a Java 8 program that can find a Stream’s minimum and maximum number. 3 4 5 6 7 8 import java.util.Arrays; import java.util.List; import java.util.stream.Stream; import java.util.Arrays; import java.util.stream.IntStream; public class MinMaxExample { public static void main(String[] args) { int[] numbers = {9, 3, 8, 1, 5, 7, 2, 6, 4}; int min = IntStream.of(numbers).min().getAsInt(); int max = IntStream.of(numbers).max().getAsInt(); System.out.println("Minimum number: " + min); System.out.println("Maximum number: " + max); } public class MapFlatMap { } public static void main(String[] args) { List> listOfLists = Arrays.asList(Arrays.asList(3, 4, 5), Arrays.asList(6, 7, 8)); Stream> listStream = listOfLists.stream().map(list -> list); Stream intStream = listStream.flatMap(list -> list.stream()); System.out.println(intStream); intStream.forEach(System.out::println); } Try it Yourself >>
  • 19. Output 1 2 3 4 5 Try it Yourself >> Read More Top 50 Java Interview Questions and Answers Top 50 Java MCQ Questions Java Developer Salary Guide in India Java Full Stack Developer Salary Java Multithreading Interview Questions and Answers 2024 So, here we have covered the mostly asked Java8 Questions from basic to advanced for all interested candidates. For a complete understanding of Java refer to our Java Certification Program. JJS is a command-line tool used to execute JavaScript code at the console. In Java 8, JJS is the new executable which is a JavaScript engine. In Java 8, a new feature was introduced to store classes. The area where all the classes that are stored in Java 8 are called MetaSpace. MetaSpace has replaced the PermGen. Till Java 7, PermGen was used by Java Virtual Machine to store the classes. Since MetaSpace is dynamic as it can grow dynamically and it does not have any size limitation, Java 8 replaced PermGen with MetaSpace. Brush up on object-oriented programming (OOP) principles, such as inheritance, polymorphism, encapsulation, and abstraction. 50. What is JJS? Q1. How to prepare for a Java interview for 8 years experience? Conclusion: FAQs } Minimum number: 1 Maximum number: 9
  • 20. Q3. What is flatMap in Java 8? Q2. What is method reference in Java 8 interview questions? It is a method that, when applied to a stream of values, maps each value to some required output value. It allows developers to refer to methods or constructors using a concise syntax, improving code readability and reducing boilerplate code