The pages of this web site are not formatted to conserve paper, but my new book (ISBN: 0-9745862-0-X) is formatted to save paper, save your printer toner, and save money. If you prefer to work my exams from printed pages, then you can save a tree, save your printer toner, and save money if you buy my new book instead of attempting to print the pages of my web site.
Today, you can find my book on the retail web site of the company that prints it and distributes it.
| No. | Answer | Remark | |
|---|---|---|---|
| 1 | b d f | new Float("A") new Float("1L") new Float("0x10") | The Float constructor is overloaded: one version accepts a primitive of type float; one accepts a primitive of type double; one accepts a String representation of a floating-point literal. The primitive char literal 'A' is converted to a float, and is accepted by the constructor that declares a parameter of type float. The String literals "NaN" and "Infinity" are accepted by the Float constructor. A sign (+ or -) is optional. The API specification states that any other String must represent a floating-point value; however, a little experimentation proves that a String is acceptable if it can be parsed as a decimal integer value. The leading 0 of an octal value is ignored, and the String is parsed as a decimal value. A String representation of a hexadecimal value is not acceptable. The String "A" does not represent a floating-point literal value; therefore, a NumberFormatException is thrown. Arguments of type String can not contain an integer type suffix, L or l. A floating-point suffix, F, f, D or d, is acceptable, but the suffix has no impact on the result. |
| 2 | h | Prints: true,true,true |
The
Boolean
class contains two
public static final Boolean
instances:
|
| 3 | c d e | new Short("+1") new Short("1.0") new Short("0x1") | The Short class has only two constructors: one accepts a primitive short; the other accepts a String. A String argument must represent an integral primitive type. A leading minus sign can be added to indicate a negative value. A leading plus sign generates a run-time error. The constructor is not able to determine the radix of the String value by examing a prefix such as 0 or 0x. The 0 prefix used to identify octal values is accepted, but the String is parsed as a decimal value. The prefix 0x generates a run-time error. A run-time error is generated if the String argument is not formatted as a decimal integer. A floating-point format results in a run-time error. |
| 4 | b | Prints: false,true | The expression b1==b2 compares the references of two instances of Byte. The result is false, because the instances are distinct. The expression b1.equals(b2) compares the contents of two instances of Byte. The result is true, because the two instances contain the same value. |
| 5 | k | None of the above | The binary representation of 256 is one bit that is set to one followed by eight bits that are set to zero. When 256 is converted to an eight bit byte value, the bit that is set to one is lost and only the bits that are set to zero remain. When 256 is converted to a short, no information is lost; so the value remains 256. |
| 6 | h | Prints: true,true,true |
NaN
is the only value that is not equal to itself. The
|
| 7 | a | Prints: long | The Long.parseLong method returns a primitive long. |
| 8 | a | Compile-time error at 1 |
The
|
| 9 | e | Prints: true,false,false | The Boolean constructor is overloaded: one version accepts a primitive boolean argument; the other accepts a String. If the String value is the word true, then the new Boolean instance will contain the value true. Both upper and lower case letters are acceptable. If the String contains any word other than true or if the reference is null, then the new instance will contain the value false. |
| 10 | d | false,true,true | The equality expression s1==s2 compares the reference values of two distinct instances of type Short. Since the instance are distinct, the equality expression is false. The expression s1.equals(s2) compares the values of two instances of type Short. Since both instances contain the value 1, the returned value is true. The expression a1==b1 compares the hash codes of two instances of Short. The result is true, because the two instances contain the same value. |
| 11 | d | Prints: true,true | The expression b1.equals(b2) compares the values of two instances of type Byte. Since both instances contain the value 1, the return value is true. The expression a==b compares the hash codes of two instances of Byte. The result is true, because the two instances contain the same value. |
| 12 | a | Prints: int primitive |
The
|
| 13 | e f g | parseDouble toString(double) valueOf | |
| 14 | g | Run-time error |
The
|
| 15 | i | Compile-time error |
The
|
| 16 | c | Prints: false,true,false | The Boolean.valueOf method is overloaded: one version accepts a primitive boolean argument; the other accepts a String. If the String value is the word true, then the new Boolean instance will contain the value true. Both upper and lower case letters are acceptable. If the String contains any word other than true or if the String reference is null, then the new instance will contain the value false. |
| 17 | b | Prints: p,p,S |
The
|
| 18 | a | Prints: -128,127 | |