Java How to Program, 11/e Multiple Choice Test Bank
Chapter 17: Lambdas
17.1 Introduction
17.1 Q1: Prior to Java SE 8, Java supported three programming
paradigms. Java SE 8 added ________.
a. procedural programming
b. object-oriented programming
c. generic programming
d. functional programming.
17.2 Streams and Reductions
17.2 Q1: Which of the following statements is false?
a. With external iteration you specify all the iteration details.
b. Every time you write code that modifies a variable, it’s possible to
introduce an error into your code.
c. Letting the library determine how to iterate over a collection of
elements is known as internal iteration.
d. All of the above are true.
17.2 Q2: An ________ (package java.util.stream) is a stream that
manipulates int values.
a. StreamOfInt
b. IStream.
c. IntegerStream
d. IntStream
17.2 Q3: Which of the following statements is false?
a. IntStream methods range and rangeClosed each produce an
ordered sequence of int values.
b. IntStream methods range and rangeClosed take two int
arguments representing the range of values.
Java How to Program, 11/e Multiple Choice Test Bank
c. Method range produces a sequence of values from its first
argument up to its second argument.
d. Method rangeClosed produces a sequence of values including
both of its arguments.
17.3 Mapping and Lambdas
17.3 Q1: ________ is an intermediate operation that transforms a
stream’s elements to new values and produces a stream containing
the resulting (possibly different type) elements.
a. Transforming
b. Converting
c. Mapping
d. Translating
17.4 Filtering
17.4 Q5: Which of the following statements is false?
a. You filter elements to produce a stream of intermediate results that
match a predicate.
b. IntStream method filter receives an object that method that
takes one parameter and returns a boolean result.
c. If the result of calling filter‘s argument is true for a given element,
that element is included in the resulting stream.
d. All of the above statements are true.
17.5 How Elements Move Through Stream Pipelines
17.5 Q1: Which of the following statements is false?
a. Each intermediate operation results in a new stream.
b. Each new stream is simply an object representing the processing
steps that have been specified to that point in the pipeline.
Java How to Program, 11/e Multiple Choice Test Bank
c. Chaining intermediate-operation method calls adds to the set of
processing steps to perform on each stream element. The last stream
object in the stream pipeline contains all the processing steps to
perform on each stream element.
d. When you initiate a stream pipeline with a terminal operation, the
each processing step is applied for to all the stream elements before
the next processing step is applied.
17.6 Method References
17.6 Q1: IntStream method ________ (a terminal operation) performs
a task on each stream element.
a. for
b. forEach
c. forElement
d. None of the above.
17.6 Q2: Which of the following statements is false?
a. A method reference of the form objectName::instanceMethodName
is a bound instance method reference.
b. A method reference of the form ClassName::staticMethodName is a
static method reference.
c. Collectors method joining is a collector that creates a
concatenated String representation of the stream’s elements,
appending each element to the String separated from the previous
element by the joining method’s argument.
d. All of the above statements are true.
17.7 IntStream Operations
17.7 Q1: Class IntStream provides terminal operations for common
stream ________—count returns the number of elements, min returns
the smallest int, max returns the largest int, sum returns the sum of
all the ints and average returns an OptionalDouble (package
java.util) containing the average of the ints as a value of type
double.
a. consolidations
b. deductions
c. reductions
d. trims
17.7 Q2: IntStream method ________ performs the count, min, max,
sum and average operations in one pass of an IntStream’s elements
and returns the results as an IntSummaryStatistics object
(package java.util).
a. allStatistics.
b. completeStatistics.
c. entireStatistics.
d. summaryStatistics
17.7 Q3: You can define your own reductions for an IntStream by
calling its ________ method. The first argument is a value that helps you
begin the reduction operation and the second argument is an object
that implements the IntBinaryOperator functional interface.
a. reduction.
b. lessen
c. trim
d. reduce
17.7 Q4: Method reduce’s first argument is formally called a(n)
________ value—a value that, when combined with any stream element
using the IntBinaryOperator produces that element’s original
value.
a. original
Java How to Program, 11/e Multiple Choice Test Bank
b. identity
c. preserve
d. self
17.8 Functional Interfaces
17.8 Q1: Which of the following statements is false?
a. Functional interfaces are also known as single abstract method
(SAM) interfaces.
b. Package java.util.function contains six basic functional
interfaces BinaryOperator, Consumer, Function, Predicate,
Supplier and UnaryOperator.
c. There are many specialized versions of the six basic functional
interfaces for use with int, long and double primitive values. There
are also generic customizations of Consumer, Function and
Predicate for binary operations—that is, methods that take two
arguments.
d. All of these statements are true.
17.8 Q2: The basic generic functional interface ________ in package
java.util.function contains method apply that takes two T
arguments, performs an operation on them (such as a calculation) and
returns a value of type T.
a. Consumer<T>
b. Function<T,R>
c. Supplier<T>
d. BinaryOperator<T>
17.8 Q3: The basic generic functional interface ________ in package
java.util.function contains method accept that takes a T
argument and returns void. Performs a task with its T argument, such
as outputting the object, invoking a method of the object, etc.
a. Consumer<T>
b. Function<T,R>
c. Supplier<T>
Java How to Program, 11/e Multiple Choice Test Bank
d. BinaryOperator<T>
17.8 Q4: The basic generic functional interface ________ in package
java.util.function contains method apply that takes a T
argument and returns a value of type R. Calls a method on the T
argument and returns that method’s result.
a. Consumer<T>
b. Function<T,R>
c. Supplier<T>
d. BinaryOperator<T>
17.8 Q5: The basic generic functional interface ________ in package
java.util.function contains method test that takes a T
argument and returns a boolean. Tests whether the T argument
satisfies a condition.
a. Consumer<T>
b. Function<T,R>
c. Supplier<T>
d. Predicate<T>
17.8 Q6: The basic generic functional interface ________ in package
java.util.function contains method get that takes no arguments
and produces a value of type T. Often used to create a collection object
in which a stream operation’s results are placed.
a. Consumer<T>
b. Function<T,R>
c. Supplier<T>
d. BinaryOperator<T>
17.8 Q7: The basic generic functional interface ________ in package
java.util.function contains method get that takes no arguments
and returns a value of type T.
a. UnaryOperator<T>
b. Function<T,R>
Java How to Program, 11/e Multiple Choice Test Bank
c. Supplier<T>
d. BinaryOperator<T>
Error! Reference source not found.
17.9 Q1: Which of the following statements is false?
a. The parameter names and variable names that you use in lambdas
cannot be the same as any other local variables in the lambda’s lexical
scope; otherwise, a compilation error occurs.
b. Lambdas may use only final local variables.
c. A lambda that refers to a local variable in the enclosing lexical scope
is known as a capturing lambda.
d. All of the above statements are true.
17.10 Stream<Integer> Manipulations
17.10 Q1: Class Array’s ________ method is used to create a Stream
from an array of objects.
a. stream
b. arrayToStream
c. createStream
d. objectToStream
17.10 Q2: Interface Stream (package java.util.stream) is a
generic interface for performing stream operations on objects. The
types of objects that are processed are determined by the Stream’s
________.
a. root
b. origin
c. source
d. start
17.10 Q3: Class Arrays provides ________ stream methods for creating
IntStreams, LongStreams and DoubleStreams from int, long and
double arrays or from ranges of elements in the arrays.
a. virtual
b. package
c. overridden
d. overloaded
17.10 Q4: Which of the following statements is false?
a. Stream method sort orders a stream’s elements into ascending
order by default.
b. To create a collection containing a stream pipeline’s results, you can
use Stream method collect (a terminal operation). As the stream
pipeline is processed, method collect performs a mutable reduction
operation that places the results into an object, such as a List, Map or
Set.
c. Method collect with one argument receives an object that
implements interface Collector (package java.util.stream),
which specifies how to perform a mutable reduction.
d. Class Collectors (package java.util.stream) provides static
methods that return predefined Collector implementations.
17.10 Q5: Which of the following statements is true?
a. Stream method filter receives a Predicate and results in a stream
of objects that match the Predicate.
b. Predicate method test returns a boolean indicating whether
the argument satisfies a condition.
c. Interface Predicate also has methods and, negate and or.
d. Each of these statements is true.
17.11 Stream<String> Manipulations
17.11 Q1: Which of the following statements is false?
a. Stream method map maps each element to a new value and
produces a new stream with the same number of elements as the
original stream.
b. A reference is a shorthand notation for a lambda expression.
c. ClassName::instanceMethodName represents a method reference for
an instance method of a class. Creates a one-parameter lambda that
invokes the instance method on the lambda’s argument and returns
the method’s result.
d. ClassName::new represents a constructor reference. Creates a
lambda that invokes the no-argument constructor of the specified
class to create and initialize a new object of that class.
17.11 Q2: By default, method sorted uses ________.
a. ascending order
b. the natural order for the stream’s element type
c. descending order
d. the order specified in a command-line argument
17.11 Q3: ________ is a method reference for an instance method of a
class. It creates a one-parameter lambda that invokes the instance
method on the lambda’s argument and returns the method’s result.
a. Math::sqrt
b. System.out::println
c. TreeMap::new
d. String::toUpperCase
17.11 Q4: ________ is a method reference for an instance method that
should be called on a specific object. It creates a one-parameter
lambda that invokes the instance method on the specified object—
passing the lambda’s argument to the instance method—and returns
the method’s result.
a. Math::sqrt
Java How to Program, 11/e Multiple Choice Test Bank
b. System.out::println
c. TreeMap::new
d. String::toUpperCase
17.11 Q5: ________ is s method reference for a static method of a
class. It creates a one-parameter lambda in which the lambda’s
argument is passed to the specified a static method and the lambda
returns the method’s result.
a. Math::sqrt
b. System.out::println
c. TreeMap::new
d. String::toUpperCase
17.11 Q6: ________ is a constructor reference. It creates a lambda that
invokes the no-argument constructor of the specified class to create
and initialize a new object of that class.
a. Math::sqrt
b. System.out::println
c. TreeMap::new
d. String::toUpperCase
17.11 Q7: Functional interface Comparator’s default method
________ reverses an existing Comparator’s ordering.
a. invert
b. descending
c. reversed
d. downward
17.12 Stream<Employee> Manipulations
17.12 Q1: A nice performance feature of lazy evaluation is the ability
to perform________ evaluation, that is, to stop processing the stream
pipeline as soon as the desired result is available.
a. premature
Java How to Program, 11/e Multiple Choice Test Bank
b. short circuit
c. terminal
d. intermediate
17.12 Q2: Stream method findFirst is a short-circuiting terminal
operation that processes the stream pipeline and terminates
processing as soon as the first object from the stream pipeline is
found. The method returns a(n) ________ containing the object that was
found, if any.
a. Optional
b. Discretionary
c. Elective
d. Extra
17.12 Q3: Which statement in the following sequence of statements
about sorting objects by two fields is false?
a. To sort objects by two fields, you create a Comparator that uses
two Functions.
b. First you call Comparator method comparing to create a
Comparator with the first Function.
c. On the Comparator for the first field, you call method comparing
with the second Function.
d. The resulting Comparator compares objects using the first
Function then, for objects that are equal, compares them by the
second Function.
17.12 Q4: Stream method ________ eliminates duplicate objects in a
stream.
a. distinct
b. discrete
c. unique
d. different