• 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: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, here is another question which I got wrong and I'm not really sure how I can work out the answer to such a question
Glenn, Mitchell. OCAJP Oracle Certified Associate Java SE 8 Programmer Practice Exams (Kindle Locations 18809-18814). Enthuware. Kindle Edition:

What happens when you try to compile and run the following class...

Select 1 option
A. It throws an OverFlowException.
B. It will print two same negative numbers.
C. It will print two different negative numbers.
D. It will print one negative and one positive number of same magnitude.
E. It will print one negative and one positive number of different magnitude.


Right, so I anwered D but the correct answer is B. I applied some basic logic here and - not knowing by heart the min value of an integer - i picked a large number say -xxxxxxxxxx and stored the value in a and then did
b = -(-xxxxxxxxxx) which gives me a positive number. But, of course, I was wrong. SO the explanation goes into a long description of bits etc, stuff that I thought it wouldn't be needed for the exam. ANy quick and dirty way to figure out what it prints without going into bits?

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason Attin wrote:
Right, so I anwered D but the correct answer is B. I applied some basic logic here and - not knowing by heart the min value of an integer - i picked a large number say -xxxxxxxxxx and stored the value in a and then did
b = -(-xxxxxxxxxx) which gives me a positive number. But, of course, I was wrong. SO the explanation goes into a long description of bits etc, stuff that I thought it wouldn't be needed for the exam. ANy quick and dirty way to figure out what it prints without going into bits?



I agree, but am not completely sure, but... yes, I highly doubt that knowledge of the Twos Complement format would be needed for the test...

The short answer is, with the Twos Complement format, the range isn't balanced. The negative of MIN_VALUE is a positive number that is larger than MAX_VALUE, and specifically, it will overflow and become MIN_VALUE again.

If you really want to know why, then I suggest doing some research on Two Complement. If you are *not* interested, and just want a "quick and dirty" way to remember, it is simple. This is the only case where this happens -- meaning, this is the only case where negating will cause an overflow.

Henry
 
Jason Attin
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
fabulous, thanks!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic