Ira, I think you are right that the output will depend on the number of threads used in the parallel split. At least, theoretically. In practice, such a small stream will almost certainly never have any work for a second
thread, regardless of the fact that it's parallel. Even for a much larger stream, I have a hard time finding an example that actually shows inconsistent values. But here's one to look at:
This still gives the same result, -2014259968, each time I run it. But if I comment out the call to parallel(), I get -2014260031. This confirms that the result is affected by whether it's parallel or not. Each time a new accumulator is created for a new split of the work, the incorrect identity value of 1 creates an additional error in the calculation. In practice this is still pretty deterministic on a given JVM on a given machine... but it's at least theoretically possible that the result could be different, depending on what
else the processor is busy with. I think it's a bad question, because although the result is
probably consistent, it's not
guaranteed to be consistent.
The simplest fix, actually, would be to use the correct identity of 0 instead of 1. Then the results are guaranteed to be consistent, even with parallel streams. If they want to use an incorrect identity like 1, the correct answer should expose that fact, not hide it.