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 cartridge, save a loose-leaf binder, and save money. If you prefer to work my exams from printed pages, then give your printer a rest and buy my new book.
Today, you can find my book at amazon.com.
| No. | Answer | Remark | |
|---|---|---|---|
| 1 | e | Compile-time error |
There are two versions of the
|
| 2 | e | Compile-time error at 3 | The Byte class has only two constructors: one accepts a primitive byte; the other accepts a String. The argument that appears in the class instance creation expression new Byte(1) is of type int, but the compiler will not implicitly narrow the int to a byte. At line 1, the assignment expression primitiveByte = 1 causes the compiler to implicitly narrow a literal of type int to type byte. The compiler will implicitly do a narrowing conversion for an assignment expression if the right hand operand is a compile-time constant of type byte, short, char or int and the value falls within the range of the variable on the left and the variable is of type byte, short, or char. For that reason, a constant int expression that is equal to 1 may be assigned to a byte without an explicit cast. The same is not true for the parameters of a method or constructor. The designers of the Java programming language felt that implicit narrowing conversions of method and constructor arguments would add unnecessary complexities to the process of resolving overloaded method calls. |
| 3 | a | Prints: 5 | The Integer class has only two constructors: one accepts a primitive int, and the other accepts a String. If the argument is of type byte, short or char, then it is implicitly promoted to type int. |
| 4 | c | Compile-time error | Long has two constructors: one accepts an argument of type primitive long; the other accepts an argument of type String. The class instance creation expression new Long(primitiveFloat) generates a compile-time error, because the compiler will not implicitly apply a primitive narrowing conversion to the argument. |
| 5 | b | Prints: 4.0 | The Double constructor is overloaded: one accepts an argument of type primitive double; the other accepts an argument of type String. |
| 6 | k | None of the above | 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. |
| 7 | c d | 3 4 | The boolean literals true and false are written with lower case letters; upper case letters produce a compile-time error. A String representation of a boolean literal is acceptable in both upper and lower case letters. |
| 8 | a b c d | new Short(1) new Short('1') new Short('b' - 'a') new Short((short)1 - (short)2) | The Short class has only two constructors: one accepts a primitive short; the other accepts a String. The argument that appears in the class instance creation expression new Short(1) is of type int, but the compiler will not implicitly narrow the int to a short. The argument that appears in the class instance creation expression new Short('1') is of type char, but the compiler will not implicitly convert the char to a short. The argument that appears in the class instance creation expression new Short('b' - 'a') is of type int, but the compiler will not implicitly narrow the int to a short. The argument that appears in the class instance creation expression new Short((short)1 - (short)2) is of type int, but the compiler will not implicitly narrow the int to a short. If both operands of a binary arithmetic expression are of type byte, char or short; then both are implicitly widened to type int, and the result of the expression is of type int. |
| 9 | d | Prints: 1,2 |
The
|
| 10 | b c e | Compile-time error at 1 Compile-time error at 2 Compile-time error at 4 | The Byte class has only two constructors: one accepts a primitive byte; the other accepts a String. The argument that appears in the class instance creation expression new Byte(1) is of type int, but the compiler will not implicitly narrow it to type byte. The argument that appears in the class instance creation expression new Byte('2') is of type char, but the compiler will not implicitly narrow it to type byte. At line 4, the result of the additive expression is of type int, but the variable is of type byte. The assignment expression generates a compile-time error. |
| 11 | c | Compile-time error | The Integer class has only two constructors: one accepts a primitive int, and the other accepts a String. The class instance creation expression new Integer(primitiveLong) generates a compile-time error, because the compiler will not apply an implicit narrowing conversion to the argument. For the same reason, the class instance creation expression new Integer(primitiveFloat) generates a compile-time error. |
| 12 | e | Compile-time error | Long has two constructors: one accepts an argument of type primitive long; the other accepts an argument of type String. The class instance creation expression new Long(i1) generates a compile-time error, because there is no constructor that accepts a reference to an instance of type Long. |
| 13 | c | Compile-time error | The Double constructor is overloaded: one accepts an argument of type primitive double; the other accepts an argument of type String. There is no constructor that accepts a reference to an instance of type Double. |
| 14 | n | Prints: 1.0,8.0,16.0,1.0,1.0,1.0 | 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. All numeric values can be promoted to type double; so all numeric values are accepted by the float constructor. |
| 15 | d | Prints: false,true,true |
Four instances of type
Boolean
containing the value
true
are created. The equality expression
b1==b2
evaluates to
false,
because the unique instances of type
Boolean
have different reference values.
The instance method
|
| 16 | f | None of the above | None of the String arguments would generate a compile-time error, but two would generate a run-time error. The argument 1.0 would generate a run-time error, because a floating-point value is not acceptable. The argument 0x1 would generate a run-time error, because a hexadecimal integer literal is not acceptable. The argument must be a decimal integer literal. The leading 0 of the argument 011 is ignored and the argument is interpreted as a decimal integer literal. |
| 17 | a | Prints: false,false |
String
instances are
immutable.
String
methods such as
|
| 18 | a | Prints: false,false |
|
| 19 | f | Prints: BDE | The substring method returns a new String instance that is a substring of the original String instance. The single parameter form of the substring method creates a new String that begins at the index specified by the argument value. The two parameter form creates a substring that starts at the index of the first parameter and ends at the index of the second parameter. The character at the start index is included in the substring, but the character at the end index is not. |
| 20 | c | Prints: true,false |
|