Java 8 Flashcards

1
Q

What New Features Were Added in Java 8?

A

Key features include Lambda Expressions, the Stream API, Optional, Method References, Default Methods in Interfaces, and the new Date-Time API.

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

What Is a Method Reference?

A

A method reference is a shorthand notation of a lambda expression that executes just one method. It’s denoted by ClassName::methodName.

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

What Is Optional? How Can It Be Used?
Optional is a container object that may or may not contain a non-null value. It helps avoid NullPointerException and can be used with methods like isPresent(), ifPresent(), or orElse().

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

Describe some of the Functional Interfaces in the Standard Library.

A

Some examples are Predicate, Function, Supplier, and Consumer. Functional interfaces are those that have a single abstract method and can be used with lambda expressions.

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

How are we using immutability in Java 8?

A

Immutability is promoted with functional programming, such as avoiding side effects in lambda expressions and using final or effectively final variables.

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

What Is a Functional Interface? What Are the Rules of Defining a Functional Interface?

A

A functional interface contains exactly one abstract method. You can annotate it with @FunctionalInterface, though this is not mandatory.

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

What Is a Default Method and When Do We Use It?

A

A default method is a method in an interface that has an implementation. It allows adding new methods to interfaces without breaking the existing implementations.

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

What Is a Lambda Expression and What Is It Used for?

A

Lambda expressions are anonymous functions that simplify the syntax for defining and using methods or instances of functional interfaces.

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

What Is a Stream? How Does It Differ From a Collection?

A

A stream is a sequence of data elements that supports various operations. It differs from a collection because it does not store elements, is lazily executed, and focuses on pipeline processing.

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

What Is the Difference Between Intermediate and Terminal Operations?

A

Intermediate operations return a new stream (e.g., filter(), map()) and are lazy, while terminal operations (e.g., forEach(), collect()) trigger the stream processing.

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

What Is the Difference Between Map and flatMap Stream Operation?

A

map() transforms each element in a stream, while flatMap() flattens multiple levels of nested streams into a single level.

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

What Is Stream Pipelining in Java 8?

A

Stream pipelining refers to chaining multiple operations together in a single pipeline, enabling efficient and lazy processing.

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

Tell Us About the New Date and Time API in Java 8.

A

Java 8 introduced the java.time package, which provides immutable and thread-safe classes like LocalDate, LocalTime, ZonedDateTime, and more for better handling of dates and times.

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

What is Type Inference?

A

Type inference allows the compiler to automatically determine the type of an expression, reducing the need for explicit type declarations.

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

What is Java 8 StringJoiner class used for?

A

StringJoiner is used to concatenate strings with a specified delimiter, and optionally, a prefix and suffix.

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

What is a Spliterator?

A

Spliterator is an interface introduced in Java 8 to traverse and partition elements of a source like a Stream, Set, or List. It supports parallelism.

17
Q

Explain the difference between predicate and function.

A

Predicate<T> is a functional interface that represents a boolean-valued function of one argument, while Function<T, R> represents a function that takes an argument of type T and returns a result of type R.</T>