<p>*3. Consider the following data field and method.
private ArrayList nums;
// precondition: nums.size() = 0;
// nums contains Integer objects
public void numQuest()
{
int k = 0;
Integer zero = new Integer(0);
while (k < nums.size())
{
if (nums.get(k).equals(zero))
nums.remove(k);
k++;
}
}
Assume that ArrayList nums initially contains the following
Integer values.
[0, 0, 4, 2, 5, 0, 3, 0]
What will ArrayList nums contain as a result of executing
numQuest ?
(A) <a href="B">0, 0, 4, 2, 5, 0, 3, 0</a> <a href="C">4, 2, 5, 3</a> <a href="D">0, 0, 0, 0, 4, 2, 5, 3</a> <a href="E">3, 5, 2, 4, 0, 0, 0, 0</a> [0, 4, 2, 5, 3]</p>
<p>College board says E. But I think B.</p>
<p>Hehe... you fell for the old one. Basically watch out for their stupid bracket tricks. See how the if statement doesn't have a bracket? That means that even if it removes some thing, it still iterates when it shouldn't</p>
<p>ex:</p>
<p>[0, 0, 2]->[0,2]</p>
<p>because
the first 0 is removed, and then now we have [0,2]. But k iterates, so the pointer goes from
[0,2]
.^...
to
[0,2]
_..^.</p>
<p>get it?</p>
<p>Btw, what exam are you taking? I am taking AB, and find it really helpful to actually code everything. Take a look at all the algorithms/(java implementations) and stuff you should know at my website:</p>
<p><a href="http://www.freewebs.com/sagar_indurkhya%5B/url%5D">www.freewebs.com/sagar_indurkhya</a></p>
<p>cheers! - Sagar</p>
<p>thanks sagar! I am taking the A exam</p>