aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes From  deepak's mock Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "From  deepak Watch "From  deepak New topic
Author

From deepak's mock

sudha
Greenhorn

Joined: Oct 30, 2000
Posts: 4
Hi
This is from deepak's mock
Consider the following code :

1. int [ ] a = new int[ 2 ];
2. int b = 1;
3. a[b] = b = 0;

Value will be stored in what element of array a.
A.a[1]
B.none
C.a[0]
Answer given was a[1]
How can it be a[1].I didn't get this.Can any of you please explain.
Regards
Sudha
Bin Zhao
Ranch Hand

Joined: Oct 04, 2000
Posts: 73
You should know the evaluation order of an expression.
this order is from left to right.
use your example:
1. int [ ] a = new int[ 2 ];
2. int b = 1;
3. a[b] = b = 0;
at line 3,first a[b] is evaluated.now b is 1,so a[b] is a[1].
now you can see it's a[1] that is being assighned a value.
hope this may clear your doubt.
Bin Zhao
madhumathi k
Greenhorn

Joined: Nov 01, 2000
Posts: 5
[] gets precedence over =
rajesh dalvi
Greenhorn

Joined: Oct 24, 2000
Posts: 7
This is the same example explained in RHE chapter 2.
The game is all about order of evaluatig expression and operator precedence and associativity.
In this example value will be stored in a[1] and that to 0.
final expression will become a[1] = 0;
How ?
Beacause in java all expressions are evaluated first from left to right.
so after this we will have
a -> a[1]
b -> b (just a reference)
0 -> 0 (literal)
so now expression becomes
a[1] = b = 0;
now associativity of oprators comes in to play.
= operator is evaluated right to left
so expression reduces to
a[1] = b (where now b = 0)
so finaly 0 is assigned to a[1]
Originally posted by sudha:
[B]Hi
This is from deepak's mock
Consider the following code :

1. int [ ] a = new int[ 2 ];
2. int b = 1;
3. a[b] = b = 0;

Value will be stored in what element of array a.
A.a[1]
B.none
C.a[0]
Answer given was a[1]
How can it be a[1].I didn't get this.Can any of you please explain.
Regards
Sudha

Adeeshwar Singhal
Greenhorn

Joined: Oct 18, 2000
Posts: 28
That was the wonderful explanation Rajesh. Where did u get it. Actually I want to delve a bit more into evaluation of expressions.
I knew the funda but couldn't get it documented anywhere.
Hope to hear from u soon.

Hey guys!! come and have some cups of HOT JAVA........<BR>Adeesh
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
sudha and madhumathi k,
may I ask you to register with proper names?
You can read this post for more details. We are glad to see you here, just a little formality´┐Ż

Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: From deepak's mock