Java 8 Flashcards

1
Q

What are the new features

released in Java 8?

A

The new features released in Java 8 are:

  1. Lambda Expression
  2. Stream API
  3. Date and Time API
  4. Functional Interface
  5. Interface Default and Static Methods
  6. Optional
  7. Base64 Encoding and Decoding
  8. Nashorn JavaScript Engine
  9. Collections API Enhancements
  10. Concurrency Enhancements
  11. Fork/Join Framework Enhancements
  12. Spliterator
  13. Internal Iteration
  14. Type Annotations and Repeatable Annotations
  15. Method Parameter Reflection
  16. JVM Parameter Changes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the main benefits of new features introduced in Java 8?

A

The main benefits of Java 8 features are:
1. Support for functional programming by Lambda and
Streams
2. Ease of high volume data processing by Streams
3. Ease of use by getting Parameter names through Reflection
4. Reusable code with enhanced Collection APIs
5. Smart exception handling with Optional
6. Control on JVM with new Parameters
7. Enhanced encryption support with Base 64
8. Faster execution with Nashorn JavaScript engine support

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a Lambda expression

in Java 8?

A

Lambda expression is an anonymous function. It is like a method
that does not need any access modifiers, name or return value
declaration. It accepts a set of input parameters and returns result.
Lambda expression can be passed as a parameter in a method. So
we can treat code in Lambda expression as data. This piece of code
can be passed to other objects and methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the three main parts

of a Lambda expression in Java?

A

Three main parts of a Lambda expression are:
1. Parameter list: A Lambda expression can have zero or
more parameters. Parameter list is optional to Lambda.
2. Lambda arrow operator: “->” is known as Lambda arrow
operator. It separates the list of parameters and the body of
Lambda.
3. Lambda expression body: The piece of code that we want
to execute is written in Lambda expression body.
E.g. In following example:
Arrays.asList( “a”, “b”, “d” ).forEach( e -> System.out.println( e ) );
Parameter list = e
Arrow = ->
Body = System.out.println( e )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the data type of a

Lambda expression?

A

A Lambda expression fulfills the purpose of passing code as data.
The data type of a Lambda expression is a Functional interface.
In most of the cases this is java.lang.Runnable interface.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the advantages of a

lambda expression?

A

We can pass a lambda expression as an object to a method. This
reduces the overhead involved in passing an anonymous class.
We can also pass a method as a parameter to another method using
lambda expressions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a Functional interface

in Java 8?

A

A Functional interface in Java is an interface that has exactly one
abstract method.
It can have default methods with implementation. A default method
is not abstract.
In Java 8, java.lang.Runnable and java.util.concurrent.Callable are
two very popular Functional interfaces

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can we define a Functional interface in Java 8?

A

To define a Functional interface in Java 8, we can create an
Interface with exactly one abstract method.
Another way is to mark an Interface with annotation
@FunctionalInterface. Even with the annotation we have to follow
the rule of exactly one abstract method.
The only exception to this rule is that if we override
java.lang.Object class’s method as an abstract method, then it does
not count as an abstract method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why do we need Functional

interface in Java?

A

Functional Interfaces are mainly used in Lambda expressions,
Method reference and constructor references.
In functional programming, code can be treated as data. For this
purpose Lambda expressions are introduced. They can be used to
pass a block of code to another method or object.
Functional Interface serves as a data type for Lambda expressions.
Since a Functional interface contains only one abstract method, the
implementation of that method becomes the code that gets passed as
an argument to another method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the main uses of Stream API in Java 8?

A

Main uses of Stream API in Java 8 are:
1. It helps in using data in a declarative way. We can make
use of Database functions like Max, Min etc., without
running a full iteration.
2. It makes good use of multi-core architectures without
worrying about multi-threading code.
3. We can create a pipeline of data operations with Java
Stream that can run in a sequence or in parallel.
4. It provides support for group by, order by etc. operations.
5. It supports writing for code in Functional programming
style.
6. It provides parallel processing of data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the differences
between Intermediate and Terminal
Operations in Java 8 Streams?

A

Main differences between Intermediate and Terminal Stream
operations are as follows:
1. Evaluation: Intermediate operations are not evaluated until
we chain it with a Terminal Operation of Stream. Terminal
Operations can be independently evaluated.
2. Output: The output of Intermediate Operations is another
Stream. The output of Terminal Operations is not a Stream.
3. Lazy: Intermediate Operations are evaluated in lazy
manner. Terminal Operations are evaluated in eager
manner.
4. Chaining: We can chain multiple Intermediate Operations
in a Stream. Terminal Operations cannot be chained
multiple times.
5. Multiple: There can be multiple Intermediate operations in
a Stream operation. There can be only one Terminal
operation in Stream processing statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a Spliterator in Java 8?

A

A Spliterator is a special type of Iterator to traverse and partition
the elements of a source in Java. A source can be a collection, an IO
channel or a generator function.
A Spliterator may traverse elements individually or sequentially in
bulk.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the differences between Iterator and Spliterator in Java 8?

A

