一、选择题(每题5分,共50分)
    1.下列代码的输出结果是:()

    1. public class StaticFoo {
    2. int num;
    3. static int x;
    4. public static void main(String[] args) {
    5. StaticFoo foo1 = new StaticFoo();
    6. foo1.num++;
    7. foo1.x++;
    8. StaticFoo foo2 = new StaticFoo();
    9. foo2.num++;
    10. foo2.x++;
    11. StaticFoo foo3 = new StaticFoo();
    12. foo3.num++;
    13. foo3.x++;
    14. StaticFoo.x++;
    15. System.out.println(foo3.num + ",");
    16. System.out.println(foo3.x);
    17. }
    18. }

    A.3,3 B.1,3 C.3,4 D.1,4
    正确答案:D

    2.下列代码的输出结果是:()

    1. public class TestTwo implements Runnable {
    2. public static void main(String[] args) throws Exception {
    3. Thread t = new Thread(new TestTwo());
    4. t.start();
    5. System.out.println("Started");
    6. t.join();
    7. System.out.println("Complete");
    8. }
    9. public void run() {
    10. for (int i = 0; i < 4; i++) {
    11. System.out.println(i);
    12. }
    13. }
    14. }

    A. StartedCompleteB. StartedComplete0123
    C. Started0123CompleteD.Complete0123Started

    正确答案:C

    3.请看下列代码编译和运行的结果是()

    1. interface DeclareStuff {
    2. public static final int EASY = 3;
    3. void doStuff(int t);
    4. }
    5. public class TestDeclare implements DeclareStuff {
    6. public static void main(String[] args) {
    7. int x = 5;
    8. new TestDeclare().doStuff(++x);
    9. }
    10. void doStuff(int s) {
    11. s += EASY + ++s;
    12. System.out.println("s=" + s);
    13. }
    14. }

    A.s=14 B.s=16 C.运行时异常 D.编译失败
    正确答案:D
    答案解析:TestDeclare中的doStuff需要有public修饰符,否则会出现子类reduce visibility错误。

    4.List类的对象list中的元素为:[0,1,2,3,4,5,6,7,8,9],现在想返回该list对象的子集合[5,6,7,8],需要做的操作是()
    A.list.subList(5,8); B. list.subList(5,9);
    C . list.subList(4,8); D. list.subList(4,9);

    正确答案:B

    5.下列代码编译和运行的结果是:()

    1. class SuperCalc {
    2. protected static int multiply(int a, int b) {
    3. return a * b;
    4. }
    5. }
    6. class SubCalc extends SuperCalc {
    7. public static int multiply(int a, int b) {
    8. int c = super.multiply(a, b);
    9. return c;
    10. }
    11. }
    12. public class TestSuper {
    13. public static void main(String[] args) {
    14. SubCalc sc = new SubCalc();
    15. System.out.println(sc.multiply(3, 4));
    16. System.out.println(SubCalc.multiply(2, 2));
    17. }
    18. }

    A.运行代码,但是没有输出
    B.代码public static int multiply(int a,int b)行,编译错误
    C.代码int c = super.multiply(a,b)行,编译错误
    D.代码System.out.println(sc.multiply(3, 4))行,编译错误

    正确答案:C
    答案解析:在static方法中不能使用this, super关键字

    6.下列代码运行的结果是()

    1. public class Forest implements Serializable {
    2. private Tree tree = new Tree();
    3. public static void main(String[] args) {
    4. Forest f = new Forest();
    5. try {
    6. FileOutputStream fs = new FileOutputStream("Forest.ser");
    7. ObjectOutputStream os = new ObjectOutputStream(fs);
    8. os.writeObject(f);
    9. os.close();
    10. } catch (Exception e) {
    11. e.printStackTrace();
    12. }
    13. }
    14. }
    15. class Tree {
    16. }

    A.编译失败 B.运行时异常
    C.Forest被序列化 D.Forest和Tree都被序列化到文件

    正确答案:B
    答案解析:因为Tree没有实现Serilizable接口

    7.下列关于线程的名字说法正确的是:()
    A.可以使用Thread类的setName()方法为线程设置名字
    B.Java规定所有的线程名字都是Thread=1形式的
    C.不能人为地为线程设置名字
    D.默认情况下线程无名字

    正确答案:A

    8.关于java线程的说法错误的是()
    A.创建线程的有两种方式,方式1是继承Thread类,方式2是实现Runnable接口
    B.解决线程安全使用问题synchronized关键字,使得同一时间只有一个线程执行该关键字限定的代码段
    C.线程通信所使用的方法有,wait,notify,notifyAll,他们都是Thread的方法
    D.Java线程包括5个状态,线程的创建,可运行,运行,阻塞和消亡

    正确答案:C

    9.下列代码中不能正确获取到Class类的对象的是:()
    A.Class c2 = int.TYPE;
    B.String sub = “hello”; Class c1 = sub.getClass();
    C.Class c1 = Class.forName(“java.lang.Integer”);
    D.Button b = new Button(); Class c1 = b.getClass(); Class c2 = c1.getSuperclass();

    正确答案:A
    答案解析:正确的形式是:Class c2= int.class;

    10.下面关于interface,叙述错误的是:()
    A.一个interface可以继承多个interface
    B.接口中的方法可以由private修饰
    C.Interface中可以定义static final 常量
    D.Interface中可以无任何方法定义

    正确答案:B
    答案解析:接口中的方法都是public的

    二、简答题(每题10分,共20分)
    1.如何通过反射创建对象,当被反射的类不存在无参数构造器时如何创建对象?

    正确答案:

    1. public class Employee {
    2. String name;
    3. public Employee()
    4. {
    5. System.out.println("eee");
    6. }
    7. public Employee(String name)
    8. {
    9. this.name = name;
    10. }
    11. public static void main(String[] args) {
    12. Class<Employee> c1 = Employee.class;
    13. try {
    14. //调用无参构造方法
    15. Employee e1 = c1.newInstance();
    16. System.out.print(e1);
    17. //调用有参构造方法
    18. Constructor<Employee> con = c1.getDeclaredConstructor(String.class);
    19. Employee e2 = con.newInstance("james");
    20. System.out.println(e2.name);
    21. } catch (InstantiationException | IllegalAccessException e) {
    22. // TODO Auto-generated catch block
    23. e.printStackTrace();
    24. } catch (NoSuchMethodException e) {
    25. // TODO Auto-generated catch block
    26. e.printStackTrace();
    27. } catch (SecurityException e) {
    28. // TODO Auto-generated catch block
    29. e.printStackTrace();
    30. } catch (IllegalArgumentException e) {
    31. // TODO Auto-generated catch block
    32. e.printStackTrace();
    33. } catch (InvocationTargetException e) {
    34. // TODO Auto-generated catch block
    35. e.printStackTrace();
    36. }
    37. }
    38. }

    2.简述static关键字的作用
    static修饰代码块,类被加载到内存时,执行一次
    static:修饰属性,代表属性为类所有。
    static修饰方法,代表方法为类所有,可用类名.static方法名的方法调用。

    三、编程题(每题10分,共30分)
    1.请实现集合元素的深层复制。(集合元素类型自定义)

    1. public static void main(String[] args) {
    2. List<Emp> list1 = new ArrayList<>();
    3. list1.add(new Emp("z1"));
    4. list1.add(new Emp("z2"));
    5. list1.add(new Emp("z3"));
    6. List<Emp> list2 = new ArrayList<>();
    7. for(Emp e: list1)
    8. {
    9. /*
    10. * //浅层复制
    11. * list2.add(e);
    12. * */
    13. //深层复制
    14. Emp ee = new Emp(e.name);
    15. list2.add(ee);
    16. }
    17. list1.get(0).name="xx";
    18. //打印list2
    19. for(Emp e:list2)
    20. {
    21. System.out.println(e.name);
    22. }
    23. }

    2.创建一个HashMap并迭代出所有的key和value

    1. public static void main(String[] args) {
    2. // TODO Auto-generated method stub
    3. Map<String, String> map = new HashMap<>();
    4. map.put("aa", "aa");
    5. map.put("bb", "bb");
    6. map.put("cc", "cc");
    7. //方法1:得到所有key
    8. Set<String> keys = map.keySet();
    9. for(String key: keys)
    10. {
    11. String value = map.get(key);
    12. System.out.println(key+"="+value);
    13. }
    14. //方法2:得到所有key&value
    15. Set<Entry<String, String>> entries = map.entrySet();
    16. for(Entry<String, String> entry: entries)
    17. {
    18. System.out.println(entry.getKey()+"="+entry.getValue());
    19. }
    20. }

    3.请使用递归的方式求1到100的累加和。

    1. public static void main(String[] args) {
    2. System.out.println(f(100));
    3. }
    4. public static int f(int n)
    5. {
    6. if(n==1)
    7. {
    8. return 1;
    9. }
    10. else{
    11. return n + f(n-1);
    12. }
    13. }