Programming Languages Chapter 17 Java supported three programming paradigms

subject Type Homework Help
subject Pages 11
subject Words 4128
subject Authors Harvey Deitel, Paul Deitel

Unlock document.

This document is partially blurred.
Unlock all pages and 1 million more documents.
Get Access
page-pf1
Chapter 17: Lambdas
17.1 Introduction
17.1 Q1: Prior to Java SE 8, Java supported three programming paradigms. Java SE 8
adds ________.
a. procedural programming
b. object-oriented programming
c. generic programming
d. functional programming.
17.1 Q2: The new language and library capabilities that support functional programming
were added to Java as part of Project ________.
a. Utilitarian
b. Upsilon
c. Lambda
d. Utility
17.2 Functional Programming Technologies
Overview
17.2 Q1: Which of the following statements is false?
a. Prior to functional programming, you typically determined what you wanted to
accomplish, then specified the precise steps to accomplish that task.
b. Using a loop to iterate over a collection of elements is known as external iteration and
requires accessing the elements sequentially. Such iteration also requires mutable
variables. External iteration is easier to parallelize.
c. Letting the library determine how to iterate over a collection of elements is known as
internal iteration.
d. Functional programming focuses on immutabilitynot modifying the data source
being processed or any other program state.
17.2.1 Functional Interfaces
17.2.1 Q1: Which of the following statements is false?
a. Functional interfaces are also known as single abstract method (SAM) interfaces.
page-pf2
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 operationsthat is, methods that take
two arguments.
d. All of these statements are true.
17.2.1 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.2.1 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>
d. BinaryOperator<T>
17.2.1 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.2.1 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>
page-pf3
17.2.1 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.2.1 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>
c. Supplier<T>
d. BinaryOperator<T>
17.2.2 Lambda Expressions
17.2.2 Q1: A lambda expression represents a(n) ________ methoda 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.
page-pf4
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
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 stepsknown as a stream
pipelinethat 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.
page-pf5
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 eager; they perform the requested operation when they
are called.
a. immediate
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
page-pf6
c. mapper
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
17.3 IntStream Operations
17.3 Q1: An ________ (package java.util.stream) is a specialized stream for
manipulating int values.
a. StreamOfInt
b. IStream.
c. IntegerStream
d. IntStream
page-pf7
17.3.1 Creating an IntStream and Displaying Its Values with
the forEach Terminal Operation
17.3.1 Q1: Which of the following statements is false?
a. A lambda can use the outer class’s this reference without qualifying it with the outer
class’s name.
b. 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.
c. Lambdas may use only final local variables.
d. A lambda that refers to a local variable in the enclosing lexical scope is known as a
capturing lambda.
17.3.2 Terminal Operations count, min, max, sum and average
17.3.2 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.3.2 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.3.3 Terminal Operation reduce
17.3.3 Q1: 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.
page-pf8
b. lessen
c. trim
d. reduce
17.3.3 Q2: Method reduce’s first argument is formally called a(n) ________ valuea
value that, when combined with any stream element using the IntBinaryOperator
produces that element’s original value.
a. original
b. identity
c. preserve
d. self
17.3.4 Intermediate Operations: Filtering and Sorting
17.3.4 Q1: 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 implements the IntPredicate
functional interface (package java.util.function).
c. IntStream method sorted (a lazy operation) orders the elements of the stream into
ascending order by default. All prior intermediate operations in the stream pipeline must
be complete so that method sorted knows which elements to sort.
d. Method filter is a stateless intermediate operationit requires information about
other elements in the stream in order to test whether the current element satisfies the
predicate.
17.3.4 Q2: 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.
page-pf9
17.3.5 Intermediate Operation: Mapping
17.3.5 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.3.6 Q1: 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.
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.4 Stream<Integer> Manipulations
17.4 Q1: Class Array’s ________ method is used to create a Stream from an array of
objects.
a. stream
b. arrayToStream
c. createStream
d. objectToStream
page-pfa
17.4.1 Q1: 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.4.1 Q2: 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.4.2 Q1: 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 pipelines 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.4.3 Q1: 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.
page-pfb
17.4.4 Filtering and Sorting a Stream and Collecting the
Results
17.4.5 Sorting Previously Collected Results
17.5 Stream<String> Manipulations
17.5.1 Mapping Strings to Uppercase Using a Method
17.5.1 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.5.2 Q1: 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.5.2 Q2: ________ 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
page-pfc
c. TreeMap::new
d. String::toUpperCase
17.5.2 Q3: ________ 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
b. System.out::println
c. TreeMap::new
d. String::toUpperCase
17.5.2 Q4: ________ 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.5.2 Q5: ________ 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.5.3 Q1: Functional interface Comparator’s default method ________ reverses an
existing Comparator’s ordering.
a. invert
b. descending
c. reversed
d. downward
17.6 Stream<Employee> Manipulations
page-pfd
17.6.1 Creating and Displaying a List<Employee>
17.6.2 Filtering Employees with Salaries in a Specified Range
17.6.2 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
b. short circuit
c. terminal
d. intermediate
17.6.2 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.6.3 Sorting Employees By Multiple Fields
17.6.3 Q1: 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.6.4 Mapping Employees to Unique Last Name Strings
17.6.4 Q1: Stream method ________ eliminates duplicate objects in a stream.
a. distinct
page-pfe
b. discrete
c. unique
d. different
17.6.5 Grouping Employees By Department
17.6.5 Q1: Collectors static method groupingBy with one argument receives a
Function that classifies objects in the streamthe 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.6.5 Q2: Map method ________ performs an operation on each keyvalue pair.
a. for
b. forNext
c. forEach
d. forAll
17.6.5 Q3: 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.6.6 Counting the Number of Employees in Each Department
17.6.6 Q1: 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.6.6 Q2: 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
page-pff
b. count
c. counting
d. enumerate
17.6.7 Summing and Averaging Employee Salaries
17.6.7 Q1: 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.7 Creating a Stream<String> from a File
17.7 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 streame.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.7 Q2: Map method entrySet returns a Set of Map.Entry objects containing the Map’s
________.
a. values
b. keys
c. index
d. keyvalue pairs
17.8 Generating Streams of Random Values
17.8 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.
page-pf10
c. An infinite stream is a stream with an unknown number of elementsyou 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.
17.8 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.9 Lambda Event Handlers
17.9 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.10 Additional Notes on Java SE 8 Interfaces
17.10 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.
page-pf11
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.10 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
d. one or more abstract methods.
17.11 Java SE 8 and Functional Programming
Resources

Trusted by Thousands of
Students

Here are what students say about us.

Copyright ©2022 All rights reserved. | CoursePaper is not sponsored or endorsed by any college or university.