读代码分析结果

来源:互联网 发布:淘宝无法提交订单 编辑:程序博客网 时间:2024/05/16 19:19

读代码分析结果答案在最后,先做再看!

阅读代码段,看是否有错,如果有错。请写出并改正!如果没有,写出结果!


一、

public class ExceptionTest {    public static void main(String[] args) {        try {            String str = null;            System.out.println(str.length());        } catch (NullPointerException e) {            System.out.println("a");        } catch (RuntimeException e) {            System.out.println("b");        } finally {            System.out.println("c");        }    }}

二、

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.set");            ObjectOutputStream os = new ObjectOutputStream(fs);            os.writeObject(f);            os.close();        } catch (Exception e) {            e.printStackTrace();        }    }    class Tree {        ;    };}

三、

public class Hello {    String title;// null    int value;// 0    public Hello() {        title = "World";    }    public Hello(int value) {        this.value = value;        title = "Hello";        Hello();    }    public static void main(String[] args) {        Hello h = new Hello();        System.out.println(h.title);    }}

四、

public class IfTest {    interface DeclareStuff {        public static final int SKEY = 3;        void doStuff(int x);    }    public class TestDeclare implements DeclareStuff {        public void doStuff(int x) {            x += SKEY + ++x;            System.out.println(x);        }    }    public static void main(String[] args) {        int x = 5;        new IfTest().new TestDeclare().doStuff(++x);//6    }}

五、

public class ThreadTest implements Runnable {    public static void main(String[] args) throws Exception {        Thread t = new Thread(new ThreadTest());        t.start();        System.out.println("Start");        t.join(1000);        System.out.println("Compliete");    }    public void run() {        for (int i = 0; i < 4; i++) {            System.out.println(i);        }    }}

六、

public class TimerTest {    public static void appendList(List list) {        list.add("test");    }    public static void main(String[] args) {        List<Integer> aalist = new ArrayList<Integer>();        aalist.add(11);        appendList(aalist);        System.out.println(aalist.get(1));    }}



  • 第一题:
    a
    c

  • 第二题:
    报错 java.io.NotSerializableException: com.test.offer.Forest$Tree
    因为:一个可序列化的类,必须要求他的变量也是可序列化的,要是变量不能被序列化那这个类就不能被序列化

  • 第三题:
    World

  • 第四题:
    16

  • 第五题:
    Start
    0
    1
    2
    3
    Compliete

  • 第六题:
    test

原创粉丝点击