Java8_LatestInterviewQuestions
Java8_LatestInterviewQuestions
8 features
High stability.
Stable environment.
Adequate support
contiguous Heap Memory and hence can be grown dynamically which helps to overcome the size
constraints. This improves the garbage collection, auto-tuning, and de-allocation of metadata.
Functional Interfaces are an interface with only one abstract method. Due to which it is also known as
the Single Abstract Method (SAM) interface
Functional interfaces can have any number of default, static, and overridden methods. For 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.
6. Can a functional interface extend/inherit another interface?
A functional interface cannot extend another interface with abstract methods as it will void the rule of
one abstract method per functional interface.
It can extend other interfaces which do not have any abstract method and only have the default, static,
another class is overridden, and normal methods.
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.
Runnable: use to execute the instances of a class over another thread with no arguments and no return
value.
Callable: use to execute the instances of a class over another thread with no arguments and it either
returns a value or throws an exception.
Comparator: use to sort different objects in a user-defined order
Comparable: use to sort objects in the natural sort order
11. What is the lambda expression in Java and How does a lambda expression
relate to a functional interface?
Lambda expression is a type of function without a name. It may or may not have results and
parameters. It is known as an anonymous function as it does not have type information by itself. It is
executed on-demand. It is beneficial 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 functional interface
15) What are the two types of common operations of Java Streams?
17) Explain some of the Intermediate operations which are used commonly in the
Java programming language?
Some of the Intermediate operations that are used commonly in the Java are:
Stream.filter(): The Java Stream filter() method is one of the significant operations under intermediate
operations. The filter() method accepts a Predicate to separate all components of the stream. It is under
intermediate operations because it enables the users to call another stream operation (for example -
Stream.forEach() method) on the result. The predicate should declare true if the component is to be
introduced in the final Stream, and the Predicate will reflect false if the component should not be
included in the final stream.
Stream.map(): The intermediate operation Stream.map(), with the help of the given function,
transforms each component in the stream into some other object. In simple words, the Stream.map()
operation converts the element of a stream into something else. It takes the given function and applies
that function to every element of the stream, which at the end returns a stream of the values that are
produced by the parameter function. Stream.map() also supports the user to create a calculation on the
data inside a Java stream. For example, if a user has a list of strings, the user can transform each string
from the given list to uppercase, lowercase, or to a substring of the original string and something
entirely else.
Stream.sorted(): The Java Stream sorted() method is also an intermediate operation or method. This
sorted() operation returns a classified (or we can say the sorted) view of the stream. The components in
the stream are sorted in a usual manner unless the user passes or uses a custom Comparator if they want
to sort the stream in a different way.
18) Explain some of the Terminal operations which are used commonly in the Java
programming language?
Some of the Terminal operations which are used commonly in the Java programming language are:
Stream.forEach(): The Java Stream forEach() method or operation is one of the terminal operations in
which starts the internal iteration of the components in the stream. In simple words, the Java Stream
forEach() method helps in repeating over all of the components of a stream and execute some process
on each of them. The forEach() method returns nothing, or we can say void. The process to be
performed on the elements of the stream is transferred as the Java lambda expression.
Stream.collect(): The Java Stream.collect() method is practiced to take elements from a stream and
save all of them in a group. Stream collect() is a method to get away from the environment of streams
and receive a solid combination of values, like an array list or a list. The Java Stream collect() method
is a terminal operation that starts the internal iteration of elements and collects the elements in the
stream in a collection or object of some kind.
Stream anyMatch(): The anyMatch() method of Java stream is also one of the terminal operations that
inputs a single Predicate or condition as a parameter and starts the internal iteration of the Stream. The
anyMatch() method implements the Predicate parameter to every component of the Stream. If the given
Predicate reflects true for any of the components of the stream, the anyMatch() method yields true. If
no elements match the Predicate, anyMatch() will return false.
Stream allMatch(): The allMatch() method of Java stream is also one of the terminal operations that
inputs a single Predicate or condition as a parameter and starts the internal iteration of the Stream. The
allMatch() method implements the Predicate parameter to every component of the Stream. If the given
Predicate reflects true for all of the components of the stream, the allMatch() method yields true. If not
all the elements match the Predicate, the allMatch() will return false.
Stream noneMatch(): The noneMatch() method of Java stream is also one of the terminal operations
that inputs a single Predicate or condition as a parameter and starts the internal iteration of the Stream.
The noneMatch() method implements the Predicate parameter to every component of the Stream. The
noneMatch() method will return true if no elements are matched by the Predicate and will return false if
one or more elements are matched with the predicate.
Stream.count(): The Java Stream count() method is also a terminal operation that reflects the number
of components present in the stream. The number of elements present in the stream is produced and
returned in the form of a long return type. In simple words, the Java Stream count() method starts the
internal iteration of the elements in the Stream and counts the elements present in it.
Stream.reduce(): The Java Stream reduce() method is also a terminal operation that helps in reducing
all the components of the stream to only a single component. The reduce() method performs a
reduction on the components of the stream with the provided function. The result of the reduce()
method is an Optional (Optional is a process of restoring a nullable T reference with a non-null value),
which holds the reduced value of the stream.
20) What are the major points of differentiation between the Iterator and the
Spliterator?
Collections Streams
Data structure holds all the data No data is stored. Have the capacity to process an infinite number of
elements elements on demand
External Iteration Internal Iteration
Can be processed any number of
Traversed only once
times
Elements are easy to access No direct way of accessing specific elements
Is a data store Is an API to process the data
18. What is the feature of the new Date and Time API in Java 8?
Immutable classes and Thread-safe
Timezone support
Fluent methods for object creation and arithmetic
Addresses I18N issue for earlier APIs
Influenced by popular joda-time package
All packages are based on the ISO-8601 calendar system
LocalDate
LocalTime
LocalDateTime
Java 8 has launched a unique class, Optional in java.util package. It can assist in drafting a clean code
without practicing multiple null tests. By working with Optional, we can define substitute values to
restore or alternate code to run. This makes the code more understandable because the actions which
were deceived are now obvious to the developer.
Optional can also be defined as an object of container which might contains or not contain a non-null
value. You need to import the java.util package to run this class. If the situation exists, isPresent () will
return true and get () will return the value. Additional methods based on the presence or absence of a
value obtained are introduced, such as orElse (), which provides the default value when the value is not
present, and ifPresent (), which creates a chunk of code when the value is present. This is the category
based on the number, that is, their cases:
Final and immutable (though may include references to mutable objects).
Acknowledged even individually based on equals(), not based on reference equation(==).
Do not have available constructors.
14. Describe the more commonly found functional interfaces in the standard
library.
Although many functional interfaces exist, these are the one's users most likely encounter:
Also, streams are inherently lazily loaded and processed, unlike collections.
Collections in Java are an in-memory data structure that holds the value of the current data structure,
whereas a stream is not a data structure; streams take input from collections, arrays, or I/O sources.
17. What were the issues that were fixed with the new Date and Time API of Java
8?
With the older versions of Java, java.util.The date was mutable. This means it has absolutely no thread
safety.
Also, java.text.SimpleDateFormat was not thread-safe in the older versions. The older Date and Time
API was difficult to understand for programmers in terms of readability too.
An optional object can be created easily using the empty() static method as shown below:
@Test
3 publicvoidwhenCreatesEmptyOptional_thenCorrec
t() {
4 Optional<String> empty = Optional.empty();
assertFalse(empty.isPresent());
5 }
6
7
8
9
PermGen is known as the memory space which is used for collecting class data like byte code, static
variables, and many more. The space allocated to PermGen is by default 64Mb. The PermGen can also
be attuned with the help of -XXMaxPermSize.
In Java SE 8, the PermGen process area was restored with MetaSpace. Java developers have transferred
permGem to the unique consciousness in the original OS, which is now commonly known as
MetaSpace. MetaSpace can auto-enhance its capacity, by default. In MetaSpace, the classes can store
and discharge during the total life sentence of the Java Virtual Machine (JVM).
Differences between PermGen and MetaSpace
Basic: PermGen is the memory operation for saving class data like a static variable, byte
code, etc. In Java SE 8, the PermGen purpose section was substituted with MetaSpace.
Default Memory Allocation: The space allocated to PermGen is by default 64Mb. In
metaSpace, it can, by default, auto increase its size.
Tuned-up Memory Flag: PermGen can be harmonized by applying-XXMaxPermSize. In
metaSpace, we can reduce the upper bound of the memory by -XX: MaxMetaspaceSize.
Memory Area - PermGen is a unique Heap space. Since the introduction of Java SE 8,
MetaSpace is now including a separate storage space in the original Operation System (OS).