| Author |
a wrapper class question
|
Angie Wu
Greenhorn
Joined: Aug 07, 2002
Posts: 10
|
|
1. class G { public static void main (String[] args) { System.out.print(Long.parseLong("11", 2)+", "); System.out.print(Long.parseLong("11", 8)+", "); System.out.print(Long.parseLong("11", 10)+", "); System.out.print(Long.parseLong("11", 16)); } } What is the result of attempting to compile and run the program? a. Prints: 2,8,10,16 b. Prints: 3,9,11,17 c. Prints: 11,11,11,11 d. Prints: 13,19,21,27 e. Compiler Error f. Runtime Error g. None of the Above The answer is b, why?
|
 |
Bishal P
Ranch Hand
Joined: Sep 06, 2002
Posts: 43
|
|
The API says parseLong
Parses the string argument as a signed long in the radix specified by the second argument.
Radix is what many people call base, meaning radix 2 is base 2 or binary. In base 2 , 11 stands for 3. I guess rest you can figure out now.
|
_ _____ _ <br />Used to be a Java Programmer but now I work on Microsoft Technologies - Word, Excel and Outlook!
|
 |
Shishio San
Ranch Hand
Joined: Aug 29, 2002
Posts: 223
|
|
Hi, Some intersting exemples
parseLong("0", 10) returns 0L parseLong("473", 10) returns 473L parseLong("-0", 10) returns 0L parseLong("-FF", 16) returns -255L parseLong("1100110", 2) returns 102L parseLong("99", 8) throws a NumberFormatException parseLong("Hazelnut", 10) throws a NumberFormatException parseLong("Hazelnut", 36) returns 1356099454469L
|
Whatever doesn't kill us ...<br />Is probably circling back for another try.<br />SCJP 1.4
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
parseLong("Hazelnut", 36) returns 1356099454469L
That one is really nuts! Do they take letters G=16, H=17, ... ? Something to play around with... -Barry
|
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
|
 |
Shishio San
Ranch Hand
Joined: Aug 29, 2002
Posts: 223
|
|
Hi Barry, That one struck me too. If you check the API though it is stated that
Parses the string argument as a signed long in the radix specified by the second argument. The characters in the string must all be digits of the specified radix (as determined by whether Character.digit(char, int) returns a nonnegative value), except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value. The resulting long value is returned. Note that neither the character L ('\u004C') nor l ('\u006C') is permitted to appear at the end of the string as a type indicator, as would be permitted in Java programming language source code - except that either L or l may appear as a digit for a radix greater than 22. An exception of type NumberFormatException is thrown if any of the following situations occurs: The first argument is null or is a string of length zero. The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX. Any character of the string is not a digit of the specified radix, except that the first character may be a minus sign '-' ('\u002d') provided that the string is longer than length 1. The value represented by the string is not a value of type long.
and the key function is Character.digit(char, int)
digit public static int digit(char ch, int radix)Returns the numeric value of the character ch in the specified radix. If the radix is not in the range MIN_RADIX <= radix <= MAX_RADIX or if the value of ch is not a valid digit in the specified radix, -1 is returned. A character is a valid digit if at least one of the following is true: The method isDigit is true of the character and the Unicode decimal digit value of the character (or its single-character decomposition) is less than the specified radix. In this case the decimal digit value is returned. The character is one of the uppercase Latin letters 'A' through 'Z' and its code is less than radix + 'A' - 10. In this case, ch - 'A' + 10 is returned. The character is one of the lowercase Latin letters 'a' through 'z' and its code is less than radix + 'a' - 10. In this case, ch - 'a' + 10 is returned.
Do you guys think that we need to know details about all the wrapper classes functions for the 1.4 exam ??
|
 |
Paul Anilprem
Enthuware Software Support
Ranch Hand
Joined: Sep 23, 2000
Posts: 2912
|
|
Originally posted by Shishio San: Do you guys think that we need to know details about all the wrapper classes functions for the 1.4 exam ??
You need to know the basic concepts regarding wrapper classes (such as, they are immutable and behavior of their equals() method ). Regarding methods, no, they do not ask such cryptic questions. You should know the basic working of the parseXXX(), xxxValue(), and toString. They do ask questions like: double d = //insert stmt here Option 1. new Double().parseDouble("12.3").doubleValue(); Option 2. Double.parseDouble("12.3").doubleValue(); Option 3. new Double(Double.parseDouble("12.3")).doubleValue(); So you do not have to worry about the radix etc. As long as you understand the basic stuff, you should be ok.
|
Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
|
 |
Shishio San
Ranch Hand
Joined: Aug 29, 2002
Posts: 223
|
|
Thx Paul
|
 |
 |
|
|
subject: a wrapper class question
|
|
|