美文网首页
java基础实践

java基础实践

作者: azmohan | 来源:发表于2020-08-09 15:17 被阅读0次
  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();
    }
}

相关文章

  • Awesome Java

    基础 Java 入门与实践 Java 语法清单 Java 8 系列之重新认识 HashMap Java 浮点数精确...

  • java基础实践

    如果下面程序编译后生成Test.class,执行java Test Lily Lucy Mary Alice Ro...

  • 编程书籍

    Java Java基础书单:《Java编程思想》《深入理解Java虚拟机:JVM高级特性与最佳实践》《Head F...

  • Java方向2017校招书单

    Java方向2017校招书单 待读书籍 分布式java应用:基础与实践深入剖析Tomcat 基础课程书籍 计算机组...

  • Java 基础

    Java 基础01Java开发入门 Java 基础02Java编程基础 Java 基础03面向对象 Java 基础...

  • Java学习

    1. 总览 2.书籍推荐 2.1 基础部分 深入理解Java虚拟机:JVM高级特性与最佳实践(第2版) Java并...

  • 技术体系

    一,java核心 java基础,jvm,算法,多线程,设计模式 Java基础:java基础相关,全栈java基础 ...

  • 2019-01-06

    大型网站系统与Java中间件实践 第一章 分布式系统的基础知识 曾宪杰的《大型网站系统与Java中间件实践》第一章...

  • 面试题汇总

    1.Java基础面试问题 Java基础之基础问题 Java基础之面向对象 Java基础之数据结构 Java基础之I...

  • Java反射基础与实践

    本文和大家聊聊Java反射。 什么是Java反射(Reflection)? 在程序运行的过程中,能够动态的检索类相...

网友评论

      本文标题:java基础实践

      本文链接:https://www.haomeiwen.com/subject/vrwqdktx.html