Main differences between Iterator and Spliterator are as follows:
1. Spliterator can be used with Streams in Java 8. Where as,
Iterator is just used with Collection.
2. Spliterator uses Internal Iteration to iterate Streams.
Iterator uses External Iteration to iterate Collections.
3. Spliterator can iterate Streams in Parallel as well as
Sequential manner. Iterator only iterates in Sequential
manner.
4. Spliterator can traverse elements individually as well as in
bulk. Iterator only iterates elements individually.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is Type Inference in Java 8?

A

A Java compiler can see each method’s invocation and it
declaration to determine what are type arguments required for
invocation.
By Type Inference, Java can determine the types of the arguments as
well as the type of the result being returned.
Type inference algorithm also tries to find the most specific type
that can work with all types of arguments.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How does Internal Iteration work in Java 8?

A

In an Iterator, the fundamental question is that which party controls
the iteration. Is it Iterator or the Collection on which iterator runs.
When a Collection controls the iterator, then it is called External
Iteration. When the Iterator controls the iteration then it is called
Internal Iteration.
In case of Internal Iteration, the client hands over an operation to
Iterator and the Iterator applies the operation to all the elements in
aggregate.
Internal Iteration is easier to implement, since the Iterator does not
have to store the state of the collection.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the main differences between Internal and External Iterator?

A

Main differences between Internal and External Iterator are as
follows:
1. An Internal Iterator controls the iteration itself. In an
External Iterator collection controls the iteration.
2. Internal Iterator can iterate elements in individually as
well as in
3. Bulk (like forEach). External iterator iterates element one
by one.
4. Internal Iterator does not have to iterate elements only
sequentially. External Iterator always iterates sequentially.
5. Internal Iterator supports declarative programming style
that goes well with functional programming. External
Iterator follows imperative style OOPS programming.
6. Some people consider Internal Iterator code more readable
than that of External Iterator.

17
Q

What are the main advantages
of Internal Iterator over External
Iterator in Java 8?

A

Some of the main advantages of Internal Iterator are:
1. Internal Iterator is based on Functional programming,
therefore it can work on declarative style code.
2. There is no need to sequentially iterate elements in Internal
Iterator.
3. Code is more readable and concise in Internal Iterator.
4. Internal Iterator supports concurrency and parallel
processing

18
Q

What are the applications in
which we should use Internal
Iteration?

A

We need Internal Iterator in applications that require high
performance, parallel processing, fast iteration and bulk operations
support.
Also in Internal Iteration applications, we do not have much control
over iteration. The other features like parallel processing etc.
become more important.

19
Q

What is the main disadvantage of Internal Iteration over External Iteration?

A

Internal Iteration has many advantages over External Iteration. But it
has one big disadvantage. Since Java API is responsible for
iterating in Internal iterator, developer does not get any control over
iteration.

20
Q

Can we provide
implementation of a method in a Java
Interface?

A

Before Java 8, it was not allowed to provide implementation of a
method in an Interface.
Java 8 has introduced the flexibility of providing implementation of
a method in an interface. There are two options for that:
1. Default Method: We can give default implementation of a
method.
2. Static Method: We can create a static method in an
interface and provide implementation

21
Q

What is a Default Method in an Interface?

A

In Java 8, we can provide implementation of a method in an
Interface and mark this method with Default keyword.
In this way, this implementation of the method becomes default
behavior for any class implementing the interface

22
Q

Why do we need Default method in a Java 8 Interface?

A

Default methods in an Interface provide backward compatibility
feature in Java 8.
Let say there is an interface Car that is implemented by BMW,
Chevrolet and Toyota classes. Now a Car needs to add capability
for flying. It will require change in Car interface. Some of the car
classes that do not have flying capability may fail. Therefore a
Default Implementation of flying methods is added in Car interface
so that cars with no flying capability can continue to implement the
original Car interface.

23
Q

What is the purpose of a Static

method in an Interface in Java 8?

A

A Static method in an Interface is utility or helper method. This is
not an object level instance method. Some of the uses of Static
method in an Interface are:
1. Single Class: There is no need to create a separate Utils
class for storing utility or helper methods. We can keep
these methods in same interface.
2. Encapsulation: With Static methods, complete behavior of
a Class is encapsulated in same class. There is no need to
maintain multiple classes.
3. Extension: It is easier to extend a Class/API. If we extend
a collection ArrayList, we get all the methods. We need not
extend Collections class also.

24
Q

What are the main differences
between legacy Date/Time API in Java
and Date/Time API of Java 8?

A

Main difference between legacy Date/Time API and Java 8
Date/Time API are:
1. Old API is not Thread safe. Java 8 API is Thread safe.
2. Old API has many mutable objects. New Java 8 API is
based on Immutable objects.
3. Performance of old API is not good. New Java 8
Date/Time API gives better performance.
4. Old API is less readable and maintainable. New Java 8
API is very well designed and is more readable.
5. Old API has month values from 0 to 11. New API has
months from 1 to 12.

25
Q

What is the new method family
introduced in Java 8 for processing of
Arrays on multi core machines?

A

