2019春招Java开发类试卷
    试题来源:牛客网https://www.nowcoder.com/test/14266493/summary

    1.以下程序输出的结果,正确的是?

    1. public class Main {
    2. public static void main(String[] args) {
    3. System.out.println("A");
    4. new Main();
    5. new Main();
    6. }
    7. public Main() {
    8. System.out.println("B");
    9. }
    10. {
    11. System.out.println("C");
    12. }
    13. static {
    14. System.out.println("D");
    15. }
    16. }

    DCABB
    DABCBC
    DACBCB
    DACBB
    答案:C
    答案解析:
    解析:在同一类里面,首先静态代码块在类加载的时候就执行了,而且只执行一次,不论你是否new了对象,而构造代码块构造方法是在每次创建对象时都会被调用,并且构造代码块的执行次序优先构造方法

    2.执行以下程序后,x和y的值分别是多少?

    1. public class Main {
    2. private static int x = 10;
    3. private static Integer y = 10;
    4. public static void updateX(int value) {
    5. value = 3 * value;
    6. }
    7. public static void updateY(Integer value) {
    8. value = 3 * value;
    9. }
    10. public static void main(String[] args) {
    11. updateX(x);
    12. updateY(y);
    13. }
    14. }

    10,10
    10,30
    30,10
    30,30
    答案:A

    3.执行以下程序后,输出结果正确的是?

    1. public class Main {
    2. public static void main(String[] args) {
    3. String s1 = "abc";
    4. String s2 = "abc";
    5. System.out.println(s1 == s2);
    6. String s3 = new String("abc");
    7. System.out.println(s1 == s3);
    8. }
    9. }

    true true
    true false
    false fasle
    false true
    答案:B

    4.以下代码会输出什么结果?

    1. public class Test {
    2. public static void main(String[] args) {
    3. System.out.println(Test2.a);
    4. }
    5. }
    6. class Test2{
    7. public static final String a="JD";
    8. static {
    9. System.out.print("OK");
    10. }
    11. }

    只有JD
    只有OK
    输出 JDOK
    输出 OKJD
    答案:A

    5.以下代码会输出什么结果?

    1. public class Test {
    2. public static void main(String[] args) {
    3. System.out.println(Test2.a);
    4. }
    5. }
    6. class Test2{
    7. public static final String a=new String("JD");
    8. static {
    9. System.out.print("OK");
    10. }
    11. }

    只有JD
    只有OK
    输出 JDOK
    输出 OKJD
    答案:D

    6.请阅读代码选择出该段代码的输入结果。

    1. public class Test {
    2. public static void main(String[] args) {
    3. System.out.print(B.c);
    4. }
    5. }
    6. class A {
    7. static {
    8. System.out.print("A");
    9. }
    10. }
    11. class B extends A{
    12. static {
    13. System.out.print("B");
    14. }
    15. public final static String c = "C";
    16. }

    AB
    ABC
    C
    BC
    答案:C

    7.执行以下程序后,输出结果正确的是?

    1. public class Main {
    2. public static void main(String[] args) {
    3. System.out.print(fun1());
    4. }
    5. public static String fun1() {
    6. try {
    7. System.out.print("A");
    8. return fun2();
    9. } finally {
    10. System.out.print("B");
    11. }
    12. }
    13. public static String fun2() {
    14. System.out.print("C");
    15. return "D";
    16. }
    17. }

    ABCD
    ACDB
    ACBD
    不确定
    答案:C

    8.JDK1.8中,执行以下程序后,该list进行了几次扩容?

    1. import java.util.ArrayList;
    2. import java.util.List;
    3. public class Main {
    4. public static void main(String[] args) {
    5. List<String> list = new ArrayList<>();
    6. for(int i=0;i<100;i++){
    7. list.add("a");
    8. }
    9. }
    10. }

    4
    5
    6
    7
    答案:C
    答案解析初始值为10,增长幅度为1.5倍,答案为10->15->22->33->49->73->109,故答案为6次。
    image.png
    通过反射的实验,可以在每次添加一个元素时,打印内部数组的长度,即可得到容量。

    1. import java.lang.reflect.Field;
    2. import java.util.ArrayList;
    3. import java.util.List;
    4. public class Main {
    5. public static void main(String[] args) {
    6. List<String> list = new ArrayList<>();
    7. Class<? extends List> clazz = list.getClass();
    8. try {
    9. Field ed =clazz.getDeclaredField("elementData");
    10. ed.setAccessible(true);
    11. for(int i=0;i<100;i++){
    12. list.add("a");
    13. Object[] arr = (Object[])ed.get(list);
    14. System.out.println(arr.length);
    15. }
    16. } catch (NoSuchFieldException e) {
    17. // TODO Auto-generated catch block
    18. e.printStackTrace();
    19. } catch (SecurityException e) {
    20. // TODO Auto-generated catch block
    21. e.printStackTrace();
    22. } catch (IllegalArgumentException e) {
    23. // TODO Auto-generated catch block
    24. e.printStackTrace();
    25. } catch (IllegalAccessException e) {
    26. // TODO Auto-generated catch block
    27. e.printStackTrace();
    28. }
    29. }
    30. }