Java 8 Flashcards

1
Q

What are some of the new features released in Java 8?

A

Lambda expression, stream API, Date & Time API, Functional Interface, Optional

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

What is a lambda expression?

A

An anonymous function, like a method that does not need any access modifiers, name or return value declaration.

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

What are the three main parts of a Lambda expression?

A

Parameter list: Can be zero or more parameters.
Lambda arrow operator: it separates the list of parameters and the body of the Lambda.
Lambda expression body: the piece of code that we want to execute is written in the expression body.

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

What is the data type of a lambda expression?

A

The data type of a lambda expression is a functional interface.

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

What is a Functional Interface?

A

An interface that has exactly one abstract method. It can have default methods with implementation.

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

What are two popular functional interfaces?

A

java.lang.Runnable and java.util.Callable

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

What is a Single Abstract Method (SAM) interface in Java 8?

A

A functional interface is also known as a Single Abstract Method Interface, since it has only 1 abstract method.

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

How to create a functional interface?

A

Create an interface with exactly one abstract method. Another way is to mark an interface with @FunctionalInterface, this must follow the same rule.

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

What is the need of functional interfaces?

A

They are mainly used in lambda expressions, method reference and constructor references. In functional programming, code is treated as data, so lambda expressions can be used to pass a block of code to another method or object.

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?

A

It helps in using data in a declarative way. It makes good use of multi-core architectures. We can create a pipeline of data operations with Stream. It supports writing for code in functional programming styles.

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

What is the differences between Intermediate and Terminal Operations in Streams?

A

Intermediate operations are not evaluated until we chain it with a Terminal Operation. The output of Intermediate Operations is another Stream, the output of Terminal Operations is not a stream. We can chain multiple intermediate operations in a stream, but terminal operations cannot be chained multiple times. There can be multiple intermediate operations in a stream, there can only be one terminal operation in a stream.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
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. This implementation of the method becomes default behaviour for any class implementing the interface.

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

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
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() returns true. It is very useful in handling data that has null values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
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.

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

What is a Stream?

A

A Stream is a sequence of elements, it is a class to support functional style programming operations on a stream of elements.

17
Q

What are the two conditions for a Lambda function to be used in a stream?

A

Non-interfering: the lambda function should not be interfering. It should not modify the underlying data.
Stateless: the lambda function should not refer to any state changes outside of the scope of the function.

18
Q

What is the use of anyMatch() in stream?

A

When we use anyMatch() the execution on Stream stops as soon as the first element with anyMatch() condition is found.

19
Q

What is the use of collect() in Stream?

A

The collect() is a terminal operation. We can use collect() to convert the elements of a Stream to a Collection. Collectors.toList() or Collectors.toSet().