ArrayList<String> list = new ArrayList<String>();
list.add("aA");
list.add("Aa");
Collections.sort(list);
System.out.println(list);
2.这段代码输出结果是?
import java.util.HashSet;
public class WrapString {
private String str="";
public WrapString(String str) {
this.str = str;
}
public static void main(String[] args) {
HashSet<Object> set = new HashSet<Object>();
set.add(new String("abc"));
set.add(new String("abc"));
set.add(new WrapString("sfd"));
set.add(new WrapString("sfd"));
System.out.println(set.size());
}
}
3.这段代码输出什么?
public static void main(String[] args) {
Object[] objs = {new String("foo"),new Integer(12),new Integer(5),new Boolean(true)};
Arrays.sort(objs);
for(Object obj:objs){
System.out.println(obj);
}
}
4.下边代码哪里有问题,指出问题代码。
package aa.bb;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
public class Test {
class A{}
class B extends A{}
public static void main(String[] args) {
List<A> la = new LinkedList<A>();
List<B> lb = new LinkedList<B>();
List<Object> lo = new LinkedList<Object>();
m1(la);
m1(lb);
m2(la);
m2(lb);
m1(lo);
m2(lo);
}
public static void m1(List<? extends A> list){}
public static void m2(List<A> list){}
}
5.输出结果:
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());
}
6.以下代码有无问题?
public class Test<N extends Integer> {
private N min,max;
public N getMin(){ return min;
}
public N getMax(){
return max;
}
public void add(N n){
if(n!=null && n.doubleValue()<min.doubleValue()) min=n;
if(n!=null && n.doubleValue()>max.doubleValue()) max=n;
}
}
public class Test<N extends Integer>换成
public class Test<N extends Number>呢?
7.下边的结果是:
public class Test{
public static Collection getSort(){
Collection sort = new LinkedList();
sort.add("B");
sort.add("C");
sort.add("A");
return sort;
}
public static void main(String[] args) {
for(Object o:getSort()){
System.out.println(o);
}
}
}
8.下边代码的结果是:
public class Test{
public static void foo(List<Object> l ){
}
public static void main(String[] args) {
List<String> l1 = new ArrayList<String>();
foo(l1);
ArrayList<Object> l2 = new ArrayList<Object>();
foo(l2);
ArrayList l3 = new ArrayList();
foo(l3);
ArrayList<Object> l4 = new ArrayList<String>();
foo(l4);
}
}
9.下边代码的结果是:
public class Test{
public static Object get0(List l){
return l.get(0);
}
public static void main(String[] args) {
String o1 = get0(new LinkedList<String>());
Object o2 = get0(new LinkedList());
Object o3 = get0(new LinkedList<Object>());
Object o4 = get0(new LinkedList<?>());
String o5 = (String)get0(new LinkedList<String>());
}
}
10.
public class Test{
enum Example{
ONE,TWO,THREE;
}
public static void main(String[] args) {
Collection c = new ArrayList();
c.add(Example.THREE);
c.add(Example.THREE);
c.add(Example.THREE);
c.add(Example.TWO);
c.add(Example.TWO);
c.add(Example.ONE);
Set set = new HashSet(c);
}
}
对这句
Set set = new HashSet(c)
代码描述正确的是:
A.The set variable contains all six elements from the coll collection, and the order is guaranteed to be preserved.
B. The set variable contains only three elements from the coll collection, and the order is guaranteed to be preserved.
C. The set variable contains all six elements from the coll collection, but the order is NOT guaranteed to be preserved. D. The set variable contains only three elements from the coll collection, but the order is NOT guaranteed to be preserved.
11.以下程序哪些句子可以1,2的输出结果?
public class Test{
public static void main(String[] args) {
// Set<Integer> set = new TreeSet<Integer>();
// Set<Integer> set = new HashSet<Integer>();
Set<Integer> set = new LinkedHashSet<Integer>();
set.add(2);
set.add(1);
System.out.println(set);
}
}
12.