您的位置:首页 > 编程语言 > Java开发

不看OCJP考题你永远不知道自己的JAVA基础有多差(三)

2012-07-03 00:23 435 查看
上期答案

第六题
11. public class Person {
12. privateString name;
13. publicPerson(Stringname) {
14. this.name = name;
15. }
16. public int hashCode() {
17. return 420;
18. }
19. }
下列哪个叙述是正确的:
A.在HashMap查找某个Person键值所需要的时间依赖于Map的大小。
B.执行删除一个Person键的操作,将删除HashMap中所有类型为Person的键
C.在HashSet中插入第二个Person对象,将导致第一个Person对象被移除。
D.在HashSet中查找一个Person对象是否存在的时间是一个常数,不依赖于HashSet的大小
答案:A
知识点:

假设某类集的大小为n,那么对此类集的查找算法的时间复杂度为O(n)因此A是正确的,且D是错误的。
对于Set和Map中,对于“重复实例”的认定必须同时复写该类的equals()和hashCord()两个方法。否则对“重复实例”的认定只是比较对象实例的内存地址,因此B和C是错误的

第七题
5. import java.util.*;
6. public class SortOf {
7. public static voidmain(String[] args) {
8. ArrayList<Integer> a = newArrayList<Integer>();
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. }
该程序片段的运行结果是?
A. [1 2 3 5]
B. [2 1 35]
C. [2 5 31]
D. [5 3 21]
E. [1 3 52]
F. 编译失败
G. 运行时有异常抛出
答案:C
知识点:
Conllections类定义了一系列针对容器操作的静态方法,笼统的讲凡事实现了Conlection接口的容器都可以在Conllections类中的静态方法中找到对应的操作,本题中使用到了其中的两个静态方法:
sort():根据元素的自然顺序对指定列表(List)按升序排列顺序。
reverse():反转指定列表中元素的顺序。
关于Conllections类的详细信息请参考JDK1.6的帮助文档

第八题
11. public interface Status {
12. /* 此处插入代码*/ int MY_VALUE = 10;
13. }
下列哪三个选项可以用于第12行
A. final
B. static
C. native
D. public
E. private
F. abstract
G. protected
答案:A B D
知识点:
接口的定义明确的要求接口要由:
抽象方法、全局常量组成。
抽象方法的修饰词:public abstract
全局常量的修饰词:public final static

第九题
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 staticvoidmain(String[] a){ newMountain(); }
17. }
上述代码片段的运行结果是?
A. 编译失败.
B. atom granite
C. granite granite
D. atom granite granite
E. An exception is thrown at runtime.
F. atom graniteatom granite
答案:F
知识点:
当子类被实例化的时候都要首先调用父类的构造方法,按照这个规则倒推,稍加细心,此题不难

第十题
10. class Line {
11. public classPoint { public int x, y;}
12. public PointgetPoint() { return new Point(); }
13. }
14. class Triangle {
15. publicTriangle() {
16. //
此处插入代码
17. }
18. }
在16行处插入代码获得一个Class Point的实例?
A. Point p = Line.getPoint();
B. Line.Point p = Line.getPoint();
C. Point p = (new Line()).getPoint();
D. Line.Point p = (newLine()).getPoint();

答案:D

知识点:

创建实例内部类的实例时,外部类的实例必须存在

外部类类名.内部类类名引用变量名称= 外部类对象的引用.new 内部类构造器;

外部类类名.内部类类名引用变量名称= new 外部类构造器.new 内部类构造器;

另一种可能的正确答案

Line.Point p = new Line().new Point();

--------------------------------------------------------分割线-----------------------------------------------

本期问题:

第十一题:

22. StringBuilder sb1 = new StringBuilder("123");

23. String s1 = "123";

24. // 此处插入代码

25. System.out.println(sb1 + " " + s1);

将下列哪段代码插入到第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");

第十二题:

11. class Alpha {

12. publicvoid foo() { System.out.print("Afoo "); }

13. }

14. public class Beta extends Alpha {

15. publicvoid foo() { System.out.print("Bfoo "); }

16. publicstatic void main(String[] args) {

17. Alpha a = new Beta();

18. Beta b = (Beta)a;

19. a.foo();

20. b.foo();

21. }

22. }

此程序的运行结果是?

A. Afoo Afoo

B. Afoo Bfoo

C. Bfoo Afoo

D. Bfoo Bfoo

E. 编译失败.

F. 运行时存在异常.

第十三题:

一下哪两个选项能够创建并初始化一个静态的整型数组:

A. static final int[] a ={ 100200 };

B. static final int[] a;

static { a=new int[2]; a[0]=100; a[1]=200;}

C. static final int[] a =new int[2]{ 100200 };

D. static final int[] a;

static void init() { a = new int[3];a[0]=100; a[1]=200; }

第十四题:

10. interface Foo { intbar(); }

11. public class Sprite {

12. public int fubar( Foo foo ) { returnfoo.bar(); }

13. public void testFoo() {

14. fubar(

15. // insert code here

16. );

17. }

18. }

在第15行处插入哪段代码可以使得Sprite类完整?

A. Foo { public int bar(){ return 1; }

B. new Foo { public intbar() { return 1; }

C. new Foo() { public intbar() { return 1; }

D. new class Foo { publicint bar() { return 1; }

第十五题

1. class Alligator {

2. public static void main(String[] args) {

3. int []x[] = {{12} {345} {6789}};

4. int [][]y = x;

5. System.out.println(y[2][1]);

6. }

7. }

以上程序的运行结果是?

A. 2

B. 3

C. 4

D. 6

E. 7

F. 编译失败
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: