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 | c | Prints: long,float,double | 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. |
| 2 | b | Prints: false,true |
|
| 3 | d | Prints: AZA | Instances of type String are immutable. In method m1, the replace method returns a new instance of type String that contains the value Y, but the String instance referenced by s1 remains unchanged. The original value, A, is printed in method m1. In method m2, the replace method returns a new instance of type String that contains the value Z, and a reference to the new instance is assigned to reference variable s1. The new value, Z, is printed in method m2. In the main method, a copy of the reference value contained by the reference variable s1 is passed as an argument to methods m1 and m2. Since String instances are immutable, methods m1 and m2 can not change the original String instance that is declared in the main method. Since references are passed by value, methods m1 and m2 can not change the reference variable declared in the main method. Regardless of anything that happens in methods m1 and m2, the reference variable s1 that is declared in the main method will continue to reference the original String instance that contains the value A. |
| 4 | d | Prints: double |
The
|
| 5 | b d | Compile-time error at 2. Compile-time error at 4. |
The
|
| 6 | d | Prints: ABAbAb | Instances of type String are immutable. In method m1, the toUpperCase method returns a new instance of type String that contains the value AB, and a reference to the new instance is assigned to reference variable s1. The new value, AB, is printed in method m1. In method m2, the toLowerCase method returns a new instance of type String that contains the value ab, but the String instance referenced by s1 remains unchanged. The original value, Ab, is printed in method m2. In the main method, a copy of the reference value contained by the reference variable s1 is passed as an argument to methods m1 and m2. Since String instances are immutable, methods m1 and m2 can not change the original String instance that is declared in the main method. Since references are passed by value, methods m1 and m2 can not change the reference variable declared in the main method. Regardless of anything that happens in methods m1 and m2, the reference variable s1 that is declared in the main method will continue to reference the original String instance that contains the value Ab. |
| 7 | e | Compile-time error |
The
|
| 8 | d | Prints: 1212 | The reference variable s3 is initialized with a reference to an instance of type String containing the value "12". The expression s1 += s2 is equivalent to the expression s1 = s1 + s2. Further simplification produces s1 = "1" + "2" = "12". The expression s3 += s1 is equivalent to the expression s3 = "12" + "12" = "1212". |
| 9 | d | Prints: double,double,double |
The
|
| 10 | b | Prints: false,true | The reference variable s1 is initialized with a reference to an instance of type String containing the value "ABCDEFG". The reference variable s2 is initialized with a reference to an instance of type String containing the value "EFGHIJ". The expression s3 = s1.substring(4,7) initializes the reference variable s3 with a reference to a unique instance of type String containing the value "EFG". The expression s4 = s2.substring(0,3) initializes the reference variable s4 with a reference to a unique instance of type String containing the value "EFG". The expression s3 == s4 compares two unique reference values and produces the value false even though s3 and s4 reference two String instances that contain the same value, "EFG". The expression s3 + s4 produces a unique instance of type String containing the value "EFGEFG". Similarly, the expression s4 + s3 produces a unique instance of type String containing the value "EFGEFG". The expression (s3 + s4).equals(s4 + s3) compares the contents of two unique instances of type String that contain the value "EFGEFG". The result of the comparison is true. |
| 11 | a g h i | b1 b7 b8 b9 |
The
|
| 12 | d | Prints: "ABCDEF" | The reference variable s1 is initialized with a reference to an instance of type String containing the value "ABCDEFG". The expression s2 = s1.substring(0,3) initializes the reference variable s2 with a reference to a unique instance of type String containing the value "ABC". The expression s3 = s1.substring(4,6) creates a unique instance of type String containing the value "EF". The expression c1 = s1.charAt(3) initializes the primitive variable c1 with the value 'D'. The expression String.valueOf(c1) invokes the static valueOf method with an argument of type primitive char and value 'D'. The valueOf method creates a new instance of type String. The value contained by the new instance is "D". The expression s2.concat(String.valueOf(c1)) invokes the concat method on the instance of type String referenced by the variable s2. The instance referenced by s2 contains the value "ABC". The value contained by the argument is "D". The result of the concatenation operation is a new instance of type String containing the value "ABCD". The expression s2.concat(String.valueOf(c1)).concat(s3) invokes the concat method on the previously created instance containing the value "ABCD". The instance referenced by the argument s3 contains the value "EF". The result of the concatenation operation is a new instance of type String containing the value "ABCDEF". |
| 13 | l | None of the above |
All methods of the
|
| 14 | a d e | abs max min | |
| 15 | f | Prints: DDDLDD | The round method does not return either of the two floating point primitive types, float or double. The ceil, sin and sqrt methods return only a value of type double. The abs and max methods are able to return an int, long, float or double depending on the argument type. |
| 16 | e f | 5 6 |
The overloaded contructors for the
String
class accept
a parameter of type
String
or
StringBuffer
or an
array of
byte
or
char.
There is no constructor that
will accept a primitive
byte
or
char.
Please note that
if you want to convert a primitive to a
String
then
you can use the static
|
| 17 | e | Prints: DDDD | The floor and ceil methods are not overloaded. There is only one version of each method. The parameter type is double, and the return type is double. |
| 18 | e | Prints: AB C D EF | The trim method creates a new String instance with the leading and trailing white space removed. |
| 19 | d | Prints: 12,true | The valueOf method returns a String representation of the input parameter. The input parameter may be of type Object, char[], or any primitive type. The toString method returns a reference to the existing String instance. It does not create a new instance of the String. |