java题(9)
Question: 36
Given:
12. NumberFormat nf = NumberFormat.getInstance();
13. nf.setMaximumFractionDigits(4);
14. nf.setMinimumFractionDigits(2);
15. String a = nf.format(3.1415926);
16. String b = nf.format(2);
Which two statements are true about the result if the default locale is Locale.US? (Choose two.)
A. The value of b is 2.
B. The value of a is 3.14.
C. The value of b is 2.00.
D. The value of a is 3.141.
E. The value of a is 3.1415.
F. The value of a is 3.1416.
G. The value of b is 2.0000.
Answer: C, F
题解:
java.util.Locale
这里使用Locale对象的第三种表示方法
Country Code
====================
China CH
Canada CA
France FR
Japan JP
Germany DE
这题倒不用怎么看locale,主要是13和14行,设置了小数最多是4位,最少是两位。且是四舍五入的。
Question: 37
Given:
12. import java.io.*;
13. public class Forest implements Serializable {
14. private Tree tree = new Tree();
15. public static void main(String [] args) {
16. Forest f = new Forest();
17. try {
18. FileOutputStream fs = new FileOutputStream("Forest.ser");
19. ObjectOutputStream os = new ObjectOutputStream(fs);
20. os.writeObject(f); os.close();
21. } catch (Exception ex) { ex.printStackTrace(); }
22. } }
23.
24. class Tree { }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. An instance of Forest is serialized.
D. An instance of Forest and an instance of Tree are both serialized.
Answer: B
如果把Tree也实现序列化接口implements Serializable就OK了
把14. private Tree tree = new Tree();改成private Tree tree;也行。
如果一个可序列化类有多个父类(包括直接或间接父类),则该类的所有父类要么是可序列化的,要么有无参数的构造器——否则会反序列化时抛出InvalidClassException异常。
当程序创建子类实例时,系统会隐式地为它的所有父类都创建实例(并建立和此子类实例的关联)!当我们反序列化某个子类的实例时,反序列化机制需要恢复其关联的父类实例,恢复这些父类实例有两种方式:
A.使用反序列化机制。
B.使用父类无参数的构造器。
在上面两种方式中,反序列化机制优先采用第一种机制。如果某个父类既不可序列化(没有implements Serializable),即不能使用第一种机制;也没有提供无参数的构造器,则不可采用第二种机制,则反序列化该子类实例将抛出异常。
Question: 38
Assuming that the serializeBanana() and the deserializeBanana() methods will
correctly use Java serialization and given:
13. import java.io.*;
14. class Food implements Serializable {int good = 3;}
15. class Fruit extends Food {int juice = 5;}
16. public class Banana extends Fruit {
17. int yellow = 4;
18. public static void main(String [] args) {
19. Banana b = new Banana(); Banana b2 = new Banana();
20. b.serializeBanana(b); // assume correct serialization
21. b2 = b.deserializeBanana(); // assume correct
22. System.out.println("restore "+b2.yellow+ b2.juice+b2.good);
24. }
25. // more Banana methods go here 50. }
What is the result?
A. restore 400
B. restore 403
C. restore 453
D. Compilation fails.
E. An exception is thrown at runtime.
Answer: C
就是把对象的数值取出来呗。好了,说一个刚开始学java时候的疑惑,为啥会是453不是12呢?加了一个字符串restore之后,后面的会按照字符串相加起来。
Question: 39
Given this method in a class:
21. public String toString() {
22. StringBuffer buffer = new StringBuffer();
23. buffer.append('<');
24. buffer.append(this.name);
25. buffer.append('>');
26. return buffer.toString();
27. }
Which statement is true?
A. This code is NOT thread-safe.
B. The programmer can replace StringBuffer with StringBuilder with no other changes.
C. This code will perform poorly. For better performance, the code should be rewritten:
return "<" + this.name + ">";
D. This code will perform well and converting the code to use StringBuilder will not enhance the performance.
Answer: B
StringBuffer是线性安全的,StringBuilder是非线性安全的。其实这个程序是线性安全的,另外,C是完全没有必要的。另外非线性安全的要比线性安全的效率高,那么D也是错的。