Question: In JAVA Thank You What is the exact output produced by running the method test? /** * TestSwap.java * Demonstrates parameter passing involving arrays and
In JAVA Thank You
What is the exact output produced by running the method test?
/** * TestSwap.java * Demonstrates parameter passing involving arrays and integers, * scope of variables, flow of control, and overloaded methods. */
public class TestSwap { public void swap (int x, int y) { int temp; temp = x; x = y; y = temp; System.out.println("Inside swap version 1:"); System.out.println("x = " + x); System.out.println("y = " + y);
}
public void swap (int[] a, int i, int j) { int temp; temp = a[i]; a[i] = a[j]; a[j] = temp; System.out.println("Inside swap version 2:"); System.out.println("a[" + i + "] = " + a[i]); System.out.println("a[" + j + "] = "+ a[j]);
}
public void printArray (int[] a) { System.out.print("Array elements: ");
for (int i = 0; i < a.length; i++) System.out.print(a[i] + " ");
System.out.println(); }
public static void test() { TestSwap t = new TestSwap(); final int ARRAY_SIZE = 3; int[] arr = new int[ARRAY_SIZE]; arr[0] = 3;
arr[1] = 8; arr[2] = 6; int x = 1, y = 2;
t.printArray (arr); t.swap (x, y); System.out.println ("Inside test:"); System.out.println ("x = " + x); System.out.println ("y = " + y); t.printArray (arr); t.swap (arr[x], arr[y]); t.printArray (arr); t.swap (arr, x, y); t.printArray (arr);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
