Java: Solving sample question for exam
<p>I'm studying for a Java exam and I can't seem to solve this sample question correctly. </p>

<p>Suppose the method int sign(int x) returns 1 if x is positive, -1 if x is negative and 0 if x is 0. Given:</p>

<pre><code>int[] nums = {-2, -1, 0, 1, 2};
</code></pre>

<p>what are the values of the elements of nums after the following code is executed?</p>

<pre><code>for(int k=0; k&lt;nums.length; k++)
{
nums[k] -= sign(nums[k]);
nums[k] += sign(nums[k]);
}
</code></pre>

<p>This is a snippet of how I tried to solve it:</p>

<pre><code>nums k -= sign (nums [k])
nums [0] = nums[0]- sign (nums[0])
nums [0] = (-2) - (0)
nums [0] = -2
</code></pre>

<p>Can someone please explain how you get to the answer: <code>-2, 0, 0, 0, 2.</code>. Thanks!</p>
