QUESTION 1
Given a pre-generics implementation of a method:
给定下列还未使用泛型的代码:
11. public static int sum(List list) {
12. int sum = 0;
13. for ( Iterator iter = list.iterator(); iter.hasNext(); ) {
14. int i = ((Integer)iter.next()).intValue();
15. sum += i;
16. }
17. return sum;
18. }
What three changes allow the class to be used with generics and avoid an unchecked warning? (Choosethree.)
哪三个改变可以使此类使用泛型,并且没有黄色警告
A. Remove line 14. 删除14行
B. Replace line 14 with "int i = iter.next();". 替换14行
C. Replace line 13 with "for (int i : intList) {".
D. Replace line 13 with "for (Iterator iter : intList) {".
E. Replace the method declaration with "sum(List
F. Replace the method declaration with "sum(List
Answer: ACF
知识点:
1)泛型必须使用包装类,而不是基本数据类型,所以选F
2)增强for,使用同样的数据类型i,遍历list中的数据,选C
3)同上,增强for,无需14行的代码
QUESTION 2
A programmer has an algorithm that requires a java.util.List that provides an efficient implementation of add(0, object), but does NOT need to support quick random access.
需要一个List算法,提供有效的添加,但是不支持快速随机查找
What supports these requirements?
以下哪个满足上述条件。
A. java.util.Queue
B. java.util.ArrayList
C. java.util.LinearList
D. java.util.LinkedList
Answer: D
知识点:
1.随机插入、随机删除操作中,用TreeList效率最高;
2.在只需要追加、迭代的环境下,LinkedList效率最高;
3.平均效率来讲,ArrayList相对平衡,但如果海量随机操作,还是会造成性能瓶颈,随机访问效率高;
4.CopyOnWriteArrayList因为线程安全的原因,致使性能降低很多,所以慎用;
5.Vector没有传说中那么低的效率;
6.让Stack来做List 的事可以,不过语义上Stack 不应该做过多的List 的事情;
7.在排序中,ArrayList具有最好的性能,TreeList均性能也不错,LinkedList
的排序效率受元素初始状态的影响很大。
8.各种List间转换几乎没有时间损耗。
QUESTION 3
Given:
11. // insert code here //在此处添加代码
12. private N min, max;
13. public N getMin() { return min; }
14. public N getMax() { return max; }
15. public void add(N added) {
16. if (min == null || added.doubleValue() < min.doubleValue())
17. min = added;
18. if (max == null || added.doubleValue() > max.doubleValue())
19. max = added;
20. }
21. }
21. }
Which two, inserted at line 11, will allow the code to compile? (Choose two.)
哪两行代码添加到11行,可以是代码通过编译
A. public class MinMax> {
B. public class MinMax extends Number> {
C. public class MinMax
D. public class MinMax
E. public class MinMax extends Object> {
F. public class MinMax
Answer: DF
知识点:泛型基础知识
QUESTION 4
Given:
12. import java.util.*;
13. public class Explorer2 {
14. public static void main(String[] args) {
15. TreeSet
16. TreeSet
17. for(int i = 606; i < 613; i++)
18. if(i%2 == 0) s.add(i);
19. subs = (TreeSet)s.subSet(608, true, 611, true);
20. s.add(629);
21. System.out.println(s + " " + subs);
22. }
23. }
What is the result?
结果是什么
A. Compilation fails. //编译错误
B. An exception is thrown at runtime.// 运行时抛出异常
C. [608, 610, 612, 629] [608, 610]
D. [608, 610, 612, 629] [608, 610, 629]
E. [606, 608, 610, 612, 629] [608, 610]
F. [606, 608, 610, 612, 629] [608, 610, 629]
Answer: E
知识点:
1)TreeSet是一个有序集合,TreeSet中的元素将按照升序排列,缺省是按照自然排序进行排列,意味着TreeSet中的元素要实现Comparable接口。或者有一个自定义的比较器。
2)subs = (TreeSet)s.subSet(608,true,611,true);
取从608开始到611结束的子集合,包括608,也包括611,但是集合中没有611,所以只有608, 610
QUESTION 5
Given:
1. public class Score implements Comparable
2. private int wins, losses;
3. public Score(int w, int l) { wins = w; losses = l; }
4. public int getWins() { return wins; }
5. public int getLosses() { return losses; }
6. public String toString() {
7. return "<" + wins + "," + losses + ">";
8. }
9. // insert code here
10. }
Which method will complete this class?
下列哪个方法可以在第9行补充完此类
A. public int compareTo(Object o){/*more code here*/}
B. public int compareTo(Score other){/*more code here*/}
C. public int compare(Score s1,Score s2){/*more code here*/}
D. public int compare(Object o1,Object o2){/*more code here*/}
Answer: B
知识点:实现Comparable接口,需要实现CompareTo方法,并且泛型类型一致或更小
QUESTION 6
Given:
11. public class Person {
12. private name;
13. public Person(String name) {
14. this.name = name;
15. }
16. public int hashCode() {
17. return 420;
18. }
19. }
Which statement is true?
下列哪条陈述正确
A. The time to find the value from HashMap with a Person key depends on the size of the map.
从HashMap中按照Person键查找值的时间依赖于map的大小
B. Deleting a Person key from a HashMap will delete all map entries for all keys of type Person.
从HashMap中按照Person键删除,会删除所有含此Person键的entry
C. Inserting a second Person object into a HashSet will cause the first Person object to be removed as a duplicate.
Person键相同,插入第二个值时,HashSet会删除第一个值,因为重复
D. The time to determine whether a Person object is contained in a HashSet is constant and does NOT depend on the size of the map.
查找一个Person对象是否存在HashSet中的时间不依赖于此map的大小
Answer: A
QUESTION 7
Given:
5. import java.util.*;
6. public class SortOf {
7. public static void main(String[] args) {
8. ArrayList
9. a.add(1); a.add(5); a.add(3);
11. Collections.sort(a);
12. a.add(2);
13. Collections.reverse(a);
14. System.out.println(a);
15. }
16. }
What is the result?
结果是什么
A. [1, 2, 3, 5]
B. [2, 1, 3, 5]
C. [2, 5, 3, 1]
D. [5, 3, 2, 1]
E. [1, 3, 5, 2]
F. Compilation fails.//编译失败
G. An exception is thrown at runtime.//运行时异常
Answer: C
知识点:Collections.sort()方法默认升序,所以开始时[1, 3, 5]
a.add(2);后,变为[1, 3, 5, 2]
Collections.reverse(a);倒置前必须排序,而2是排序后添加的,所以选C
QUESTION 8
Given
11. public interface Status {
12. /* insert code here */ int MY_VALUE = 10;
13. }
Which three are valid on line 12?(Choose three.)
在12行可以添加哪三个代码
A. final
B. static
C. native
D. public
E. private
F. abstract
G. protected
Answer: ABD
知识点:接口的属性必须是public static final, 所以选ABD
QUESTION 9
Given:
5. class Atom {
6. Atom() { System.out.print("atom "); }
7. }
8. class Rock extends Atom {
9. Rock(String type) { System.out.print(type); }
10. }
11. public class Mountain extends Rock {
12. Mountain() {
13. super("granite ");
14. new Rock("granite ");
15. }
16. public static vvoid main(String[] a) { new Mountain(); }
17. }
What is the result?
此段代码的运行结果是什么
A. Compilation fails.
B. atom granite
C. granite granite
D. atom granite granite
E. An exception is thrown at runtime.
F. atom granite atom granite
Answer: F
知识点:先加载父类,然后加载父类构造方法,再加载子类,然后子类的构造方法,所以new Mountain();里的super("granite ");是执行Atom然后调用Rock生成grantie;之后调用 new Mountain();的 new Rock("granite ");,执行步骤与上一步相同,所以选F
QUESTION 10
Click the Exhibit button. Which three statements are true? (Choose three.)
点击Exhibit按钮,哪三个陈述正确?
A. Compilation fails.//编译失败
B. The code compiles and the output is 2.//输出为2
C. If lines 16, 17 and 18 were removed, compilation would fail.
D. If lines 24, 25 and 26 were removed, compilation would fail.
E. If lines 16, 17 and 18 were removed, the code would compile and the output would be 2.
F. If lines 24, 25 and 26 were removed, the code would compile and the output would be 1.
Answer: BEF
知识点:内部优先,所以25行优先级最高,其次才是17行
QUESTION 11
Given:
10. class Line {
11. public class Point { public int x,y;}
12. public Point getPoint() { return new Point(); }
13. }
14. class Triangle {
15. public Triangle() {
16. // insert code here
17. }
18. }
Which code, inserted at line 16, correctly retrieves a local instance of a Point object?
16行插入哪段代码,可以正确得倒Point对象的本地实例
A. Point p = Line.getPoint();
B. Line.Point p = Line.getPoint();
C. Point p = (new Line()).getPoint();
D. Line.Point p = (new Line()).getPoint();
Answer: D
知识点:实例内部类,引用时必须先构建外部类,而生成对象也必须先构建外部类
QUESTION 12
Given:
11. class Alpha {
12. public void foo() { System.out.print("Afoo "); }
13. }
14. public class Beta extends Alpha {
15. public void foo() { System.out.print("Bfoo "); }
16. public static void main(String[] args) {
17. Alpha a = new Beta();
18. Beta b = (Beta)a;
19. a.foo();
20. b.foo();
21. }
22. }
What is the result?
本段代码运行结果是什么
A. Afoo Afoo
B. Afoo Bfoo
C. Bfoo Afoo
D. Bfoo Bfoo
E. Compilation fails.
F. An exception is thrown at runtime.
Answer: D
知识点:
1)17行多态,运行时看等号右边,
2)18行同上
QUESTION 13
Click the Exhibit button. Which statement is true about the classes and interfaces in the exhibit?
点击Exhibit 按钮时,关于其中的类与接口,哪条语句陈述正确
A. Compilation will succeed for all classes and interfaces.
B. Compilation of class C will fail because of an error in line 2.
C. Compilation of class C will fail because of an error in line 6.
D. Compilation of class AImpl will fail because of an error in line 2.
Answer: C
知识点:重写时,子类方法的返回值可以与父类方法的返回值相同,或者更小,但是不能更大
QUESTION 14
Which two code fragments correctly create and initialize a static array of int elements? (Choose two.)
下列哪两段代码可以创建和初始化一个静态数组
A. static final int[] a = { 100,200 };
B. static final int[] a;
static { a=new int[2]; a[0]=100; a[1]=200; }
C. static final int[] a = new int[2]{ 100,200 };
D. static final int[] a;
static void init() { a = new int[3]; a[0]=100; a[1]=200; }
Answer: AB
知识点:A是最普通的方法,B用静态代码块
QUESTION 15
Given:
10. interface Foo { int bar(); }
11. public class Sprite {
12. public int fubar( Foo foo ) { return foo.bar(); }
13. public void testFoo() {
14. fubar(
15. // insert code here
16. );
17. }
18. }
Which code, inserted at line 15, allows the class Sprite to compile?
15行插入哪段代码,可以使Sprite类通过编译
A. Foo { public int bar() { return 1; }
B. new Foo { public int bar() { return 1; }
C. new Foo() { public int bar() { return 1; }
D. new class Foo { public int bar() { return 1; }
Answer: C
知识点:fubar( Foo foo )使得15行必须new一个Foo匿名内部类,或者它的实现类,所以选C
QUESTION 16
Given:
1. class Alligator {
2. public static void main(String[] args) {
3. int []x[] = {{1,2}, {3,4,5}, {6,7,8,9}};
4. int [][]y = x;
5. System.out.println(y[2][1]);
6. }
7. }
What is the result?
结果是什么
A. 2
B. 3
C. 4
D. 6
E. 7
F. Compilation fails.
Answer: E
知识点:写法诡异而已,两个都是2维数组
QUESTION 17
Given:
22. StringBuilder sb1 = new StringBuilder("123");
23. String s1 = "123";
24. // insert code here
25. System.out.println(sb1 + " " + s1);
Which code fragment, inserted at line 24, outputs "123abc 123abc"?
24行插入哪段代码,可以得到结果 "123abc 123abc"
A. sb1.append("abc"); s1.append("abc");
B. sb1.append("abc"); s1.concat("abc");
C. sb1.concat("abc"); s1.append("abc");
D. sb1.concat("abc"); s1.concat("abc");
E. sb1.append("abc"); s1 = s1.concat("abc");
F. sb1.concat("abc"); s1 = s1.concat("abc");
G. sb1.append("abc"); s1 = s1 + s1.concat("abc");
H. sb1.concat("abc"); s1 = s1 + s1.concat("abc");
Answer: E
知识点:StringBuilder 用append方法添加字符串;
QUESTION 18
Given that the current directory is empty, and that the user has read and write permissions, and the following:
假设当前目录是空,用户拥有读写权限,
11. import java.io.*;
12. public class DOS {
13. public static void main(String[] args) {
14. File dir = new File("dir");
15. dir.mkdir();
16. File f1 = new File(dir, "f1.txt");
17. try {
18. f1.createNewFile();
19. } catch (IOException e) { ; }
20. File newDir = new File("newDir");
21. dir.renameTo(newDir);
22. }
23. }
Which statement is true?
下列哪条语句陈述正确
A. Compilation fails.
B. The file system has a new empty directory named dir.
C. The file system has a new empty directory named newDir.
D. The file system has a directory named dir, containing a file f1.txt.
E. The file system has a directory named newDir, containing a file f1.txt.
Answer: E
知识点:dir.mkdir();创建一级目录; File f1 = new File(dir, "f1.txt");创建文件;Java中createNewFile方法主要是如果该文件已经存在,则不创建,返回一个false;dir.renameTo(newDir);让目录改名
QUESTION 19
Given:
11. class Converter {
12. public static void main(String[] args) {
13. Integer i = args[0];
14. int j = 12;
15. System.out.println("It is " + (j==i) + " that j==i.");
16. }
17. }
What is the result when the programmer attempts to compile the code and run it with the command java Converter 12?
程序员试图以指令java Converter 12运行时
A. It is true that j==i.
B. It is false that j==i.
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 13.
Answer: D
知识点:编译错误,String[] args与args[0]类型不匹配,改成Integer.valueOf(args[0])
即可通过编译
QUESTION 20
Given:
11. String test = "Test A. Test B. Test C.";
12. // insert code here
13. String[] result = test.split(regex);
Which regular expression, inserted at line 12, correctly splits test into "Test A", "Test B", and "Test C"?
12行,插入哪段代码,可以正确分离出"Test A", "Test B", and "Test C"
A. String regex = "";
B. String regex = " ";
C. String regex = ".*";
D. String regex = "\\s";
E. String regex = "\\.\\s*";
F. String regex = "\\w[ \.] +";
Answer: E
知识点:正则表达式