wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes operator assignment  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "operator assignment  " Watch "operator assignment  " New topic
Author

operator assignment

velan vel
Ranch Hand

Joined: Nov 15, 2005
Posts: 137
hai ranchers
can any one expalin the ans for this question

here ans is c


1. public class Q10
2. {
3. public static void main(String[] args)
4. {
5. int i=3;
6. System.out.println(i*=2 + i++);
7. }
8. }
a Program compiles correctly and prints 7 when executed.
b Program compiles correctly and prints 11 when executed.
c Program compiles correctly and prints 15 when executed.
d None of above.
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
i*=2 + i++ ---> evaluation takes place as follows

i=i*(2+ i++)

2+3 is 5. and i is incremented to 4 thus i=4.
thus we now have
i=4*5
thus i=20.

correct me if i am wrong

niranjan


SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 479
no nirajan....

Actual answer is C

1. public class Q10
2. {
3. public static void main(String[] args)
4. {
5. int i=3;
6. System.out.println(i*=2 + i++);
7. }
8. }
a Program compiles correctly and prints 7 when executed.
b Program compiles correctly and prints 11 when executed.
c Program compiles correctly and prints 15 when executed.
d None of above.

Here how it goes

i++ means assign the orginal value of i to it and then later increment it .
so
i*=2 + i++)
i=i*(2+i++)
i=3*(2+3)
i=3*5
i=15.

Had it been like this
i*=(2 + ++i)
i=i*(2+ ++i)/// because first increment the original value of i and then assign it to it.
i=3*(2+4)
i=3*6
i=18.

Hope you got it
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: operator assignment