1. Rewrite the following loops, using the enhanced for loop construct. Here, val
ID: 3702997 • Letter: 1
Question
1. Rewrite the following loops, using the enhanced for loop construct. Here, values is an array of floating-point numbers.
a) for (int i = 0; i < values.length; i++) { total = total + values[i]; }
b) for (int i = 1; i < values.length; i++) { total = total + values[i]; }
c) for (int i = 0; i < values.length; i++)
{
if (values[i] == target) { return i; }
}
2.
Insert the missing statement in the code fragment below. The method should return the result of adding the first and last values of the integer array received as argument.
A.
B.
C.
3.
What output is generated by the program below?
A.
values[0] + values[values.length - 1]Explanation / Answer
1) a) for (int value: values) { total = total + value; } b) for (int value: values) { total = total + value; } total = total-values[0]; c) int i=0; for (int value: values) { if(value == target) return i; i++; } 2. C. array[0] + array[array.length - 1] 3. D. 462
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.