Java笔试题汇总

Java笔试题汇总

1. 字符串之间的比较

public class StringDemo {
    private static final String MESSAGE = "csdn";
    public static void main(String[] args) {
        String a = "cs" + "dn";
        String b = "cs";
        String c = "dn";
        System.out.println(a == MESSAGE);
        System.out.println((b + c) == MESSAGE);
    }
}

输出结果 Ture False
2.

class Example {
    public static void main(String[] args) {
        String strl = "hello";
        String str2 = "he" + new String("llo");
        System.err.println(strl ==str2);
    }
}

输出:false
3.

2. 多态相关

public class Person {
    private String name = "Person";
    int age = 0;
}

public class Child extends Person {
    public String grade;
    public static void main(String[] args) {
        Person p = new Child();
        System.out.println(p.name);

    }
}

结果:编译异常

3. 继承相关

class Base {
    Base() {
        System.out.print("Base");
    }
}

public class Alpha extends Base {
    public static void main(String[] args) {
        new Alpha();
        new Base();  
    }
}

输出结果:BaseBase
2.

class Test extends Father{
    private String name="test";
    public static void main(String[] args){
        Test test = new Test();
        System.out.println(test.getName());
    }
}

class Father{
    private String name="father";
    public String getName() {
        return name;
    }
}

输出: father

3、

class Student {
    static {
        System.out.println("Student 静态代码块");
    }

    {
        System.out.println("Student 构造代码块");
    }

    public Student() {
        System.out.println("Student 构造方法");
    }
}

class StudentDemo {
    static {
        System.out.println("studentDemo静态代码块");
    }

    public static void main(String[] args) {
        System.out.println("我是main方法");
        Student s1 = new Student();
        Student s2 = new Student();
    }
}

输出:
studentDemo静态代码块
我是main方法
Student 静态代码块
Student 构造代码块
Student 构造方法
Student 构造代码块
Student 构造方法

4. 异常捕获相关

public class TestDemo {
    public static String output = "";
    public static void foo(int i) {
        try {
            if (i == 1) {
                throw new Exception();
            }
        } 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);
    }
}

输出:3423
2.

public class TestDemo {
    public static int aMethod(int i) throws Exception {
        try {
            return 10 / i;
        } catch (Exception ex) {
            throw new Exception("exception in aMethod");
        } finally {
            System.out.printf("finally");
        }
    }

    public static void main(String[] args) {
        try {
            aMethod(0);
        } catch (Exception ex) {
            System.out.printf("exception in main");
        }
        System.out.printf("finished");
    }
}

输出:finallyexception in mainfinished
3.

public class TestDemo {
    public static int aMethod(int i) throws Exception {
        try {
            return i / 10;
        } catch (Exception ex) {
            throw new Exception("exception in aMethod");
        } finally {
            System.out.printf("finally");
        }
    }

    public static void main(String[] args) {
        try {
            aMethod(0);
        } catch (Exception ex) {
            System.out.printf("exception in main");
        }
        System.out.printf("finished");
    }
}

输出:finallyfinished
4.

class Demo{
    public int add(int a,int b){
        try{
            return a+b;
        }catch(Exception e){
            System.out.println("catch 语句块");
        }finally{
            System.out.println("finally语句块");
        }
        return 0;
    }

    public static void main(String[] args){
        Demo demo = new Demo();
        System.out.println("和是:"+demo.add(9,34));
    }
}

输出:
finally语句块
和是:43
5.

class Test1 {
    public int div(int a, int b) {
        try {
            return a / b;
        }catch(NullPointerException e){
            System.out.println("NullPointerException");
        }
        catch (ArithmeticException e) {
            System.out.println("ArithmeticException");
        } catch(Exception e){
            System.out.println("Exception");
        }finally {
            System.out.println("finally");
        }
        return 0;
    }

    public static void main(String[] args) {
        Test1 demo = new Test1();
        System.out.println("商是:" + demo.div(9, 0));

    }

}

输出:
ArithmeticException
finally
商是:0

5. 值传递与地址传递

class Example {
    String str = new String("good");
    char[] ch = { 'a', 'b', 'c' };

    public static void main(String args[]) {
        Example ex = new Example();
        ex.change(ex.str, ex.ch);
        System.out.print(ex.str + " and ");
        System.out.print(ex.ch);
    }

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

输出:good and gbc
2.

public class SendValue {
    public String str = "6";

    public static void main(String[] args) {
        SendValue sv = new SendValue();
        sv.change(sv.str);
        System.out.println(sv.str);
    }

    public void change(String str) {
        str = "10";
    }
}

输出;
6

6. 静态成员

public class HasStatic {
    private static int x = 100;
    public static void main(String[] args) {
        HasStatic hsl = new HasStatic();
        hsl.x++;
        HasStatic hs2 = new HasStatic();
        hs2.x++;
        hsl = new HasStatic();
        hsl.x++;
        HasStatic.x--;
        System.out.println("x=" + x);
    }
}

输出:x=102
个人理解:静态变量是类级别的,被所有实例对象共享,因此一旦初始化,它们在类加载时分配,并且任何实例对象对其的修改都会影响其他实例对象,因为它们引用同一份静态变量。
上述题目中:x是静态变量,被实例对象hsl,hs2共享,当实例对象对静态变量进行修改时,会影响这唯一的静态变量。
2.

public class B {
    public static B t1 = new B();
    public static B t2 = new B();

    {
        System.out.println("构造块");
    }

    static {
        System.out.println("静态块");
    }

    public static void main(String[] args) {
        B t = new B();
    }
}

输出:
构造块
构造块
静态块
构造块
3.

public class B {
    public static B t1 = new B();
    public static B t2 = new B();
    {
        System.out.println("构造块");
    }
    static {
        System.out.println("静态块");
    }
    public static void main(String[] args) {
        B t = new B();
    }
}

输出:
构造块
构造块
静态块
构造块

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值