?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

ArrayList Integer Adjustment

APCSA-AFXXPV

Consider the following code segment.

ArrayList<Integer> nums = // list full of integers.  
                          // nums contains at least one element.

int i=0;
while(nums.size()>1)
{
    if(i<nums.size()-1)
    {
        nums.set(i, nums.get(i)+nums.get(i+1));
        nums.remove(i+1);
    }
}

Which of the following statements BEST describes the result of this segment of code?

A

An index out of bounds error is thrown

B

The code gets into an infinite loop

C

All elements in nums have been removed; it now has no elements

D

nums now has only one element, which is the sum of all the original array elements

E

Each element in nums has been increased by the value of the next element