StreamQuiz

StreamQuiz

Professional Development

12 Qs

quiz-placeholder

Similar activities

CloudBinary_Jenkins_Quiz- 2

CloudBinary_Jenkins_Quiz- 2

Professional Development

15 Qs

Quiz Genesys Session 2

Quiz Genesys Session 2

Professional Development

10 Qs

DOT NET-SET1

DOT NET-SET1

Professional Development

15 Qs

Mech 3

Mech 3

Professional Development

10 Qs

GAGC-Dotnet-SET2

GAGC-Dotnet-SET2

Professional Development

15 Qs

Code X Trade

Code X Trade

Professional Development

15 Qs

Unity & C-Sharp Quiz

Unity & C-Sharp Quiz

Professional Development

10 Qs

AI-900 MÓDULO 02 pt-br

AI-900 MÓDULO 02 pt-br

Professional Development

15 Qs

StreamQuiz

StreamQuiz

Assessment

Quiz

Computers

Professional Development

Medium

Created by

Diego Ramirez

Used 4+ times

FREE Resource

12 questions

Show all answers

1.

MULTIPLE CHOICE QUESTION

1 min • 1 pt

Media Image

What could be the output of the following?

12112

  1. 212

  1. 212112

java.util.stream.ReferencePipeline$3@4517d9a3

  1. The code does not compile.

Answer explanation

  1. 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

Media Image

What could be the output of the following?

  1. true false

  1. true true

  1. java.util.stream.ReferencePipeline$3@4517d9a3

  1. An exception is thrown.

  1. The code hangs.

Answer explanation

  1. 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

Media Image

What could be the output of the following?

  1. false false

  1. An exception is thrown

  1. java.util.stream.ReferencePipeline$3@4517d9a3

  1. The code does not compile.

  1. The code hangs.

Answer explanation

  1. 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

  1. Which are true statements about terminal operations in a stream that runs successfully? (Choose all that apply.)

    1. A.- At most one terminal operation can exist in a stream pipeline.

    2. B.- Terminal operations are a required part of the stream pipeline in order to get a result.

    3. C.- Terminal operations have Stream as the return type.

    4. D.- The peek() method is an example of a terminal operation.

    5. E.- The referenced Stream may be used after calling a terminal operation.

A

B

C

D

E

Answer explanation

  1. 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.)

Media Image
Media Image
Media Image
Media Image
Media Image

Answer explanation

  1. 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

Media Image

Which of the following can fill in the blank so that the code prints out false? (Choose all that apply.)

  1. allMatch

  1. anyMatch

  1. findAny

  1. findFirst

  1. noneMatch

Answer explanation

  1. 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

Media Image

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?

Media Image
Media Image
Media Image
Media Image
Media Image

Answer explanation

  1. 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

Create resources
Host any resource
Get auto-graded reports
or continue with
Microsoft
Apple
Others
By signing up, you agree to our Terms of Service & Privacy Policy
Already have an account?