Chapter 10 Streams Flashcards
Can I create a record inside a Java class? If false, why?
True.
When Optional is empty the method get returns:
Throws exception.
When Optional is empty the method ifPresent(Consumer c) returns:
Does nothing.
When Optional is empty the method isPresent() returns:
Returns false.
When Optional is empty the method orElse(T other) returns:
Returns other parameter.
When Optional is empty the method orElseGet(Supplier s) returns:
Returns result of calling Supplier.
When Optional is empty the method orElseThrow() returns:
Throws NoSuchElementException.
When Optional is empty the method orElseThrow(Suppliers) returns:
Throws exception created by calling Supplier.
Intermediate operations are executed upon method call. If false, why?
False – Intermediate operations are lazy and only executed when a terminal operation is invoked.
Terminal operations can exist multiple times in a pipeline. If false, why?
False – Only one terminal operation is allowed per pipeline; it triggers stream evaluation.
The return type of intermediate operations is the stream type.
True
A stream remains valid after a terminal operation is called.
False – Once a terminal operation is executed, the stream is consumed and no longer valid.
A terminal operation is required for a pipeline to be useful.
True
True or False: opt.orElseThrow(() -> throw new Exception()); will compile correctly.
False: It will not compile because orElseThrow expects a lambda returning an exception, not one that throws an exception.
True or False: orElseThrow() in Java 17 can only accept method references as arguments.
False: It can accept both method references and lambda expressions that return exceptions.
True or False: Removing the throw from () -> throw new Exception() will make the code compile.
True: The lambda should return an exception (() -> new Exception()) instead of throwing it.
True or False: The correct lambda form for orElseThrow is () -> new Exception().
True: This lambda returns an exception, meeting orElseThrow’s requirement.
True or False: orElseThrow is new to Java 17.
False: orElseThrow has been available since Java 10 as part of Optional.
True or False: Stream.empty() creates a finite stream with zero elements.
True
True or False: Stream.of(varargs) creates an infinite stream.
False: It creates a finite stream with the specified elements.
True or False: coll.parallelStream() is infinite.
False: coll.parallelStream() creates a finite stream from the collection but processes it in parallel.
True or False: Stream.generate(supplier) creates an infinite stream.
True: It generates an infinite stream by calling the supplier for each element.
True or False: Stream.iterate(seed, predicate, unaryOperator) always creates an infinite stream.
False: It stops when the predicate returns false, so it can be finite.
The Optional.ofNullable(value) method is a factory method that either wraps a value or returns an empty Optional.
True
Optional.empty() contains a default value.
(It contains no value at all.)
Calling get() on an empty Optional returns null.
(It throws a NoSuchElementException.)
ifPresent(Consumer c) runs the Consumer even if the Optional is empty.
(It does nothing when empty.)
isPresent() returns true for an empty Optional.
(It returns false.)
orElse(T other) throws an exception when the Optional is empty.
(It returns the provided default value.)
orElseThrow() returns null when the Optional is empty.
(It throws a NoSuchElementException.)
orElseGet(Supplier s) behaves identically to orElse(T other).
(It calls the Supplier function only if empty.)
Optional.of(value) allows null values.
(It throws NullPointerException if value is null.)
The ternary operator is required to handle null values with Optional.
(Optional.ofNullable(value) does this automatically.)
The orElseGet(() -> Math.random()) method returns the same value every time it is called.
False (It calls the Supplier each time, generating a new random value.)
The orElseThrow() method, when used without a Supplier, throws a NullPointerException.
False (It throws a NoSuchElementException by default.)
The orElse(Double.NaN) method requires a Supplier to provide a default value.
False (It takes a direct value, not a Supplier.)
Returning null instead of an Optional provides a clearer API.
False (Returning Optional is clearer because it explicitly represents the possibility of absence.)
Optional cannot be used in functional programming styles.
False (Optional supports functional programming with methods like ifPresent().)
Intermediate operations in a stream execute immediately when called.
False (They use lazy evaluation and execute only when a terminal operation runs.)
A stream can be reused after executing a terminal operation.
False (A stream is consumed after a terminal operation and cannot be reused.)
A terminal operation is optional in a stream pipeline.
False (A terminal operation is required to produce a result.)
An intermediate operation in a stream pipeline can exist only once.
False (Multiple intermediate operations can be used in a pipeline.)
A terminal operation in a stream pipeline can be executed multiple times on the same stream.
False (Once a terminal operation is executed, the stream is no longer valid.)
A terminal operation always returns another stream.
False (Terminal operations return a non-stream result, such as a collection or a primitive value.)
A stream is a sequence of data that follows a pipeline model.
True
The orElseThrow() method can be customized to throw a specific exception.
True
The return type of an intermediate operation is always a non-stream type.
False (Intermediate operations return another stream.)
What is the purpose of the peek method in Java Streams?
The peek method allows performing an action on each element of a stream as it is consumed, primarily for debugging purposes.
Is peek an intermediate or terminal operation in Java Streams?
peek is an intermediate operation, meaning it returns a new stream and does not trigger execution.
When using peek in a parallel stream, what must be considered regarding shared state?
In parallel streams, the action in peek may be executed in different threads, so proper synchronization is required if modifying shared state.
Can peek be used to modify stream elements?
No, peek should not be used for modifying elements. It is meant for observation, and modifying elements may lead to unpredictable behavior.
What type of argument does the peek() method expect?
The peek() method expects a Consumer’<’T’>’ object as an argument.
What happens if you call peek() without an argument?
Calling peek() without an argument will fail to compile because it requires a Consumer’<’T’>’ as a parameter.
How can you use peek() correctly in a stream?
You can use peek() with a lambda expression or method reference, e.g.,
What does the allMatch(Predicate<? super T> predicate) method do?
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.
What happens if the stream is empty when calling allMatch(predicate)?
It returns true, and the predicate is not evaluated.
Why is allMatch(predicate) considered a short-circuiting terminal operation?
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.