Analyze the code below public class Test public static void

Analyze the code below:

public class Test {
public static void main(String[] args) {
int[] x = {1, 2, 3, 4};
int[] y = x;

x = new int[2];
System.out.print(y[2] + \" \");
}
}

java

Solution

Answer:

Its output will be 3.

Y is copied to x in second statement.\" X=new int[2] \"this statement saves space to variable x, but has no values in it.For y[2] is 3rd element of array which was copied at first. Hence 3

Analyze the code below: public class Test { public static void main(String[] args) { int[] x = {1, 2, 3, 4}; int[] y = x; x = new int[2]; System.out.print(y[2]

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site