Java OCA OCP Practice Question 2844

Question

What statements about the following code are true? (Choose all that apply.)

import java.util.Arrays;

public class Main {

   public static void main(String[] args) {
      System.out.println(Arrays.asList("abc","Java","Javascript","SQL") 
            .parallelStream().parallel() // q1 
            .reduce(0, // w  ww.jav a2s  .  c o  m
               (c1, c2) -> c1.length() + c2.length(), // q2 
               (s1, s2) -> s1 + s2)); // q3 

   }
}
  • A. It compiles and runs without issue, outputting the total length of all strings in the stream.
  • B. The code will not compile because of line q1.
  • C. The code will not compile because of line q2.
  • D. The code will not compile because of line q3.
  • E. It compiles but throws an exception at runtime.


C.

Note

The code does not compile, so A and E are incorrect.

The problem here is that c1 is a String but c2 is an int, so the code fails to combine on line q2, since calling length() on an int is not allowed, and C is correct.

The rest of the lines compile without issue.

Note that calling parallel() on an already parallel is allowed, and it may in fact return the same object.




PreviousNext

Related