Java 8 has enhanced the Arrays class with methods that can run
efficiently on multi core machines.
These methods start with keyword parallel.
Egg. Arrays.parallelSetAll(), Arrays.parallelSort() etc.
This parallel set of methods provides parallel processing of Arrays
that can run Java code very fast on a multi core machine.

26
Q

How does Java 8 solve
Diamond problem of Multiple
Inheritance?

A

In Multiple Inheritance if a class extends more than one classes with
two different implementation of same method then it causes
Diamond problem.
Consider following example to see problem and solution for
Diamond problem in Java 8:
public interface BaseInterface{
default void display() { //code goes here }
}
public interface BaseOne extends BaseInterface { }
public interface BaseTwo extends BaseInterface { }
public class ChildClass implements BaseOne, BaseTwo { }
In the above code, class ChildClass gives compile time error. Java
Compiler cannot decide which display method should it invoke in
ChildClass.
To solve this problem, Java SE 8 has given the following remedy:
public interface A{
default void display() { //code goes here }
}
public interface B extends A{ }
public interface C extends A{ }
public class D implements B,C{
default void display() {
B.super.display();
}
}
public interface BaseInterface{
default void display() { //code goes here }
}
public interface BaseOne extends BaseInterface { }
public interface BaseTwo extends BaseInterface { }
public class ChildClass implements BaseOne, BaseTwo {
default void display(){
BaseOne.super.display();
}
}
The method invocation at BaseOne.super.display(); solves the
Diamond problem as it resolves the confusion for compiler.

27
Q

What are the differences
between Predicate, Supplier and
Consumer in Java 8?

A

The subtle difference between Predicate, Supplier and Consumer in Java 8 is as follows:

Predicate is an anonymous function that accepts one argument and returns a result.

Supplier is an anonymous function that accepts no argument and returns a result.

Consumer is an anonymous function that accepts one argument and returns no result

28
Q

How Java 8 supports Multiple Inheritance?

A
In Multiple Inheritance a class can inherit behavior from more than
one parent classes.
Prior to Java 8, a class can implement multiple interfaces but
extend only one class.
In Java 8, we can have method implementation within an interface.
So an interface behaves like an Abstract class.
Now if we implement more than one interface with method
implementation in a class, it means we are inheriting behavior from
multiple abstract classes. That is how we get Multiple Inheritance
in Java 8.
29
Q
In case we create a class that
extends a base class and implements
an interface. If both base class and
interface have a default method with
same name and arguments, then
which definition will be picked by
JVM?
A

In such a scenario, JVM will pick the definition in base class.

30
Q

Can we access a static method of
an interface by using reference of the
interface?

A

No, a static method of interface has to be invoked by using the name
of the interface

31
Q

How can you get the name of
Parameter in Java by using
reflection?

A

Java 8 has introduced a method Parameter.getName() to get the
name of a parameter by using reflection.
Before using this feature, we need to turn on this feature in Java
compiler.
To turn on this feature, just run javac with –parameters argument.
To verify the availability of this feature, we can use Parameter.
isNamePresent() method.

32
Q

What is Optional in Java 8?

A

Optional is a container object that may have a null or non-null
value. If it has a value then isPresent() method returns true.
It a value is present, we can call get() method to get the value. Else
we will get nothing.
It is very useful in handling data that has null values.

33
Q

What are the uses of Optional?

A

We can use Optional to avoid NullPointerException in an
application.
Optional performs Null check at compile time, so we do not get run
time exception for a null value.
Optional reduces the codebase pollution by removing unnecessary
null checks.
Optional can also be used to handle default case for data when a
value is null

34
Q

Which method in Optional
provides the fallback mechanism in
case of null value?

A

In case, an Optional has null value, we can use orElseGet() method
as fallback mechanism. If we implement orElseGet() method, it will
be invoked when the value of Optional is null.

35
Q

What are the popular annotations introduced in Java 8?

A

Some of the popular annotations introduced in Java 8 are:
@FunctionalInterface: This annotation is used to mark an interface
as Functional Interface. As mentioned earlier, A FunctionalInterface
can be used for lambda expressions.
@Repeatable: This annotation is used for marking another
annotation. It indicates that the marked annotation can be applied
multiple times on a type.

36
Q

What is a StringJoiner in Java 8?

A
StringJoiner is a new class in Java 8 that can be used to create a
String. It can construct a sequence of characters separated by a
delimiter. It can also optionally add a prefix and suffix to this
sequence. We can use this sequence to get a String.
37
Q

What are the main differences
between an interface with default
method and an abstract class in Java
8?

A
An interface with a default method appears same as an Abstract
class in Java. But there are subtle differences between two.
1. Instance variable: An interface cannot have instance
variables. An abstract class can have instance variables.
2. Constructor: An interface cannot have a constructor. An
abstract class can have constructor.
3. Concrete Method: An interface cannot have concrete
methods other than default method. An abstract class is
allowed to define concrete methods with implementation.
4. Lambda: An interface with exactly one default method can
be used for lambda expression. An abstract class cannot be
used for lambda expression.