Question

In: Computer Science

In Java: int[] A = new int[2]; A[0] = 0; A[1] = 2; f(A[0],A[A[0]]); void f(int...

In Java:

int[] A = new int[2];

A[0] = 0; A[1] = 2;

f(A[0],A[A[0]]);

void f(int x, int y) {

x = 1; y = 3;

}

For each of the following parameter-passing methods, saw what the final values in the array A would be, after the call to f. (There may be more than one correct answer.)

a. By value. b. By reference. c. By value-result.

Solutions

Expert Solution

a)With pass by value, none of the actual arguments are changed, so the variables retain the values they were initialized with.

So,. A[0] =0;

A[A[0]] = 0;

b) With pass by reference, the arguments are changed.

After the call to the f the value changed

A[0] = 1;

A[A[0]] = 2; as because we pass an full array and array behaves as a object in java

c) by value result it is same as pass by reference

So, A[0] = 1;

A[A[0]] = 2;


Related Solutions

Java try and catch problem public void method1(){ int[] array = new int[1]; try{ array[2] =...
Java try and catch problem public void method1(){ int[] array = new int[1]; try{ array[2] = 0;} catch(ArithmeticException e){array[2] = 0} // ? finally{ return 0;}} Could you please tell me why the line with the question mark will not report an error?
(JAVA) InvertArrangement +invert(int[] a) : int [] +print(int[] a) : void Example 1: the invert method...
(JAVA) InvertArrangement +invert(int[] a) : int [] +print(int[] a) : void Example 1: the invert method receives the following arrangement: [1,2,3,4,5] The invert method returns the array [5,4,3,2,1] Example 2: the print method receives the following array: [5,4,3,2,1] The print method prints: 5,4,3,2,1 (data separated by commas). TIP: for the print method use System.out.print () without the "ln".
public void printQueue(DoublyLinkedQueue<Integer> Q){ int len = Q.size(); int k = 0; for (int i=0; i...
public void printQueue(DoublyLinkedQueue<Integer> Q){ int len = Q.size(); int k = 0; for (int i=0; i < len; ++i){ k = Q.dequeue(); Q.enqueue(k);    System.out.println(Q.dequeue()); } } What is the complexity of this code? Select one: a. O(N), N = k b. Q(1) c. Q(N2), N = Q.size() d. O(M), M = Q.size() e. None of the alternatives is correct. which one?
JAVA RECURSION public void recMethod(int[] array, int start, int end) { if(start < end) { print...
JAVA RECURSION public void recMethod(int[] array, int start, int end) { if(start < end) { print the array double the value in the array at position start recMethod(array, start+1, end) print the array } else { print "done" } } Assume the method is invoked with array = [3, 4, 6, 7, 8, 10, 4], start = 2, and end = 5 1.How many times is the array printed before the word "done" is printed? 2.How many times is the...
class ArrayReverse1{ public static void reverse(int[] a, int index) { if (index >0) { index= index...
class ArrayReverse1{ public static void reverse(int[] a, int index) { if (index >0) { index= index - 1; // Decrementing the index System.out.printf("%d%n", a[index]); reverse(a, index); // Recursive call } return; } public static void main (String args[]) { int [] array = { 1, 2, 3, 4, 5 }; int n=array.length; reverse(array,n); // function call } } Write a generic version of the corrected recursive reverse method that could be used to print any of the following arrays (or...
Let f(x, y) be a function such that f(0, 0) = 1, f(0, 1) = 2,...
Let f(x, y) be a function such that f(0, 0) = 1, f(0, 1) = 2, f(1, 0) = 3, f(1, 1) = 5, f(2, 0) = 5, f(2, 1) = 10. Determine the Lagrange interpolation F(x, y) that interpolates the above data. Use Lagrangian bi-variate interpolation to solve this and also show the working steps.
*/ #include using namespace std; void start (int boxes [10]); void move (int squares [10], int...
*/ #include using namespace std; void start (int boxes [10]); void move (int squares [10], int x, int y, int z); void add (int arr [10], int first, int last); void print (int arr [10]); int main () {     int my_arr [10];         cout << "The original array is:\n";     print (my_arr);         start (my_arr);     cout << "\n\nThe array after start is:\n";     print (my_arr);         move (my_arr, 2, 4, 6);     cout << "\n\nThe...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down Java codes to invoke m1 method, declare variables as needed, (Do NOT implement the method)
What is the ouput of the following code? void loop(int num) { for(int i = 1;...
What is the ouput of the following code? void loop(int num) { for(int i = 1; i < num; ++i) { for(int j = 0; j < 5; ++j) { cout << j; } } } int main() { loop(3); return 0; }
Find f. f ''(x) = x−2,    x > 0,    f(1) = 0,    f(4) = 0 f(x)=
Find f. f ''(x) = x−2,    x > 0,    f(1) = 0,    f(4) = 0 f(x)=
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT