SCJP真题库更新14

 

QUESTION 66

Given a pre-generics implementation of a method:  

Which three changes must be made to the method sum to use generics? (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 )

public static int sum(List<Integer> intList){

int sum=0;

for(int i : intList){

sum += i;

}

return sum;

}

参考大纲:集合和泛型

 

What is the result? 

A. Compilation fails due to an error in line 23.  

B. Compilation fails due to an error in line 29.

C. A ClassCastException occurs in line 29.   

D. A ClassCastException occurs in line 31.  

E. The value of all four object prints in natural order.

 

 

Answer: ( C ) 

Arrays.sort(Object[] a)方法中,会先将实参数组中的每一个元素转化为Comparable接口类型,此时如果元素没有实现Comparable接口则会抛出java.lang.ClassCastException的异常;接下来调用每一个元素的从Comparable接口实现的compareToT o)方法来进行整体数组的堆排序,此时要求每个对象相互间是可以类型转换的,否则会报ClassCastException的异常。

参考大纲:泛型

 

QUESTION 68

Place the code into position to create a class that maps from Strings to Integer values. The result of execution must be [one]. Some options may be used more than once.

Answer: (  )

public class NumberNames {

        private HashMap<String, Integer> map =

new HashMap<String, Integer>();

        public void put(String name, Integer value){

                map.put(name, value);

        }

        public Set<String> getNames(){

                Return map.keySet();

        }

}

 

QUESTION 69

Place a result onto each method call to indicate what would happen if the method call

were inserted at line 9. Note: Results can be used more than once.

Answer: (  )

Method

Result

m1(listA)

Compiles and runs without error

泛型规范没问题

m2(listA)

Compiles and runs without error

泛型规范没问题

m1(listB)

Compiles and runs without error

BA的子类,泛型规范没问题

m2(listB)

Does not compile

m1(listO)

Does not compile

m2(listO)

Does not compile

 

 

 

 

 

 

 

 

 

 

 

 

QUESTION 70

Given the exhibit:

What is the result? 

A. apple:apple

B. carrot:apple

C. apple:banana

D. banana:apple

E. carrot:carrot

F. carrot:banana

 

 

Answer: ( C )

PriorityQueue优先级队列:预设是自然排序,因此pq内的元素顺序将是apple->banana->carrot

poll()取第一个元素,取完之后删除,peek取第一个元素,并不删除元素。

需要注意的是,此类及其迭代器虽然实现了 Collection Iterator 接口的所有可选 方法,但是方法 iterator() 中提供的迭代器 保证以任何特定的顺序遍历优先级队列中的元素。如果需要按顺序遍历,请考虑使用 Arrays.sort(pq.toArray())或者使用如下的两种循环代码块:

 

1     while(!priorityQueue.isEmpty()){

         

          System.out.println(priorityQueue.poll());

      }

 

2

                Object element = null;

      while((element = priorityQueue.poll() )!= null){

         

          System.out.println(element);

      }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值