159k views
0 votes
What would be the results after the following code was executed? int[] x = {23, 55, 83, 19}; int[] y = {36, 78, 12, 24}; for(int a = 0; a < x.length; a++) { x[a] = y[a]; y[a] = x[a]; }

User Argie
by
3.8k points

1 Answer

4 votes

Answer:

x = y = {36, 78, 12, 24}

Explanation:

The loop executes 4 times, as indicated by the length of array x.

The first line in the content of the loop assigns every element in array y to array x. Because both arrays now have the same content, the second line of code is quite redundant and is assigning the new values of x to y. Since these new values of x are the old values of y, there is no change in the contents of y. They are just being replaced by themselves.

In other words, the second line is not needed for anything. In fact, if the loop has much more contents, the second makes it work twice as much, reducing efficiency.

User Chiranga Alwis
by
4.1k points