The program will compile and run. The array object will be constructed successfully (but it will have no cells and no values).
The enhanced for loop will run correctly
(but it will not assign anything to val
and will never
execute its loop body.)
The program will print out:
The total is: 0.0
This is a questionable claim. It does not distinguish between an array with elements that sum to zero and an array with no elements.
A better program would test for zero-length arrays and treat them as a special case. Our next program will do that.
Here is the program with some additional statements for computing the average of the elements. It checks that there are more than zero elements and avoids dividing by zero.
You might think that it is strange to test if array
contains any elements,
since it is obvious that it does.
However, in a more realistic program the array would come from an
external source (perhaps from a file), and might contain no elements.
The data might come from a human user, and sometimes humans make errors.
Fill in the blanks.