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