1.
Q1: The following code is intended to store the sum of all the values in the integer ArrayList
list
in the variable total
. Which of the following code segments can be used to replace /* missing code */
so that the code works as intended?
int total = 0;
/* missing code */
System.out.println(total);
Options:
I. for (int pos = 0; pos < list.size(); pos++)
total += list.get(pos);
II. for (int pos = list.size(); pos > 0; pos--)
total += list.get(pos);
III. int pos = 0;
while (pos < list.size()) {
total += list.get(pos);
pos++;
}
- I only
- II only
- III only
- I and III only
- II and III only