Java笔试题易错精选

字符串String

String不可变特性(值传递、引用传递)

  • 1、写出下列程序的运行结果

public class TestStringFinal {
    public static void main(String[] args) {
        String a = "hello";
        change(a);
        System.out.println(a);
    }

    public static void change(String name) {
        name = "world";
    }
}

结果为:hello

  • 2、写出下列程序的运行结果

public class TestString {
    public void change(String str, char ch[]) {
        str = "test ok";
        ch[0] = 'g';
    }

    public static void main(String args[]) {
        String str = new String("good");
        char[] ch = { 'a', 'b', 'c' };
        TestString ex = new TestString();
        ex.change(str, ch);
        System.out.print(str + " and ");
        System.out.print(ch);
    }
}

结果为:good and gbc

  • 3、写出下列程序的运行结果

结果为:

true
false
true
true
true
false
true

多线程

多线程中start()和run()方法

  • 1、写出下列程序的运行结果

public class TestRunAndStart {
    public static void main(String[] args) {
        Thread t = new Thread() {
            public void run() {
                pong();
            }
        };
        t.run();
        System.out.print("ping");
    }
    
    static void pong() {
        System.out.print("pong");
    }
}

结果为:pongping

猜你喜欢

转载自www.cnblogs.com/hglibin/p/9425306.html