Java How to Program, 11/e Multiple Choice Test Bank
17.12 Q5: Collectors static method groupingBy with one
argument receives a Function that classifies objects in the stream—
the values returned by this function are used as the keys in a Map. The
corresponding values, by default, are ________ containing the stream
elements in a given category.
a. Lists
b. Arrays
c. Strings
d. Collectors
17.12 Q6: Map method ________ performs an operation on each key–
value pair.
a. for
b. forNext
c. forEach
d. forAll
17.12 Q7: Functional interface BiConsumer’s accept method has two
parameters. For Maps, the first represents the ________ and the second
the corresponding ________.
a. key, variable
b. lambda, value
c. lambda, variable
d. key, value
17.12 Q8: Collectors static method groupingBy with two
arguments receives a Function that classifies the objects in the
stream and another Collector (known as the ________ Collector).
a. stream
b. downstream
c. grouping stream
d. upsteam
17.12 Q9: Collectors static method ________ returns a Collector
that counts the number of objects in a given classification, rather than
collecting them into a List.
a. counter
b. count
c. counting
d. enumerate
17.12 Q10: Stream method ________ maps objects to double values
and returns a DoubleStream. The method receives an object that
implements the functional interface ToDoubleFunction (package
java.util.function).
a. doubleMap
b. toDouble
c. mapToDouble
d. toDoubleStream
17.12 Q11: Which of the following statements is false?
a. Interface IntPredicate’s default method and performs a logical
AND operation with short-circuit evaluation between the
IntPredicate on which it’s called and its IntPredicate argument.
b. Interface IntPredicate’s default method invert reverses the
boolean value of the IntPredicate on which it’s called.
c. Interface IntPredicate default method or performs a logical
OR operation with short-circuit evaluation between the
IntPredicate on which it’s called and its IntPredicate argument.
d. You can use the interface IntPredicate default methods to
compose more complex conditions.
17.13 Creating a Stream<String> from a File
17.13 Q1: Which of the following statements is false?
a. Files method lines creates a String containing the lines of text
from a file.
b. Stream method flatMap receives a Function that maps an object
into a stream—e.g., a line of text into words.
c. Pattern method splitAsStream uses a regular expression to
tokenize a String.
d. Collectors method groupingBy with three arguments receives a
classifier, a Map factory and a downstream Collector.
17.13 Q2: Map method entrySet returns a Set of Map.Entry objects
containing the Map’s ________.
a. values
b. keys
c. index
d. key–value pairs
17.14 Streams of Random Values
17.14 Q1: Which of the following statements is false?
a. Class SecureRandom’s methods ints, longs and doubles
(inherited from class Random) return IntStream, LongStream and
DoubleStream, respectively, for streams of random numbers.
b. SecureRandom method ints with no arguments creates an
IntStream for an infinite stream of random int values.
c. An infinite stream is a stream with an unknown number of
elements—you use an intermediate operation to complete processing
on an infinite stream.
d. SecureRandom method ints with a long argument creates an
IntStream with the specified number of random int values.
© Copyright 1992-2016 by Deitel & Associates, Inc. and Pearson Education, Inc.
17.14 Q2: Which of the following statements is false?
a. SecureRandom method ints with two int arguments creates an
IntStream for an infinite stream of random int values in the range
starting with the first argument and up to, but not including, the
second.
b. SecureRandom method ints with a long and two int arguments
creates an IntStream with the specified number of random int
values in the range starting with the first argument and up to, but not
including, the second.
c. To convert an IntStream to a Stream<Integer> call IntStream
method toStream.
d. Function static method identity creates a Function that
simply returns its argument.
17.15 Infinite Streams
17.15 Q1 Which of the following statements is false?
a. Any stream created from a finite data structure will have a finite
number of elements.
b. Eager evaluation makes it possible to work with infinite streams
that represent an unknown, potentially infinite, number of elements.
c. You create infinite streams with the stream-interfaces methods
iterate and generate.
d. All of the above statements are true.
17.16 Lambda Event Handlers
17.16 Q1: Which of the following statements is false?
a. Some event-listener interfaces are functional interfaces.
b. For such interfaces as mentioned in (a), you can implement event
handlers with lambdas.
c. For a simple event handler, a lambda significantly reduces the
amount of code you need to write.
d. All of the above are true.
17.17 Additional Notes on Java SE 8 Interfaces
17.17 Q1: Which of the following statements is false?
a. Functional interfaces must contain only one method and that
method must be abstract.
b. When a class implements an interface with default methods and
does not override them, the class inherits the default methods’
implementations. An interface’s designer can now evolve an interface
by adding new default and static methods without breaking existing
code that implements the interface.
c. If one class inherits the same default method from two interfaces,
the class must override that method; otherwise, the compiler will
generate a compilation error.
d. You can create your own functional interfaces by ensuring that each
contains only one abstract method and zero or more default or
static methods.
17.17 Q2: You can declare that an interface is a functional interface by
preceding it with the @FunctionalInterface annotation. The
compiler will then ensure that the interface contains ________;
otherwise, it’ll generate a compilation error.
a. no abstract methods
b. all abstract methods
c. only one abstract method
Java How to Program, 11/e Multiple Choice Test Bank
d. one or more abstract methods.
17.2.2 Lambda Expressions
17.2.2 Q1: A lambda expression represents a(n) ________ method—a
shorthand notation for implementing a functional interface.
a. functional
b. unnamed
c. undesignated
d. anonymous
17.2.2 Q2: The type of a lambda is the type of the ________ that the
lambda implements.
a. anonymous class
b. functional interface
c. stream
d. collection
17.2.2 Q3: Which of the following statements is false?
a. Lambda expressions can be used anywhere functional interfaces are
expected.
b. A lambda consists of a parameter list followed by the arrow token
and a body, as in:
[parameterList] –> {statements}
c.Method references are specialized shorthand forms of lambdas.
d. Each of the above statements is true.
17.2.2 Q4: Which of the following statements is false?
a. A lambda that receives two ints, x and y, and returns their sum is
(int x, int y) –> {return x + y;}
b. A lambda’s parameter types may be omitted, as in:
(x, y) –> {return x + y;}
in which case, the parameter and return types are set to the lambda’s
default type.
c. A lambda with a one-expression body can be written as:
(x, y) –> x + y
In this case, the expression’s value is implicitly returned.
d. When a lambda’s parameter list contains only one parameter, the
parentheses may be omitted, as in:
value –> System.out.printf(“%d “, value)
17.2.2 Q5: What is the meaning of ( ) in the following lambda?
() –> System.out.println(“Welcome to lambdas!”)
a. the lambdas parameters are inferred
Java How to Program, 11/e Multiple Choice Test Bank
b. the lambdas parameters are supplied by a method reference
c. the lambda has an empty parameter list
d. the given expression is not a valid lambda
17.2.3 Streams
17.2.3 Q1: Which of the following statements is false?
a. Streams are objects that implement interface Stream (from the
package java.util.stream) and enable you to perform functional
programming tasks.
b. Streams move elements through a sequence of processing steps—
known as a stream pipeline—that begins with a data source, performs
various intermediate operations on the data source’s elements and
ends with a terminal operation.
c. A stream pipeline is formed by chaining method calls.
d. An advantage of streams over collections is that streams have their
own storage, so once a stream is processed, it can be reused, because
it maintains a copy of the original data source.
17.2.3 Q2: Intermediate operations are________; they aren’t performed
until a terminal operation is invoked. This allows library developers
to optimize stream-processing performance.
a. eager
b. idle
c. lazy
d. inactive
17.2.3 Q3:Terminal operations are ________; they perform the
requested operation when they are called.
a. immediate
Java How to Program, 11/e Multiple Choice Test Bank
b. idle
c. lazy
d. eager
17.2.3 Q4: The intermediate Stream operation ________ results in a
stream containing only the elements that satisfy a condition.
a. distinct
b. map
c. filter
d. limit
17.2.3 Q5: The intermediate Stream operation ________ results in a
stream containing only the unique elements.
a. distinct
b. map
c. filter
d. limit
17.2.3 Q6: Intermediate Stream operation ________ results in a stream
with the specified number of elements from the beginning of the
original stream.
a. distinct
b. map
c. filter
d. limit
17.2.3 Q7: Intermediate Stream operation ________ results in a stream
in which each element of the original stream is mapped to a new value
(possibly of a different type)—e.g., mapping numeric values to the
squares of the numeric values. The new stream has the same number
of elements as the original stream.
a. mapped
b. map
c. mapper
Java How to Program, 11/e Multiple Choice Test Bank
d. mapping
17.2.3 Q8: Terminal Stream operation ________ performs
processing on every element in a stream (e.g., display each element).
a. forNext
b. for
c. forAll
d. forEach
17.2.3 Q9: Stream reduction operation ________ uses the elements of a
collection to produce a single value using an associative accumulation
function (e.g., a lambda that adds two elements).
a. reduce
b. condense
c. combine
d. associate
17.2.3 Q10: Stream mutable reduction operation ________ creates a new
collection of elements containing the results of the stream’s prior
operations.
a. combine
b. accumulate
c. gather
d. collect
17.2.3 Q11: Stream mutable reduction operation ________creates an
array containing the results of the stream’s prior operations.
a. array
b. createArray
c. toArray
d. reduceArray