1.
Q12: Given the above code, what is the value in
sum
after execution?
add()
get(<expression>)
method call (remember evaluating expressions subgoals)
get
represents the index in the ArrayList. The size of the ArrayList is the number of elements contained. If the ArrayList is initially empty, the size is 0.
arrayListName.size() - 1
, inclusive; otherwise IndexOutOfBoundsException
occurs
arrayListName.get(index)
returns the value stored at that index
set(<expression>, value)
method call which will be the index for the element to be updated
ArrayList<Integer> alpha = new ArrayList<Integer>();
alpha.add(2);
alpha.add(4);
alpha.add(6);
alpha.add(8);
alpha.add(10);
alpha.add(12);
alpha.add(14);
int sum = 0;
for (int k = 0; k < alpha.size(); k+=2)
sum = sum + alpha.get(k);
sum
after execution?