一、选择题(每题5分,共50分)
1.下列代码的输出结果是:()
public class StaticFoo {int num;static int x;public static void main(String[] args) {StaticFoo foo1 = new StaticFoo();foo1.num++;foo1.x++;StaticFoo foo2 = new StaticFoo();foo2.num++;foo2.x++;StaticFoo foo3 = new StaticFoo();foo3.num++;foo3.x++;StaticFoo.x++;System.out.println(foo3.num + ",");System.out.println(foo3.x);}}
A.3,3 B.1,3 C.3,4 D.1,4
正确答案:D
2.下列代码的输出结果是:()
public class TestTwo implements Runnable {public static void main(String[] args) throws Exception {Thread t = new Thread(new TestTwo());t.start();System.out.println("Started");t.join();System.out.println("Complete");}public void run() {for (int i = 0; i < 4; i++) {System.out.println(i);}}}
A. StartedCompleteB. StartedComplete0123
C. Started0123CompleteD.Complete0123Started
正确答案:C
3.请看下列代码编译和运行的结果是()
interface DeclareStuff {public static final int EASY = 3;void doStuff(int t);}public class TestDeclare implements DeclareStuff {public static void main(String[] args) {int x = 5;new TestDeclare().doStuff(++x);}void doStuff(int s) {s += EASY + ++s;System.out.println("s=" + s);}}
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.下列代码编译和运行的结果是:()
class SuperCalc {protected static int multiply(int a, int b) {return a * b;}}class SubCalc extends SuperCalc {public static int multiply(int a, int b) {int c = super.multiply(a, b);return c;}}public class TestSuper {public static void main(String[] args) {SubCalc sc = new SubCalc();System.out.println(sc.multiply(3, 4));System.out.println(SubCalc.multiply(2, 2));}}
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.下列代码运行的结果是()
public class Forest implements Serializable {private Tree tree = new Tree();public static void main(String[] args) {Forest f = new Forest();try {FileOutputStream fs = new FileOutputStream("Forest.ser");ObjectOutputStream os = new ObjectOutputStream(fs);os.writeObject(f);os.close();} catch (Exception e) {e.printStackTrace();}}}class Tree {}
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.如何通过反射创建对象,当被反射的类不存在无参数构造器时如何创建对象?
正确答案:
public class Employee {String name;public Employee(){System.out.println("eee");}public Employee(String name){this.name = name;}public static void main(String[] args) {Class<Employee> c1 = Employee.class;try {//调用无参构造方法Employee e1 = c1.newInstance();System.out.print(e1);//调用有参构造方法Constructor<Employee> con = c1.getDeclaredConstructor(String.class);Employee e2 = con.newInstance("james");System.out.println(e2.name);} catch (InstantiationException | IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SecurityException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
2.简述static关键字的作用
static修饰代码块,类被加载到内存时,执行一次
static:修饰属性,代表属性为类所有。
static修饰方法,代表方法为类所有,可用类名.static方法名的方法调用。
三、编程题(每题10分,共30分)
1.请实现集合元素的深层复制。(集合元素类型自定义)
public static void main(String[] args) {List<Emp> list1 = new ArrayList<>();list1.add(new Emp("z1"));list1.add(new Emp("z2"));list1.add(new Emp("z3"));List<Emp> list2 = new ArrayList<>();for(Emp e: list1){/** //浅层复制* list2.add(e);* *///深层复制Emp ee = new Emp(e.name);list2.add(ee);}list1.get(0).name="xx";//打印list2for(Emp e:list2){System.out.println(e.name);}}
2.创建一个HashMap并迭代出所有的key和value
public static void main(String[] args) {// TODO Auto-generated method stubMap<String, String> map = new HashMap<>();map.put("aa", "aa");map.put("bb", "bb");map.put("cc", "cc");//方法1:得到所有keySet<String> keys = map.keySet();for(String key: keys){String value = map.get(key);System.out.println(key+"="+value);}//方法2:得到所有key&valueSet<Entry<String, String>> entries = map.entrySet();for(Entry<String, String> entry: entries){System.out.println(entry.getKey()+"="+entry.getValue());}}
3.请使用递归的方式求1到100的累加和。
public static void main(String[] args) {System.out.println(f(100));}public static int f(int n){if(n==1){return 1;}else{return n + f(n-1);}}
