recategorized by
2,841 views
0 votes
0 votes

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]);
  1. 20:20
  2. 18:18
  3. 18:20
  4. 20:18
recategorized by

3 Answers

Best answer
5 votes
5 votes
for (int k = 0; k < 10; k ++)

p[k] = array [k];

in this loop we are copying all the values from the array to the array P

now

Q=p

Q is pointing P

now p[4]=20

but Q contains the old P array where in 4 th index i.e Q[4] is is also 20(as the location is updated by previous P )

Note: here pointer is involved , so the location is being update ,so Q is getting 20 here .

but  question is about array[4] which was  initial array ,so value 18

so answer is 18:20
selected by
2 votes
2 votes
for (int k = 0; k < 10; k ++)

p[k] = array [k];

in this loop we are copying all the values from the array to the array P

now

Q=p

Q is pointing P

now p[4]=20

but Q contains the old P array where in 4 th index i.e Q[4] is is also 20(as the location is updated by previous P )

Note: here pointer is involved , so the location is being update ,so Q is getting 20 here .

but question is about array[4] which was also 18

so answer is 18:20
1 votes
1 votes

Answer: (C)

for ( int k=0; k<10; k++)
    p[k]= array [k];

The elements of "array" are copied to "p" using above statements.

q=p;

Memory location of "p" is assigned to "q" using above statement.

p[4]=20;

Memory location of p[4] now equals 20. So, q[4] = p[4] = 20

array[4] = 18

So, 

System.out.println(array[4]+":"+q[4]);

prints 18:20

Answer:

Related questions

2 votes
2 votes
1 answer
4
go_editor asked Aug 20, 2016
2,991 views
It is possible to define a class within a class termed as nested class. There are __ types of nested classes2345