?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

ArrayList Integer Sum

APCSA-FEPDVX

Which of the following loops will correctly print the sum of all the elements from nums?

    ArrayList<Integer> nums = // list full of integer values
    I.
    int sum = 0;
    for (Integer n : nums)
    {
        sum += n;
    }
    System.out.println(sum);
    II.
    int sum = 0;
    for (int n : nums)
    {
        sum += n;
    }
    System.out.println(sum);
    III.
    int sum = 0;
    for (int i=0; i<nums.size(); i++)
    {
        sum += new Integer(nums.get(i));
    } 
    System.out.println(sum);
A

I only.

B

II only.

C

III only.

D

I and II only.

E

I, II, and III.