This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes jquest question!!!!!!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "jquest question!!!!!!!!" Watch "jquest question!!!!!!!!" New topic

jquest question!!!!!!!!

sunil kathuria

Joined: Jun 06, 2001
Posts: 24
What will be the result of attempt to compile and the run the following code
1. public class ADirtyOne {
2. public static void main(String args[]) {
3. System.out.println(Math.abs(Integer.MIN_VALUE));
4. }
5. }
Select one correct answer
a) causes compilation error
b) causes no error and value printed on the
screen is less than zero
c) causes no error and value printed on the screen is one more
than Integer.MAX_VALUE
d) will throw runtimeexception
answer given is --->b
i think option c is also right as (Integer.MAX_VALUE+1) will produce
-2147483648 which is less than zero
pl.confirm me the right answer and correct me if wrong
thanks in adv.
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Did you TRY it?

"JavaRanch, where the deer and the Certified play" - David O'Meara
Timothy Willard

Joined: Apr 03, 2001
Posts: 25
I believe you should try to execute these lines of code. I received the following results.
System.out.println(Math.abs(Integer.MIN_VALUE)); //-2147483648
System.out.println(Math.abs(Integer.MIN_VALUE+1)); //2147483648
Hope this helps!
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Can someone explain why Math.abs () failed to deliver the absolute value?
Paul Michael
Ranch Hand

Joined: Jul 02, 2001
Posts: 697
hi Cameron... it's probably because the number of negative values an int can hold is NOT equal to the number of the positive values it can contain. in this case it's one more than the number of positive values.

SCJP 1.2 (89%), SCWCD 1.3 (94%), IBM 486 (90%), SCJA Beta (96%), SCEA (91% / 77%), SCEA 5 P1 (77%), SCBCD 5 (85%)
Anshul Manisha
Ranch Hand

Joined: Apr 17, 2001
Posts: 74
Math.abs() performed as it is supposed to
From Java API Documentation abs(int i) method in math class -
Returns the absolute value of an int value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned.
Note that if the argument is equal to the value of Integer.MIN_VALUE, the most negative representable int value, the result is that same value, which is negative.

AM<BR> <A HREF="" rel="nofollow"></A>
sunil kathuria

Joined: Jun 06, 2001
Posts: 24
pl. read the option 3 again
it says that --> causes no error and value printed on the screen is one more than Integer.MAX_VALUE
that means if we add 1 to Integer.MAX_VALUE
or System.out.println((Math.abs(Integer.MAX_VALUE+1));
which gives -2147483648 which is less than zero
so pl. confirm me the right answer

Chuck Lorenz

Joined: Jul 06, 2001
Posts: 6
"value printed on the screen is one more than Integer.MAX_VALUE"
...try interpretting the phrase as
System.out.println((Math.abs(Integer.MAX_VALUE)); + 1
The range of int is -2147483648 to 2147483647.
Notice that the absolute value of Integer.MIN_VALUE is one greater then the absolute value of Integer.MAX_VALUE.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: jquest question!!!!!!!!