?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Easy

Enhanced For Loop Using Integer Objects in an ArrayList

APCSA-LXY6XJ

Consider the following code.

    ArrayList<Integer> nums = new ArrayList<Integer>();
    nums.add(700);
    nums.add(500);
    nums.add(100);
    nums.add(300);
    nums.add(100);

    int sum = 0;
    // insert your code here

Which loop would correctly print the sum of the values from nums?

I.

for (Integer num : nums)
{
    sum += num.intValue();
}
System.out.print(sum);

II.

for (int num : nums)
{
    sum += num;
}
System.out.print(sum);

III.

for (Integer num : nums)
{
    sum += num;
}
System.out.print(sum);
A

I only.

B

II only.

C

III only.

D

II and III only.

E

I, II, and III.