Java SE 8 Programmer II: Exam 1Z0-809 Question Answer | ForkJoinPool, RecursiveAction | Oracle Exam

Java ForkJoinPool and RecursiveAction - Exam Question Answer

Question

Given: class Sum extends RecursiveAction{//line n1 static final int THRESHOLD_SIZE= 3; int stIndex, lstIndex; int [ ] data; public Sum (int [ ]data, int start, int end){ this.data = data; this stIndex = start; this.

lstIndex = end; } protected void compute ( ) { int sum = 0; if (lstIndex "" stIndex <= THRESHOLD_SIZE){ for (int i = stIndex; i < lstIndex; i++){ sum += data [i]; } System.out.println(sum); } else { new Sum (data, stIndex + THRESHOLD_SIZE, lstIndex).fork( ); new Sum (data, stIndex, Math.min(lstIndex, stIndex + THRESHOLD_SIZE) ).compute (); } } } and the code fragment: ForkJoinPool fjPool = new ForkJoinPool ( ); int data [ ] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} fjPool.invoke (new Sum (data, 0, data.length)); and given that the sum of all integers from 1 to 10 is 55

Which statement is true?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

A.

The given code implements a RecursiveAction that calculates the sum of an array of integers by dividing the array into smaller sub-arrays, and recursively computing the sum of each sub-array. The compute() method checks if the size of the sub-array is less than or equal to the THRESHOLD_SIZE constant. If the size is less than or equal to the THRESHOLD_SIZE, it computes the sum of the sub-array and prints it. Otherwise, it splits the sub-array into two smaller sub-arrays and forks a new Sum task to compute the sum of each sub-array.

The code fragment creates a ForkJoinPool and submits a Sum task to the pool with the entire array and the range from 0 to the length of the array as parameters. The invoke() method is called to start the computation and wait for the result. Therefore, the program should print the total sum of the array, which is 55.

So, the correct answer is B. The program prints 55.