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 | b | Compile-time error at 1 |
At line 1, the invocation of the
|
| 2 | c | Compile-time error |
The
|
| 3 | g | Prints: true,true,false |
|
| 4 | b d e | Long.parseLong("1L") Long.parseLong("0x10") Long.parseLong("1.0") |
|
| 5 | g | valueOf | |
| 6 | b | Prints: A B C | Strings are immutable. No method will change the contents of a String instance. Some String methods such as concat, replace, substring and trim will return a new String instance with the desired modifications. In this case, the String instances returned by the methods trim and concat are ignored. |
| 7 | b | Compile-time error at 1 |
At line 1, the invocation of the
|
| 8 | e | Prints: true,false,false |
|
| 9 | b d e | Long.parseLong("+1") Long.parseLong("1L") Long.parseLong("1.0") |
|
| 10 | e f | parseDouble valueOf | |
| 11 | g | Compile-time error | The StringBuffer class has an append method, but the String class does not. A compile-time error is generated due to the attempt to invoke the append method on an instance of type String. |
| 12 | c | Prints: int,int,int | The Math class declares four versions of the min method; each declares a pair of parameters of the same type. The parameter pair can be of type int, long, float or double. The return type is the same as the argument types. At run-time, the arguments might not match the declared parameter types; so one argument or both might require an implicit conversion to an acceptable type. If both arguments are of type byte, short or char, then both will be promoted to type int. If only one argument is of type byte, short or char, then it will be promoted to the type of the other argument. If both arguments are of type int, long, float or double but the types differ, then a primitive widening conversion will be applied to one of the two arguments. |
| 13 | a b | intValue parseInt |
|
| 14 | e g | parseDouble valueOf | |
| 15 | d | Prints: int,int,long | The Math class declares four versions of the min method; each declares a pair of parameters of the same type. The parameter pair can be of type int, long, float or double. The return type is the same as the argument types. At run-time, the arguments might not match the declared parameter types; so one argument or both might require an implicit conversion to an acceptable type. If both arguments are of type byte, short or char, then both will be promoted to type int. If only one argument is of type byte, short or char, then it will be promoted to the type of the other argument. If both arguments are of type int, long, float or double but the types differ, then a primitive widening conversion will be applied to one of the two arguments. |
| 16 | c | Prints: true,false |
|
| 17 | f | Run-time error | An argument of type String must represent a floating-point value. The argument "0x10" represents a hexadecimal integer literal; so a NumberFormatException would be thrown at run-time. |
| 18 | e | Compile-time error | The StringBuffer class has an insert method, but the String class does not. A compile-time error is generated due to the attempt to invoke the insert method on an instance of type String. |
| 19 | b | Prints: false,false,true | At run-time, the expression a+b is evaluated four times. Each evaluation produces a new String instance containing the value "AB". Each of the four instances has a unique reference. If any two of the four instances appear as the operands of the equality operator, then the result is always false. The left and right operands of the equality expression (a+b)==(a+b) reference unique String instances. Since the two references are not the same, the equality expression produces the value false. Similarly, the expression c==d produces the value false. Since the contents of the String instances referenced by c and d are the same, the method invocation expression c.equals(d) produces the value true. |