Computer Science Question

<p>Consider the following method:</p>

<p>public void fun (int a, int b)
{
a += b;
b += a;
}</p>

<p>What is the output from the following code?</p>

<p>int x = 3, y = 5;
fun(x, y);
System.out.println(x + " " + y);</p>

<p>(A) 3 5
(B) 3 8
(C) 3 13
(D) 8 8
(E) 8 13
** I chose E as my answer. **</p>

<p>I ran this on JCreator and it says it cannot find the symbol method fun.
Can anyone tell me why and how to correct this?</p>

<p>Here is the code I typed:
** Class **
public class Test2
{
public void fun(int a, int b)
{
a += b;
b += a;
}</p>

<p>}</p>

<p>===============================================================</p>

<p>** Driver Program **</p>

<p>public class Test
{
public static void main (String [] args)
{
int x = 3;
int y = 5;
fun(x, y);
System.out.println(x + " " + y);
}
}</p>

<p>Of course it can't find fun; it's in a different class file, lord knows why you put it in a different class file o_O</p>

<p>Anyway, E is the trick answer, the question isn't testing to see if you know what the += operator means; it's testing to see if you know the crazy way Java handles variable passing.</p>

<p>Oh thanks kierke</p>

<p>So the answer will be A.</p>

<p>I fixed it, I made fun a static method and it worked.</p>

<p>And then called Test2.fun...</p>

<p>OMFG i want to destroy my compsci teacher. he's still not teaching us a dam thing!
our last test scores were an avg 60something percent. aghhh destroy him! we just finished classes.</p>

<p>hah, our test avg scores are around 70-80's.</p>

<p>with the sophomores scoring high and the juniors/seniors literally flunking.
weird eh?</p>

<p>Won't the answer be D, cuz your adding 5+3 and 3+5= (8,8)????????????????????????</p>

<p>No... Methods in Java that accept primitive answer types pass parameters by value and not by reference. Instead of passing the actual memory location of the variables, the method body receives copies of the parameters. New variables are created holding these values. Thus, any modifications that occurs within the method to the passed-in variables is discarded once the thread of execution leaves the method. </p>

<p>Note: When OBJECTS are passed as a parameter, the memory value is also passed to the method. Modifications made to objects within a helper method are visible outside of the method. </p>

<p>Ahh the days of C++ when one could choose to pass by reference or by value... I really miss C++. Too bad AP changed the language on me.</p>