About Question enthuware.ocajp.i.v7.2.878 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
javaman
Posts: 33
Joined: Wed Nov 13, 2013 4:11 pm
Contact:

About Question enthuware.ocajp.i.v7.2.878 :

Post by javaman »

int[] scores = { 1, 2, 3, 4, 5, 6};
System.arraycopy(scores, 2, scores, 3, 2);
for(int i : scores) System.out.print(i);

I don't understand how the correct answer can be 123346. Signature for arraycopy is:
public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
System.arraycopy(scores, 2, scores, 3, 2) is in English: copy 1 (2-1) item from the 3rd position (index 2) of the 'scores' array to the 4th (index 3) position of the same array.
So from 1,2,3,4,5,6 I copy item with value 3 to position 4 and I get 1,2,3,3,4,5,6. Array is immutable and I assume that 6 'falls off'. This gives me 1,2,3,3,4,5 and not 1,2,3,3,4,6

javaman
Posts: 33
Joined: Wed Nov 13, 2013 4:11 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.878 :

Post by javaman »

I guess it copies the nr of chars from the pos indicated by 2nd arguments to position indicated by 4th element and overwrites whatever is there...

admin
Site Admin
Posts: 10068
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.878 :

Post by admin »

Your code prints 123346 because from the source elements are 3 and 4. You are putting them in position 4, 5, which makes the final array 123 34 6.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 144 guests