Java奇葩笔试题

1、下面代码中,在if处填写什么代码,可以使得输出结果为:AB

1
2
3
4
5
6
7
8
9
public static void main(String[] args) {

        if (          ){//填写条件
            System.out.print("A");
        } else {
            System.out.print("B");
        }
    }
}

2、 运算符问题,下面代码分别输出什么?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
package test;
public class Test {
    public static void main(String[] args) {
        int i1 = 10, i2 = 10;
        System.err.println("i1 + i2 = " + i1 + i2);
        System.err.println("i1 - i2 = " + i1 - i2);
        System.err.println("i1 * i2 = " + i1 * i2);
        System.err.println("i1 / i2 = " + i1 / i2);
    }
}

3、下面代码的结果是什么?还是抛出异常?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
package test;

public class Test {

    public void myMethod(String str) {
        System.err.println("string");
    }

    public void myMethod(Object obj) {
        System.err.println("object");
    }

    public static void main(String[] args) {
        Test t = new Test();
        t.myMethod(null);
    }
}

4、下面代码的输出结果是什么?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
package test;

public class Test {

    public static void main(String[] args) {
        double val = 11.5;
        System.err.println(Math.round(val));
        System.err.println(Math.floor(val));
        System.err.println(Math.ceil(val));
    }
}

5、 下面代码的结果是什么?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package test;

public class Test extends Base {

    public static void main(String[] args) {
        Base b = new Test();
        b.method();

        Test t = new Test();
        t.method();
    }

    @Override
    public void method() {
        System.err.println("test");
    }

}

class Base {
    public void method() throws InterruptedException {
        System.err.println("base");
    }
}

6、以下代码的结果是什么?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
package test;

public class Test extends Base {

    public static void main(String[] args) {
        new Test().method();
    }

    public void method() {
        System.err.println(this.getClass().getName());
        System.err.println(super.getClass().getName());
        System.err.println(this.getClass().getSuperclass().getName());
        System.err.println(super.getClass().getSuperclass().getName());
    }

}

class Base {
}

7、true or false?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
package test;

public class Test {

    public static void main(String[] args) {
        String str1 = new String("abc");
        String str2 = new String("abc");
        System.err.println(str1.equals(str2));

        StringBuffer sb1 = new StringBuffer("abc");
        StringBuffer sb2 = new StringBuffer("abc");
        System.err.println(sb1.equals(sb2));
    }
}

8、输出的结果是什么?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package test;

public class Test {

    public static void main(String[] args) {
        System.err.println(new Test().method1());
        System.err.println(new Test().method2());
    }

    public int method1() {
        int x = 1;
        try {
            return x;
        } finally {
            ++x;
        }
    }

    public int method2() {
        int x = 1;
        try {
            return x;
        } finally {
            return ++x;
        }
    }
}

9、true or false?

1
2
3
4
5
6
7
8
package test;

public class Test {

    public static void main(String[] args) {
        System.err.println(12 - 11.9 == 0.1);
    }
}

10、以下代码输出是什么?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
package test;

import java.math.BigInteger;

public class Test {




    public static void main(String[] args) {
        BigInteger one = new BigInteger("1");
        BigInteger two = new BigInteger("2");
        BigInteger three = new BigInteger("3");
        BigInteger sum = new BigInteger("0");
        sum.add(one);
        sum.add(two);
        sum.add(three);
        System.out.println(sum.toString());
    }
}

答案:

(1)、 System.out.printf("A") == null

(2)、

第一行结果i1 + i2 = 1010

第二行异常,字符串不能和数字做减法

第三行i1 * i2 = 100

第四行i1 / i2 = 1

(3)、尽可能的从子类找,因此是string

(4)、要注意返回类型long,double,double,答案是

12

11.0

12.0

(5)、 答:异常。因为子类重写父类方法时也要抛出异常。

(6)、

test.Test

test.Test

test.Base

test.Base

(7)、

true

false

(8)、

1

2

(9)、 false

(10)、0

转载自:http://my.eoe.cn/zhongcx/archive/24063.html

原文地址:https://www.cnblogs.com/zsw-1993/p/4879456.html