java基础实践

2020-08-09  本文已影响0人  azmohan
  1. 如果下面程序编译后生成Test.class,执行java Test Lily Lucy Mary Alice Rose,请写出运行结果:
class Test  {
    public static void main(String args[]) {
        String foo = args[1];
        String bar = args[2];
        String baz = args[3];
        System.out.println("foo:"+foo);
        System.out.println("bar:"+bar);
        System.out.println("baz:"+baz);
    }
}
  1. 请输出下面程序的运行结果(若编译不过,或运行出错,直接写出错误):
class Test {
    public static void main(String args[]) {
        int i = 1,j = 10;
        do {
            if(i++ > --j) continue;
            System.out.println("i = " + i + ",j = " + j);
        } while (i < 5) ;
    }
}
  1. 请输出下面程序的运行结果(若编译不过,或运行出错,直接写出错误):
class Test  {
    public static void main(String args[])  {
        int i = 0xfffffff1;
        int j = ~i;
        System.out.println("j = "+j);
    }
}
  1. 请输出下面程序的运行结果(若编译不过,或运行出错,直接写出错误):
class Test  {
    public static void main(String args[])  {
        float f = 4.2f;
        Float g = new Float(4.2f);
        Double d = new Double(4.2);
        System.out.println("f = g :" + (f == g));
        System.out.println("g = g :" + (g == g));
        System.out.println("d = f :" + (d == f));
        System.out.println("d.equals(f) :" + d.equals(f));
        System.out.println("d.equals(g) :" + d.equals(g));
        System.out.println("g.equals(4.2):" + g.equals(4.2));
    }
}
  1. 请输出下面程序的运行结果(若编译不过,或运行出错,直接写出错误):
public class Test  {
    public static int add3(Integer i) {
        int val = i.intValue();
        val += 3;
       return  i = new Integer(val);
    }

    public static void main(String args[]) {
        Integer i = new Integer(0);
        System.out.println(add3(i));
        System.out.println(i.intValue());
    }
}
  1. 请输出下面程序的运行结果(若编译不过,或运行出错,直接写出错误):
class Test {
    public static void stringReplace(String text) {
        text = text.replace("j","i");
    }

    public static void stringBufferAppend(StringBuffer text) {
        text = text.append("c");
    }

    public static void main(String args[]) {
        String txtStr = new String("java");
        StringBuffer txtBuffer = new StringBuffer("java");
        stringReplace(txtStr);
        stringBufferAppend(txtBuffer);
        System.out.println(txtStr + txtBuffer);
    }
}

  1. 请输出下面程序的运行结果(若编译不过,或运行出错,直接写出错误):
class Test {
    public static void main(String [] args) {
        int [][] array = new int[3][4];
        System.out.println("array.length = " + array.length);
        System.out.println("array[0].length = " + array[0].length);
    }
}
  1. 请输出下面程序的运行结果(若编译不过,或运行出错,直接写出错误):
import java.io.IOException;
class ExceptionTest {
    public static void main (String args[]) {
        try {
            methodA();
        } catch (Exception e) {
            System.out.println("caught Exception");
        } catch (IOException e) {
            System.out.println("caught IOException");
        }
    }

    private static void methodA () throws IOException,Exception {
        throw new IOException();
    }
}
  1. 请输出下面程序的运行结果(若编译不过,或运行出错,直接写出错误):
class Test {
    public static String output = "";
    public static void foo(int i) {
        try {
            if(i == 1) {
                throw new Exception();
            }
            output += "1";
        } catch (Exception e) {
            output += "2";
            return;
        } finally {
            output += "3";
        }
        output += "4";
    }

    public static void main(String []args) {
        foo(0);
        foo(1);
        System.out.println(output);
    }
}
  1. 请输出下面程序的运行结果(若编译不过,或运行出错,直接写出错误):
class Test {
    public static int sValue = 2;

    public static void showValue() {
        int sValue = 3;
        System.out.println("showValue sValue = " + sValue);
    }

    public static void main(String [] args) {
        showValue();
        System.out.println("main sValue = " + sValue);
    }
}
  1. 请输出下面程序的运行结果(若编译不过,或运行出错,直接写出错误):
