SCJP认证试题(七)

 /**
*
* @author yaoyuan
*/
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 sun += I;
16 }
17 return sum;
18 }

/**
*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)"
*/

[color=red]// Answer : A C F[/color]



/**
*
* @author yaoyuan
*/

23 Object[] myObjects = {
24 new Integer(12),
25 new String("foo"),
26 new Integer(5),
27 new Boolean(true)
28 }
29 Arrays.sort(myObjects);
30 for(int i=0;i<myObjects.length;i++){
31 System.out.print(myObjects[i].toString());
32 System.out.print(" ");
33 }

/**
*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 ClassCaseException occurs in line 29
*D A ClassCaseException occurs in line 31
*E The value of all four object prints in natural order
*/

[color=red]// Answer : C[/color]


/**
*
* @author yaoyuan
*/

import java.util.*;

public class PQ{
public static void main(String[] args){
PriorityQueue<String> pq = new PriorityQueue<String>();
pq.add("carrot");
pq.add("apple");
pq.add("banana");
System.out.println(pq.poll() + ":" + pq.peek());
}
}

/**
*which code,inserted ay line 14,will allow this class to correctly serialized
*and desterilize?
*
*A apple:apple
*B carrot:apple
*C apple:banana
*D banana:apple
*E carrot:carrot
*F carrot:banana
*/

[color=red]// Answer : C[/color]

[color=green]/** API 详解
一个基于优先级堆的无界优先级队列。优先级队列的元素按照其自然顺序进行排序,或者根据构造队列时提供的 Comparator 进行排序,具体取决于所使用的构造方法。优先级队列不允许使用 null 元素。依靠自然顺序的优先级队列还不允许插入不可比较的对象(这样做可能导致 ClassCastException)。
*/
[/color]


 /**
*
* @author yaoyuan
*/

11 public class Key{
12 private long id1;
13 private long id2;
14
15 //class key methods
16 }


/**
*A programmer is developing a class Key, that will be used as a key in a standard java.util.HashMap.
*Which two methods should be overridden to assure that key works correctly as a key?(choose two)
*
*
*
*A public int hashCode()
*B public Boolean equals(Key k)
*C public int compareTo(Object o)
*D public Boolean equals(Object o)
*/

[color=red]// Answer : A D[/color]


 /**
*
* @author yaoyuan
*/

// insert code here
private N min, max;
public N getMin(){return min;}
public N getMax(){return max;}
public void add(N added){
if(min == null || added.doubleValue() < min.doubleValue())
min = added;
if(max == null || added.doubleValue() < max.doubleValue())
max = added;
}
}

/**
*Which two ,inserted will allow the code to compile?(choose two)
*
*
*A public class M inMax<?>{
*B public class M inMax<? extends Number>{
*C public class M inMax<N extends Object>{
*D public class M inMax<N extends Number>{
*E public class M inMax<? extends Object>{
*F public class M inMax<N extends Integer>{
*/

[color=red]// Answer : D F[/color]


/**
*
* @author yaoyuan
*/

enum Example{ONE, TWO, THREE}

/**
*Which statement is true?
*
*A The expressions(ONE == ONE) and ONE.equals(ONE) are both guaranteed to be true
*B The expression(ONE == TWO) is guaranteed to be true and ONE compare to (TWO) is guaranteed to be * less than one
*C The Example values cannot be used in a raw java.util.HashMap;instead, the programmer must use a * java.util.EnumMap
*D The Example values can be used in a java.util.SortedSet, but the set will not be sorted because * enum erated Type do not implement java.lang.Comparable
*/
[color=red]
// Answer : A[/color]



/**
*
* @author yaoyuan
*/

11 public void getNumbers(){
12 ArrayList numbers = new ArrayList();
13 for(int i=0;i<10;i++){
14 int value = i * ((int)Math.random());
15 Integer intObj = new Integer(value);
16 numbers.add(intObj);
17 }
18 System.out.println(numbers);
19 }


/**
*Which line of code marks the earliest point that an object referenced by intObj becomes a candidate for
*garbage collection?
*
*
*A line 16
*B line 17
*C line 18
*D line 19
*E The object is not a candidate for garbage collection
*/
[color=red]
// Answer : D[/color]


 /**
*
* @author yaoyuan
*/

12 public class Yippee2{
13
14 public static void main(String[] args){
15 for(int x =1;x < yahoo.length;x++){
16 System.out.print(yahoo[x] + " ");
17 }
18 }
19 }



/**
*the command and line invocation : java Yippee2 a b c
*What is the result?
*
*A a b
*B b c
*C a b c
*D Compilation fails
*E An exception is thrown at runtime
*/

[color=red]// Answer : B[/color]



/**
*
* @author yaoyuan
*/

10 class Inner{
11 private int x;
12 public void setX(int x){this.x=x;}
13 public int getX(){return x;}
14 }
15
16 class Outer{
17 private Inner y;
18 public void setY(Inner y){this.y=y;}
19 public Inner getY(){return y;}
20 }
21
22 public class Gamma{
23 public static void main(String[] args){
24 Outer o = new Outer();
25 Inner i = new Inner();
26 int n = 10;
27 i.setX(n);
28 o.setY(i);
29 //insert code here
30 System.out.println(o.getY().getX());
31 }
32 }

/**
*Which three code fragments, added individually at line 29, produce the output 100?
*(choose three)
*
*
*A n = 100;
*B i.setY(100);
*C o.getY().setX(100);
*D i = new Inner(); i.setX(100);
*E o.setY(i); i = new Inner(); i.setX(100);
*F i = new Inner();i.setX(100); o.setY(i);
*/

[color=red]// Answer : B C F[/color]



/**
*
* @author yaoyuan
*/
1 package utils;
2
3 public class Repetition{
4 public static String twice(String s){return s+s;}
5 }
/*
*and given another class Demo:
*/
1 //insert code here
2
3 public class Demo{
4 public static void main(String[] args){
5 System.out.println(twice("pizza"));
6 }
7 }

/**
*Which code should be inserted at line 1 of Demo.java to compile and run Demo to print"pizzapizza"
*
*
*A import utils.*;
*B static import utils.*;
*C import utils.Repetition.*;
*D static import utils.Repetition.*;
*E import utils.Repetition.twice();
*F import static utils.Repetition.twice;
*G import import utils.Repetition.twice;
*/

[color=red]// Answer : F[/color]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值