
StreamQuiz
Authored by Diego Ramirez
Computers
Professional Development
Used 4+ times

AI Actions
Add similar questions
Adjust reading levels
Convert to real-world scenario
Translate activity
More...
Content View
Student View
12 questions
Show all answers
1.
MULTIPLE CHOICE QUESTION
1 min • 1 pt
What could be the output of the following?
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.
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.
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.
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.
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.
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.
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.
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.
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.
Access all questions and much more by creating a free account
Create resources
Host any resource
Get auto-graded reports

Continue with Google

Continue with Email

Continue with Classlink

Continue with Clever
or continue with

Microsoft
%20(1).png)
Apple
Others
Already have an account?
Similar Resources on Wayground
10 questions
SQL Commands - SELECT Statements
Quiz
•
Professional Development
10 questions
Day 3 C Programming quiz
Quiz
•
Professional Development
11 questions
Types of Mass Media
Quiz
•
10th Grade - Professi...
17 questions
Volume 2
Quiz
•
Professional Development
10 questions
Intro to Node.js
Quiz
•
Professional Development
10 questions
Friday Fun
Quiz
•
Professional Development
12 questions
Guess the Gaming Pet 1
Quiz
•
KG - Professional Dev...
11 questions
Technology Morning Tea Quiz
Quiz
•
KG - Professional Dev...
Popular Resources on Wayground
8 questions
2 Step Word Problems
Quiz
•
KG - University
20 questions
Comparing Fractions
Quiz
•
4th Grade
15 questions
Fractions on a Number Line
Quiz
•
3rd Grade
20 questions
Equivalent Fractions
Quiz
•
3rd Grade
25 questions
Multiplication Facts
Quiz
•
5th Grade
10 questions
Latin Bases claus(clois,clos, clud, clus) and ped
Quiz
•
6th - 8th Grade
22 questions
fractions
Quiz
•
3rd Grade
7 questions
The Story of Books
Quiz
•
6th - 8th Grade