This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on array" Watch "Question on array" New topic
Author

Question on array

Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
class Test {
public static void main(String[ ] args) {
int[] a = { 1, 2, 3, 4 };
int[] b = { 2, 3, 1, 0 };
System.out.println( a [ (a = b)[3] ] );
}
}
What will it print when compiled and run ?

Answer: 1


How 1 is the answer??


Thanks<br />Dinesh
vaibhav mishra
Ranch Hand

Joined: Jun 18, 2008
Posts: 168
it seems that since a=b assignment occurs, it make referecnce a point to array reference b which is similar to in numerical sense a[any index]=b[any index] and a result is stored in temporary variable say t
where
t=(a=b)[3]
t =a[3]//this a is pointing towards b
t=0
so t is 0

it might occur that a[] value are modified but it is not so,

and the resulting statement is

which will translate to

which will print value of a[0]

which is 1
but this inner a is referencing to initial b so a's current value are
a[]={2,3,1,0} which is
same as b

a[3] has value of b[3] that is 0 stored in it
so code reduces from a[a[3]]
[code]a[3]//replacing value of a[][]


SCJP
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on array
 
Similar Threads
Mock exam question
operators
b*=b1 vs b=b*b1
Right Shift operator
reference ... is ambiguous. How to handle it?