• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

operator!

 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you have time, please answer the following questions, and tell me if you know the answer right away or need to write a small program to get the ans...
int a = 1;
a = (a++) + (a++);
//a = ?
int a = 2;
a = (a=4) + a
//a = ?
int a = 2;
a = a + (a=4)
//a = ?
reason, pls.
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answers are 3, 8, and 6. I was able to deduce the results without need of a program, but I wrote a program to verify the results.
The reason for my correct answers: JavaRanch bartenders are required to answer questions with unfailing certitude. One mistake and I'm toast, so I can't afford to be wrong.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ernest,
Ans 1--->3
Ans 2--->8
Ans 3--->6
And I have done it without running the code.
Regards
Gurpreet Sachdeva
For Mock Exams, FAQ and some useful information about Bitshift operator, inner classes, garbage collection,etc please visit: http://www.go4java.20m.com
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Case 1:
post increment operator. So a value is used first and then incremented. This happens in both the side of +
Case 2:
+ is binary so left to right
a=4 takes place first, so its 8
Case 3:
Again + binary so left to right
so a gets 2 and then 4 so its 6
Enjoy
Ragu
 
Ernest Lee
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you guys are great..
i thought these questions will cause some confusion...
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ragu Sivaraman:
Case 3:
Again + binary so left to right
so a gets 2 and then 4 so its 6
Enjoy
Ragu[/B]


Hi raghu
i have question ... as in case 3 (a=4) is in brackets then it should be executed first.
so result should be 8??
and Micheal .. can you also give explanation(I will be more satisfied if you give reason for all three)

------------------
Regards
Ravish
 
Michael Ernest
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, it's bad enough I have to be correct, now I have to explain myself?
In the first, the post-increment operator shows how it operates even within one statement. The original value, 1, gets loaded, then a is incremented to 2. 2 is then added to 1, and summed, following which it is incremented again. The result of the sum is 3, because the second increment occurs last.
The second one's the easiest to see; RHS assignment precedes addition. a gets set to 4, then added to another a. Voila, 8.
In case 3, it's the reverse, the addition comes first, so it's 2 and then the re-assigned four.
These last two examples illustrate one important concept. If you're going to write statements like this, you're only making more work for yourself.

------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic