Java OCA OCP Practice Question 1629

Question

How many lines does this code output?

List<String> list = new LinkedList<>();
list.add("A");
list.add("B");

list.stream().forEach(System.out.println);
list.stream().forEach(System.out.println);
  • A. Two
  • B. Four
  • C. The code does not compile.
  • D. The code compiles but throws an exception at runtime.


C.

Note

Calling two different streams is allowed.

The code attempts to use a method reference when calling the forEach() method.

It does not use the right syntax for a method reference.

A double colon needs to be used.

The code would need to be changed to System.

out::println to work and print two lines for each call.

Since it does not compile, Option C is correct.




PreviousNext

Related