1Z0-809 · Question #64
Given the following code and code fragment: ``java class Sum extends RecursiveAction { //line n1 static final int THRESHOLD_SIZE = 3; int stIndex, lsIndex; int [] data; public Sum(int []data, int…
The correct answer is A. The program prints several values that total 55. Option A is correct because the Fork/Join framework splits the array into chunks (based on THRESHOLD_SIZE = 3), and each subtask computes and prints its own partial sum independently - producing several separate output values (e.g., 6, 15, 24, 10) that collectively add up to…
Question
class Sum extends RecursiveAction { //line n1
static final int THRESHOLD_SIZE = 3;
int stIndex, lsIndex;
int [] data;
public Sum(int []data, int start, int end) {
this.data = data;
this.stIndex = start;
this.lsIndex = end;
}
protected void compute () {
int sum = 0;
if (lsIndex ? stIndex<= THRESHOLD_SIZE) {
for (int i = stIndex; i < lsIndex; i++) {
sum = data [i];
}
System.out.println(sum);
} else {
new Sum (data, stIndex + THRESHOLD_SIZE, lsIndex).fork();
new Sum (data, stIndex,
Math.min (stIndex, 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?Options
- AThe program prints several values that total 55.
- BThe program prints 55.
- CA compilation error occurs at line n1.
- DThe program prints several values whose sum exceeds 55.
How the community answered
(58 responses)- A76% (44)
- B3% (2)
- C12% (7)
- D9% (5)
Explanation
Option A is correct because the Fork/Join framework splits the array into chunks (based on THRESHOLD_SIZE = 3), and each subtask computes and prints its own partial sum independently - producing several separate output values (e.g., 6, 15, 24, 10) that collectively add up to 55.
Why the distractors are wrong:
- B is wrong because
RecursiveActionhas no return value - there is no mechanism to accumulate and combine the chunk sums into a single "55." Each subtask just prints its local result and exits. - C is wrong because extending
RecursiveActionis perfectly valid Java;RecursiveActionis the correct base class for Fork/Join tasks that don't return a result, so no compilation error occurs at line n1. - D is wrong because the data is partitioned cleanly without overlap, so no element is processed twice - the partial sums cover every element exactly once, totaling exactly 55, not more.
Memory tip: The key distinction is RecursiveAction (no return value, just side effects like printing) vs. RecursiveTask<T> (returns a value that can be joined/merged). When you see RecursiveAction, expect multiple printed outputs, never a single combined result.
Community Discussion
No community discussion yet for this question.