OCJP题目及解析

这篇博客详细解析了OCJP(Oracle Certified Java Programmer)考试的多个题目,涵盖泛型、集合、多态、继承、接口、字符串操作等多个知识点,帮助考生理解和准备考试。
摘要由CSDN通过智能技术生成

OCJP题目及解析

每天做一点

1.

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? (Choose
three.)哪三个改变可以使此类使用泛型,并且没有黄色警告
A. Remove line 14.
B. Replace line 14 with "int i = iter.next();".
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<int> intList)".
F. Replace the method declaration with "sum(List<Integer> intList)". 

Answer: A,C,F

知识点:

1)泛型必须使用包装类,而不是基本数据类型,所以选F

2)增强for,使用同样的数据类型i,遍历list中的数据,选C

3)同上,增强for,无需14行的代码

2.

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. What supports these requirements.?
需要一个List算法,提供有效的添加,但是不支持快速随机查找
A. java.util.Queue
B. java.util.ArrayList
C. java.util.LinearList
D. java.util.LinkedList 

Answer: D

知识点:

A:队列,肯定不行它没有实现list接口。A×

B: ArrayList。有 add方法,支持快速方法有 gen(index)方法 ,底层是用数组实现的。不符合题意:B×

C: C向这个结构,查jdk api根本不存在

D:链表结构,支持add方法。缺点访问速度慢。链表结构,优点:添加删除的速度快。底层是引用实现的 D√

4.

QUESTION 4
Given:
12. import java.util.*;
13. public class Explorer2 {
   
14. public static void main(String[] args) {
   
15. TreeSet<Integer> s = new TreeSet<Integer>();
16. TreeSet<Integer> subs = new TreeSet<Integer>();
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

解析:考察 TreeSet的使用, Set集合。无序,但是值不能重复。实现不重复的原因是。执行equals和hashcode方法。subset方法。从608 到611的数字。true包括608和611。填充到子集合中。sub:608,610.

5.

QUESTION 5
Given:
1. public class Score implements Comparable<Score> {
   
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?
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原码:泛型跟结构一致。选择B

public interface Comparable<T> {
   
//里面只有一个compareTo()方法
 public int compareTo(T o);
}

6.

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.
B. Deleting a Person key from a HashMap will delete all map entries for all keys of type Person.
C. Inserting a second Person object into a HashSet will cause the first Person object to be removed as a
duplicate.
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.
Answer: A

解析:在hashMap中找到以Person为key的时间取决于map的大小
hashmap不存在重复值。该类重写了hashcode。这个方法永远为真。检验是否重复还有equlas方法。

7.

QUESTION 7
Given:
5. import java.util.*;
6. public class SortOf {
   
7. public static 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值