• 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

Dan's Mock- Math.abs()

 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Section8 Exam B Question 14
Which of the following statements are true in terms of the java.lang.Math.abs method?
a. Four overloaded versions of abs exist.
b. An ArithmeticException is declared in the throws clause.
c. The type of the return value depends on the type of the argument.
d. The returned value is always of a floating-point primitive type.
e. If the argument is greater than or equal to zero then the returned value is equal to the argument.
f. If the argument, arg, is less than zero then the returned value is -arg.
g. None of the Above


Given answers are A, C,E, F


Four overloaded versions of abs exist. The type of the return value depends on the type of the argument. If the argument is greater than or equal to zero then the returned value is equal to the argument. If the argument, arg, is less than zero then the returned value is -arg.


I totally agree with the first three answers. But answer F seems to be wrong.
Here is a small test... Try to print the following and you get true.
Math.abs(Integer.MIN_VALUE)==Integer.MIN_VALUE);
Dan - Any comments?
[ March 11, 2003: Message edited by: Sarma Lolla ]
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
f. If the argument, arg, is less than zero then the returned value is -arg.
-Integer.MIN_VALUE equals Integer.MIN_VALUE, so
Math.abs(Integer.MIN_VALUE) equals -Integer.MIN_VALUE
Math.abs(Integer.MIN_VALUE) equals Integer.MIN_VALUE


The Java programming language uses two's-complement representation for integers, and the range of two's-complement values is not symmetric, so negation of the maximum negative int or long results in that same maximum negative number. Overflow occurs in this case, but no exception is thrown. JLS 15.15.4

 
Sarma Lolla
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marlene,
I should agree that you are right. The following line prints true. So answer F is perfect.
Math.abs(Integer.MIN_VALUE) == -Integer.MIN_VALUE
Thanks.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic