Java OCA OCP Practice Question 2749

Question

What is the output of the following?

Stream<String> stream = Stream.iterate("", (s) -> s + "1"); 
System.out.println(stream.limit(2).map(x -> x + "2")); 
A.   12112
B.   212
C.   212112
D.   java.util.stream.ReferencePipeline$3@1234d9a3
E.  The code does not compile.
F.  An exception is thrown.
G.  The code hangs.


D.

Note

No terminal operation is called, so the stream never executes.

The methods chain to create a stream that would contain "2" and "12."

The first line creates an infinite stream.

The second line would get the first two elements from that infinite stream and map each element to add an extra character.




PreviousNext

Related