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 (int k = 0; k < 10; k ++)
p[k] = array [k];
q = p;
p[4] = 20;
System.out.println(array [4] + “ : ” + q[4]);
(A) 20 : 20
(B) 18 : 18
(C) 18 : 20
(D) 20 : 18
Answer: (C)
Explanation:
int [ ] p = new int [10];
int [ ] q = new int [10];
for (int k = 0; k < 10; k ++)
p[k] = array [k];
q = p;
p[4] = 20;
System.out.println(array [4] + “:” + q[4]);
In for (int k = 0; k < 10; k ++) p[k] = array [k];
We are copying array element in array after that p[4] = 20; will make final change into p array.
So, array[4] is same as previous(i.e. 18) but p[4] is now 20.
So, option (C) is correct.
Quiz of this Question
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...