class Sub {
    public void show() {
        System.out.println("I am Sub show");
    }

    public void show(String str) {
        System.out.println("show str = " + str);
    }
}

class Derive extends Sub {
    public void show() {
        System.out.println("I am Derive show");
    }
}

class Base extends Derive {
    public void show(String str) {
        System.out.println("Base show str = " +str);
    }

    public static void main(String [] args) {
        Sub sub = new Base();
        sub.show("me");
        sub.show();
    }
}
  1. 请输出下面程序的运行结果(若编译不过,或运行出错,直接写出错误):
interface IClick {
    void onClick();
}

abstract class AClick {
    public void onClick() {
        System.out.println("AClick onClick");
    }
}

class Base extends AClick implements IClick {
    public void onClick() {
        System.out.println("Base onClick");
    }

    public static void main(String [] args) {
        IClick click = new Base();
        click.onClick();
        AClick aclick = new Base();
        aclick.onClick();
    }
}
  1. 请输出下面程序的运行结果(若编译不过,或运行出错,直接写出错误):
class Sub {
    public void test() {
        System.out.println("Sub test");
    }
}

class Derive extends Sub {
    private void test() {
        System.out.println("Derive test");
    } 

    public static void main(String [] args) {
        Sub sub = new Derive();
        sub.test();
    }
}
  1. 请输出下面程序的运行结果(若编译不过,或运行出错,直接写出错误):
class Sub {
    public Sub() {
        showSelf(getClass().getName(),"null");
    }
    public Sub(String str) {
        showSelf(getClass().getName(),str);
    }

    public void showSelf(String className,String str) {
        System.out.println("I am " + className +  ",str = " + str);
    }

    public static void main(String [] args) {
        Sub sub = new Derive("lily");
    }
}

class Derive extends Sub {
    public Derive(String str) {
        showSelf(getClass().getName(),str);
    }
}
  1. 请输出下面程序的运行结果(若编译不过,或运行出错,直接写出错误):
class Sub {
    public void have100M() {
        System.out.println("I have 100 million");
    }
}

class Derive extends Sub {
    public static void main(String [] args) {
        Derive derive = new Derive();
        derive.have100M();
    }
}
  1. 请输出下面程序的运行结果(若编译不过,或运行出错,直接写出错误):
abstract class Grandpa {
    public abstract void have100M();
}

abstract class Father extends Grandpa {
    public void dispose() {
        System.out.println("Come on,My boy.Better city,Better life");
    }
}

class You extends Father {
    public void have100M() {
        System.out.println("I have 100 million");
    }
    public static void main(String [] args) {
        Grandpa pa = new You();
        pa.have100M();
    }
}
  1. 请指出这些getNumber方法哪些override,哪些是overload?:
class Sub {
    public int getNumber() {
        return 0;
    }

    public int getNumber(int a) {
        return a;
    }
}

class Derive extends Sub {
    public int getNumber() {
        return 1;
    }

    public int getNumber(int a,int b) {
        return a + b;
    }

    public int getNumber(String a) {
        return Integer.valueOf(a);
    }
}
  1. 请输出下面程序的运行结果(若编译不过,或运行出错,直接写出错误):
class Test {
    public static void main(String [] args) {
        Thread thread = new Thread("glThread") {
            public void run() {
                System.out.println(Thread.currentThread().getName() + " is runing");
            }
        };
        thread.run();
    }
}
  1. 请输出下面程序的运行结果(若编译不过,或运行出错,直接写出错误):
class Test {
    public static void main(String [] args) {
        Runnable runnable = new Runnable() {
            public void run() {
                System.out.println(Thread.currentThread().getName() + " is running");
            }
        };
        Thread thread = new Thread(runnable,"glThread");
        thread.run();
        thread.start();
    }
}
  1. 请输出下面程序的运行结果(若编译不过,或运行出错,直接写出错误):
class Test {
    public static void main(String [] args) {
        Runnable runnable = new Runnable() {
            public void run() {
                System.out.println(Thread.currentThread().getName() + " is running");
            }
        };
        Thread thread = new Thread(runnable,"glThread");
        thread.start();
        thread.start();
    }
}
上一篇 下一篇

猜你喜欢

热点阅读