Given the array of integers 'array' shown below : 13 7 27 2 18 33 9 11 22 8
Given the array of integers 'array' shown below :
13 7 27 2 18 33 9 11 22 8
What is the output of the following Java statements?
int [ ] p = new int [10];
int [ ] q = new int [10];
for (ink k = 0; k < 10; k++)
p[k] = array [k];
q = p;
p[4] = 20;
System out. println (array [4]+ "." + q[4]);
Show answerHide answer
Correct answer: (C) 18 : 20
Explanation
The loop copies all values of 'array' into p. After q = p, q refers to the same array object as p, so p[4] = 20 also makes q[4] = 20. 'array' itself is unchanged, so array[4] = 18. The output is 18 : 20.