CoreJava——阶段测试题(二)

来源:互联网 发布:数据库一致性 编辑:程序博客网 时间:2024/06/08 00:08

2.遍历一个List有哪些不同的方式?

1.for(int i=0;i<list.size;i++){System.out.println(list.get(i));}2.for(Object o:list){System.out.println(o);}3.Iterator iterator = list.iterator();4.while(iterator.hasNext()){System.out.println(iterator.next());}

3.break,continue以及return的用法。

break跳出当前循环,继续往下执行continue 跳出本次循环,继续下一次循环return 结束当前方法,return之后的语句不会执行 

4.写出2个常见的检查时异常以及4个运行时异常。

检查时异常:IOException   FileNotFoundException   ClassNotFoundException     运行时异常:ClassCastException  IndexOutOfBoundsException   NullPointerException       IllegalArgumentException

5.访问修饰有哪些,以及它们的访问范围。
这里写图片描述

四、程序分析题(5分/题,共10分)

1.下面代码的运行结果是什么?

class HelloA {    public HelloA() {        System.out.println("HelloA");    }    { System.out.println("I'm A class"); }    static { System.out.println("static A"); }}public class HelloB extends HelloA {    public HelloB() {        System.out.println("HelloB");    }       { System.out.println("I'm B class"); }    static { System.out.println("static B"); }    public static void main(String[] args) {      new HelloB();   }}
答案:static Astatic BI'm A classHelloAI'm B classHelloB解析:new HelloB();1.加载静态代码块先加载父类的,再加载本类的2.调用父类的匿名代码块3.调用父类的构造器4.调用本类的匿名代码块5.调用本类的构造器p.s.new一个对象的时候JVM都做了那些事情1.类加载,(java -cp xx.xx.xx,Class.forName/load)   同时初始化类中静态的属性(赋默认值)2.执行静态代码块(先父类后自己)    //如果之前做过类加载,从第三步开始执行3.分配内存空间,同时初始化非静态的属性(赋默认值)4.调用父类构造器,如果父类中有匿名代码块先调用匿   名代码块5.父类构造器执行完后,如果自己声明属性的同时有   显示的赋值,那么进行显示赋值把默认值覆盖6.执行匿名代码块7.执行构造器8.返回内存地址

2.分析以下程序的输出结果

public class Polymorphic extends A{        public int j=4;        public Polymorphic(){            print();        }        public void print(){            System.out.println(j);        }        public static void main(String[] args) {            new Polymorphic();        }}class A{        public int i=6;        public A(){            print();        }        public void print(){            System.out.print(i);        }}
答案:04

五、编程题(10分/题,共30分)
1、古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少对?

//这是一个菲波拉契数列问题public class lianxi01 {    public static void main(String[] args) {    System.out.println("第1个月的兔子对数:    1");    System.out.println("第2个月的兔子对数:    1");    int f1 = 1, f2 = 1, f, M=24;        for(int i=3; i<=M; i++) {  //3 4 5 6            f = f2;                   //1 2 3 5            f2 = f1 + f2;             //2 3 5 8            f1 = f;                   //1 2 3 5            System.out.println(                    "第" + i +"个月的兔子对数: "+f2);        }    }}

2、写一个类Student类,有id、name、score属性,把学生对象放到集合中排序
按照学生分数从高到低排(名字叫tom的学生特殊照顾一下,无论多少分都要排在第一名)

package com.briup.day11.e4;public class Student{    private int id;    private String name;    private int score;    public Student(int id, String name, int score) {        this.id = id;        this.name = name;        this.score = score;    }    public Student() {    }    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getScore() {        return score;    }    public void setScore(int score) {        this.score = score;    }    public String toString() {        return "Student         [id=" + id + ",         score=" + score+ "]";    }}package com.briup.day11.e4;import java.util.Comparator;public class MyComparator implements Comparator<Student> {    public int compare(Student o1, Student o2) {        if(o1.getName()=="tom"&&o2.getName()!="tom"){            return -1;        }else if(o1.getName()!="tom"&&o2.getName()=="tom"){            return 1;        }else{            if(o1.getScore()>o2.getScore()){                return -1;            }else if(o1.getScore()<o2.getScore()){                return 1;            }else{                return 0;            }        }    }   }
package com.briup.day11.e4;import java.util.TreeSet;public class StudentTreeSet {    public static void main(String[] args) {        MyComparator myComparator = new MyComparator();        TreeSet<Student> treeSet = new TreeSet<Student>(myComparator);        Student s1 = new Student(1, "zhangsan1", 42);        Student s2 = new Student(2, "zhangsan2", 46);        Student s3 = new Student(3, "zhangsan3", 41);        Student s4 = new Student(4, "tom", 40);        treeSet.add(s1);        treeSet.add(s3);        treeSet.add(s4);        treeSet.add(s2);        for(Student o:treeSet){            System.out.println(o);        }    }}

3.编写PictureClient.java、PictureServer.java和PictureThread.java文件,功能是用多线程实现将一张图片从客户端(PictureClient.java)上传至服务端(PictureServer.java),多线程是指:服务端一直在运行,等待客户端的消息,当有多个客户端同时上传图片时,服务端要能同时处理多个客户端,须考虑程序在运行过程中可能出现的异常。

//PictureClient.javaimport java.io.*;import java.net.Socket;import java.util.Scanner;public class PictureClient{    public static void main(String[] args){        System.out.println("这是客户端。。。");        try{            Socket socket=new Socket("127.0.0.2",9999);            File file=new File("file/client.jpg");            if(!file.exists()){                file.createNewFile();            }            DataOutputStream dataOutputStreamToServer=new DataOutputStream(socket.getOutputStream());            dataOutputStreamToServer.writeLong(System.currentTimeMillis());            FileInputStream fileInputStreamFromFile=new FileInputStream(file);            OutputStream outputStreamToServer=socket.getOutputStream();            byte[] data=new byte[1024];            int len=0;            while((len=fileInputStreamFromFile.read(data))!=-1){                outputStreamToServer.write(data,0,len);            }            socket.shutdownOutput();            InputStream inputStreamFromServer=socket.getInputStream();            byte[] dataFromServer=new byte[1024];            int read=inputStreamFromServer.read(dataFromServer);            System.out.println(new String(dataFromServer,0,read));            fileInputStreamFromFile.close();            socket.close();        }catch(IOException e){            e.printStackTrace();        }    }}//PictureServer.javaimport java.io.IOException;import java.net.*;public class PictureServer{    public static void main(String[] args){        System.out.println("这是服务端。。。");        try{            ServerSocket serverSocket=new ServerSocket(9999);            while(true){                Socket socket=serverSocket.accept();                new Thread(new PictureThread(socket)).start();            }        }catch(IOException e){            e.printStackTrace();        }    }}

-_-终于码完了,其实码到后面又开始偷懒啦~~~CoreJava学习也暂时告一段落,我爱学习,学习使我快乐~