Java Reference
In-Depth Information
8
C. E is incorrect because the Locale.US is specifi ed for fmt , so the output does not
depend on the JVM's environment or platform. The currency format rounds decimals
up to two decimal places, so 99.999 is rounded up to 100.00 and printed in the U.S.
locale. The output is $100.00 , and therefore the answer is C.
9
C. The code compiles fi ne, so E is incorrect. The StringBuilder contains aaa after the
append(“aaa“) method call. The insert(1, “bb“) inserts bb at position 1, resulting in
abbaa . The insert(4, “ccc“) inserts ccc at position 4, resulting in abbaccca . There-
fore, the answer is C.
10. B. The code compiles and runs fi ne, so D and E are incorrect. The File object repre-
sents the fi lename data.txt . The constructor of MyReader chains a FileReader and
BufferedReader to data.txt . The go method reads in the contents of data.txt one line
at a time and prints each character without a linefeed, so the output is HELLO and the
answer is B.
11. B, C, and D. The data to be output consists of more than strings or characters, so
writer classes are not appropriate. FileOutputStream is needed to write to the fi le.
BufferedOutputStream is needed to buffer the data, and the best choice for writing
various primitive types and strings is DataOutputStream , so the answer is B, C, and D.
12. A . The format specifi er formats d with a width of 4 and precision 2, which results in
2.73 . The string is almost is printed, followed by a space. (There is a space between
%s and %d in the format specifi er.) The value of x is printed as 3 , and then a linefeed
( %n ). The result is 2.73 is almost 3 , so the answer is A.
13. D. The ”2$” portion of ”%2$d” denotes the second argument to be formatted, which is
the 5. Therefore, 5 is output twice and the 10 does not appear in the result. The output is 5
is bigger than 5 and the answer is D.
14. E. The code compiles fi ne, so C and D are incorrect. Line B is a false statement because
the access specifi er of a fi eld does not have an effect on serialization. An attempt is
made on line 20 to serialize an Employee object, but the Employee class does not imple-
ment the Serializable interface. Therefore, a NotSerialiableException is thrown, so
A is false and E is true. Therefore, the answer is E.
15. B and D. Line 5 compiles fi ne and is how you obtain a reference to the Console object.
The System.console() method might return null if the environment does not have
a console, so D is true. Line 6 does not compile because the return value of readPassword
is a char array, not a String , so B is true. E is false; the fi rst argument of readPassword is
a format specifi er. Therefore, the answer is B and D.
16. C. The code compiles fi ne and the A class properly implements Serialiable , so E is
incorrect. D is incorrect because no constructors are invoked on a class being deserial-
ized. (A nonserializable parent class constructor may be invoked.) Instantiating A on
line 6 invokes the constructor on line 21 and A is printed. After the object is deserial-
ized, line 14 prints the value of ref2.a , which is 12 , so the fi nal output is A12 and
therefore the answer is C.
Search WWH ::




Custom Search