About Question com.enthuware.ets.scjp.v6.2.197 :

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

Moderator: admin

Post Reply
Diego Almeida

About Question com.enthuware.ets.scjp.v6.2.197 :

Post by Diego Almeida »

Question:

What will be the output of compiling and running the following program?
class CloneTest
{
public static void main(String[] args)
{
int ia[ ][ ] = { { 1 , 2}, null };
int ja[ ][ ] = (int[ ] [ ])ia.clone();
System.out.print((ia == ja) + " ");
System.out.println(ia[0] == ja[0] && ia[1] == ja[1]);
}
}


Answer:
It will print 'false true' when run.


Explanation:
A clone of a multidimensional array is shallow, which is to say that it creates only a single new array. Subarrays are shared, so ia and ja are different but ia[0] and ja[0] are same.

why? how prints false in the first operation? The Explanation said: "is[0] and ja[0] are same!" someone help me?

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

Re: About Question com.enthuware.ets.scjp.v6.2.197 :

Post by admin »

false is printed because of

Code: Select all

System.out.print((ia == ja) + " ");
true is printed because of

Code: Select all

System.out.println(ia[0] == ja[0] && ia[1] == ja[1]);


See the attached image:
arrayclone.gif
arrayclone.gif (2.45 KiB) Viewed 3982 times
You can see that ia and ja point to different arrays so ia == ja is false. ia[0] == ja[0] as well as ia[1] == ja[1] are both true. So it prints true.

HTH,
Paul

Diego Almeida

Re: About Question com.enthuware.ets.scjp.v6.2.197 :

Post by Diego Almeida »

Tks! I'd not seen the "print" without "ln", I had thinking the first sysout don't print nothing. Understand thoroughly!

Post Reply

Who is online

Users browsing this forum: Bing [Bot], witek_m and 12 guests