What could be the output of the following?
StreamQuiz

Quiz
•
Computers
•
Professional Development
•
Medium
Diego Ramirez
Used 4+ times
FREE Resource
12 questions
Show all answers
1.
MULTIPLE CHOICE QUESTION
1 min • 1 pt
Answer explanation
D. No terminal operation is called, so the stream never executes. The first line creates an infinite stream reference. If the stream were executed on the second line, it would get the first two elements from that infinite stream, "" and "1", and add an extra character, resulting in "2" and "12", respectively. Since the stream is not executed, the reference is printed instead, giving us option D.
2.
MULTIPLE CHOICE QUESTION
1 min • 1 pt
What could be the output of the following?
true false
true true
java.util.stream.ReferencePipeline$3@4517d9a3
An exception is thrown.
The code hangs.
Answer explanation
F. Both streams created in this code snippet are infinite streams. The variable b1 is set to true since anyMatch() terminates. Even though the stream is infinite, Java finds a match on the first element and stops looking. However, when allMatch() runs, it needs to keep going until the end of the stream since it keeps finding matches. Since all elements continue to match, the program hangs, making option F the answer.
3.
MULTIPLE CHOICE QUESTION
1 min • 1 pt
What could be the output of the following?
false false
An exception is thrown
java.util.stream.ReferencePipeline$3@4517d9a3
The code does not compile.
The code hangs.
Answer explanation
E. An infinite stream is generated where each element is twice as long as the previous one. While this code uses the three-parameter iterate() method, the condition is never false. The variable b1 is set to false because Java finds an element that matches when it gets to the element of length 4. However, the next line tries to operate on the same stream. Since streams can be used only once, this throws an exception that the “stream has already been operated upon or closed” and making option E the answer. If two different streams were used, the result would be option B.
4.
MULTIPLE SELECT QUESTION
1 min • 1 pt
Which are true statements about terminal operations in a stream that runs successfully? (Choose all that apply.)
A.- At most one terminal operation can exist in a stream pipeline.
B.- Terminal operations are a required part of the stream pipeline in order to get a result.
C.- Terminal operations have Stream as the return type.
D.- The peek() method is an example of a terminal operation.
E.- The referenced Stream may be used after calling a terminal operation.
A
B
C
D
E
Answer explanation
A, B. Terminal operations are the final step in a stream pipeline. Exactly one is required, because it triggers the execution of the entire stream pipeline. Therefore, options A and B are correct. Option C is true of intermediate operations rather than terminal operations. Option D is incorrect because peek() is an intermediate operation. Finally, option E is incorrect because once a stream pipeline is run, the Stream is marked invalid.
5.
MULTIPLE SELECT QUESTION
1 min • 1 pt
Which of the following sets result to 8.0? (Choose all that apply.)
Answer explanation
C, F. Yes, we know this question is a lot of reading. Remember to look for the differences between options rather than studying each line. These options all have much in common. All of them start out with a LongStream and attempt to convert it to an IntStream. However, options B and E are incorrect because they do not cast the long to an int, resulting in a compiler error on the mapToInt() calls.
Next, we hit the second difference. Options A and D are incorrect because they are missing boxed() before the collect() call. Since groupingBy() is creating a Collection, we need a nonprimitive Stream. The final difference is that option F specifies the type of Collection. This is allowed, though, meaning both options C and F are correct.
6.
MULTIPLE CHOICE QUESTION
1 min • 1 pt
Which of the following can fill in the blank so that the code prints out false? (Choose all that apply.)
allMatch
anyMatch
findAny
findFirst
noneMatch
Answer explanation
A. Options C and D do not compile because these methods do not take a Predicate parameter and do not return a boolean. When working with streams, it is important to remember the behavior of the underlying functional interfaces. Options B and E are incorrect. While the code compiles, it runs infinitely. The stream has no way to know that a match won't show up later. Option A is correct because it is safe to return false as soon as one element passes through the stream that doesn't match.
7.
MULTIPLE CHOICE QUESTION
1 min • 1 pt
We have a method that returns a sorted list without changing the original. Which of the following can replace the method implementation to do the same with streams?
Answer explanation
F. There is no Stream<T> method called compare() or compareTo(), so options A through D can be eliminated. The sorted() method is correct to use in a stream pipeline to return a sorted Stream. The collect() method can be used to turn the stream into a List. The collect() method requires a collector be selected, making option E incorrect and option F correct.
Create a free account and access millions of resources
Similar Resources on Quizizz
10 questions
C Code Master lvl 2 A

Quiz
•
Professional Development
17 questions
Success Forge C# Assignment 1

Quiz
•
Professional Development
10 questions
CIT JULY 2022

Quiz
•
Professional Development
10 questions
DECI - Week 5 - round

Quiz
•
Professional Development
10 questions
Java SE: Programming I

Quiz
•
Professional Development
10 questions
C - Programming ( Output based - Loop )

Quiz
•
Professional Development
10 questions
Day 2 C Programming quiz

Quiz
•
Professional Development
16 questions
DP-900 MÓDULO 4 pt-br

Quiz
•
Professional Development
Popular Resources on Quizizz
15 questions
Character Analysis

Quiz
•
4th Grade
17 questions
Chapter 12 - Doing the Right Thing

Quiz
•
9th - 12th Grade
10 questions
American Flag

Quiz
•
1st - 2nd Grade
20 questions
Reading Comprehension

Quiz
•
5th Grade
30 questions
Linear Inequalities

Quiz
•
9th - 12th Grade
20 questions
Types of Credit

Quiz
•
9th - 12th Grade
18 questions
Full S.T.E.A.M. Ahead Summer Academy Pre-Test 24-25

Quiz
•
5th Grade
14 questions
Misplaced and Dangling Modifiers

Quiz
•
6th - 8th Grade