Chapter 10 Streams Flashcards

1
Q

Can I create a record inside a Java class? If false, why?

A

True.

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

When Optional is empty the method get returns:

A

Throws exception.

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

When Optional is empty the method ifPresent(Consumer c) returns:

A

Does nothing.

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

When Optional is empty the method isPresent() returns:

A

Returns false.

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

When Optional is empty the method orElse(T other) returns:

A

Returns other parameter.

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

When Optional is empty the method orElseGet(Supplier s) returns:

A

Returns result of calling Supplier.

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

When Optional is empty the method orElseThrow() returns:

A

Throws NoSuchElementException.

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

When Optional is empty the method orElseThrow(Suppliers) returns:

A

Throws exception created by calling Supplier.

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

Intermediate operations are executed upon method call. If false, why?

A

False – Intermediate operations are lazy and only executed when a terminal operation is invoked.

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

Terminal operations can exist multiple times in a pipeline. If false, why?

A

False – Only one terminal operation is allowed per pipeline; it triggers stream evaluation.

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

The return type of intermediate operations is the stream type.

A

True

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

A stream remains valid after a terminal operation is called.

A

False – Once a terminal operation is executed, the stream is consumed and no longer valid.

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

A terminal operation is required for a pipeline to be useful.

A

True

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

True or False: opt.orElseThrow(() -> throw new Exception()); will compile correctly.

A

False: It will not compile because orElseThrow expects a lambda returning an exception, not one that throws an exception.

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

True or False: orElseThrow() in Java 17 can only accept method references as arguments.

A

False: It can accept both method references and lambda expressions that return exceptions.

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

True or False: Removing the throw from () -> throw new Exception() will make the code compile.

A

True: The lambda should return an exception (() -> new Exception()) instead of throwing it.

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

True or False: The correct lambda form for orElseThrow is () -> new Exception().

A

True: This lambda returns an exception, meeting orElseThrow’s requirement.

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

True or False: orElseThrow is new to Java 17.

A

False: orElseThrow has been available since Java 10 as part of Optional.

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

True or False: Stream.empty() creates a finite stream with zero elements.

A

True

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

True or False: Stream.of(varargs) creates an infinite stream.

A

False: It creates a finite stream with the specified elements.

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

True or False: coll.parallelStream() is infinite.

A

False: coll.parallelStream() creates a finite stream from the collection but processes it in parallel.

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

True or False: Stream.generate(supplier) creates an infinite stream.

A

True: It generates an infinite stream by calling the supplier for each element.

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

True or False: Stream.iterate(seed, predicate, unaryOperator) always creates an infinite stream.

A

False: It stops when the predicate returns false, so it can be finite.

24
Q

The Optional.ofNullable(value) method is a factory method that either wraps a value or returns an empty Optional.

25
Q

Optional.empty() contains a default value.

A

(It contains no value at all.)

26
Q

Calling get() on an empty Optional returns null.

A

(It throws a NoSuchElementException.)

27
Q

ifPresent(Consumer c) runs the Consumer even if the Optional is empty.

A

(It does nothing when empty.)

28
Q

isPresent() returns true for an empty Optional.

A

(It returns false.)

29
Q

orElse(T other) throws an exception when the Optional is empty.

A

(It returns the provided default value.)

30
Q

orElseThrow() returns null when the Optional is empty.

A

(It throws a NoSuchElementException.)

31
Q

orElseGet(Supplier s) behaves identically to orElse(T other).

A

(It calls the Supplier function only if empty.)

32
Q

Optional.of(value) allows null values.

A

(It throws NullPointerException if value is null.)

33
Q

The ternary operator is required to handle null values with Optional.

A

(Optional.ofNullable(value) does this automatically.)

34
Q

The orElseGet(() -> Math.random()) method returns the same value every time it is called.

A

False (It calls the Supplier each time, generating a new random value.)

35
Q

The orElseThrow() method, when used without a Supplier, throws a NullPointerException.

A

False (It throws a NoSuchElementException by default.)

36
Q

The orElse(Double.NaN) method requires a Supplier to provide a default value.

A

False (It takes a direct value, not a Supplier.)

37
Q

Returning null instead of an Optional provides a clearer API.

A

False (Returning Optional is clearer because it explicitly represents the possibility of absence.)

38
Q

Optional cannot be used in functional programming styles.

A

False (Optional supports functional programming with methods like ifPresent().)

39
Q

Intermediate operations in a stream execute immediately when called.

A

False (They use lazy evaluation and execute only when a terminal operation runs.)

40
Q

A stream can be reused after executing a terminal operation.

A

False (A stream is consumed after a terminal operation and cannot be reused.)

41
Q

A terminal operation is optional in a stream pipeline.

A

False (A terminal operation is required to produce a result.)

42
Q

An intermediate operation in a stream pipeline can exist only once.

A

False (Multiple intermediate operations can be used in a pipeline.)

43
Q

A terminal operation in a stream pipeline can be executed multiple times on the same stream.

A

False (Once a terminal operation is executed, the stream is no longer valid.)

44
Q

A terminal operation always returns another stream.

A

False (Terminal operations return a non-stream result, such as a collection or a primitive value.)

45
Q

A stream is a sequence of data that follows a pipeline model.

46
Q

The orElseThrow() method can be customized to throw a specific exception.

47
Q

The return type of an intermediate operation is always a non-stream type.

A

False (Intermediate operations return another stream.)

48
Q

What is the purpose of the peek method in Java Streams?

A

The peek method allows performing an action on each element of a stream as it is consumed, primarily for debugging purposes.

49
Q

Is peek an intermediate or terminal operation in Java Streams?

A

peek is an intermediate operation, meaning it returns a new stream and does not trigger execution.

50
Q

When using peek in a parallel stream, what must be considered regarding shared state?

A

In parallel streams, the action in peek may be executed in different threads, so proper synchronization is required if modifying shared state.

51
Q

Can peek be used to modify stream elements?

A

No, peek should not be used for modifying elements. It is meant for observation, and modifying elements may lead to unpredictable behavior.

51
Q

What type of argument does the peek() method expect?

A

The peek() method expects a Consumer’<’T’>’ object as an argument.

52
Q

What happens if you call peek() without an argument?

A

Calling peek() without an argument will fail to compile because it requires a Consumer’<’T’>’ as a parameter.

53
Q

How can you use peek() correctly in a stream?

A

You can use peek() with a lambda expression or method reference, e.g.,

54
Q

What does the allMatch(Predicate<? super T> predicate) method do?

A

It checks whether all elements in a stream match the given predicate. If all elements satisfy the condition, it returns true; otherwise, it returns false.

55
Q

What happens if the stream is empty when calling allMatch(predicate)?

A

It returns true, and the predicate is not evaluated.

56
Q

Why is allMatch(predicate) considered a short-circuiting terminal operation?

A

Because it may not evaluate the predicate on all elements if it determines the result early, stopping as soon as it finds an element that does not match.