OCJP(1Z0-851) 模拟题分析(一)

Exam : 1Z0-851

Java Standard Edition 6 Programmer Certified Professional Exam

以下分析全都是我自己分析或者参考网上的,定有疏漏,还请大家对我的分析提出质疑。


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? (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

考察把原始类型转换成泛型。则把List转换成List<Integer>,由于类型参数不能为基本类型,所以E不正确,而第13行中Iterator转换成Iterator<Integer>,Integer可以自动装箱和拆箱,所以第14行不需要。


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.?
A. java.util.Queue
B. java.util.ArrayList
C. java.util.LinearList
D. java.util.LinkedList

Answer: D

实现了 java.util.List接口,并且需要高效率在首部进行插入操作,选项中仅有LinkedList符合


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.
}
Which two, inserted at line 11, will allow the code to compile? (Choose two.
)A. public class MinMax<?>
{
The safer , easier way to help you pass any IT exams.
B. public class MinMax<? extends Number>
{
C. public class MinMax<N extends Object>
{
D. public class MinMax<N extends Number>
{
E. public class MinMax<? extends Object>
{
F. public class MinMax<N extends Integer>
{

Answer: D,F

必须有N这个泛型,所以从CDF中选择,而N需要实现了doubleValue方法,Object肯定不行,而Number和Integer都实现了这个方法。


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以及它的subSet方法,两个true表示包括608和611。TreeSet是一个有序集,所以其中的元素必须是有序的。

public SortedSet<E> subSet(E fromElement,
                  E toElement)
Returns a view of the portion of this set whose elements range from  fromElement, inclusive, to toElement, exclusive. (If fromElement and toElement are equal, the returned set is empty.) The returned set is backed by this set, so changes in the returned set are reflected in this set, and vice-versa. The returned set supports all optional set operations that this set supports.
subSet返回的是一个view,而不是copy,所以对subSet的改变会反映到原set里面,反之,在原set中的变化也会反映到subSet里面。

看下面的例子,首先移除一个15,发现原始的treeSet和subset里面的15都没了,而treeSet加上15之后,原始的treeSet和subset里面的15又都出现了。

package cn.xujin;
import java.util.TreeSet;

public class Explorer1 {
	public static void main(String[] args) {
		TreeSet<Integer> treeSet = new TreeSet<Integer>();
		TreeSet<Integer> subSet = new TreeSet<Integer>();
		for(int i = 10; i <= 20; i++)
			treeSet.add(i);
		subSet = (TreeSet<Integer>) treeSet.subSet(13, true, 16, true);
		System.out.println(subSet);//[13, 14, 15, 16]
		subSet.remove(15);
		System.out.println(subSet);//[13, 14, 16]
		System.out.println(treeSet);//[10, 11, 12, 13, 14, 16, 17, 18, 19, 20]
		treeSet.add(15);
		System.out.println(subSet);//[13, 14, 15, 16]
		System.out.println(treeSet);//[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
	}
 }


5.Given:
1. public class Score implements Comparable<Scor
  • 2
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值