zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Java面试题: 基础考核-拆箱装箱, 数据类型, MAP

JAVA面试题Map基础 数据类型 装箱 拆箱 考核
2023-09-11 14:15:39 时间

目录

1. 以下程序输出内容是?

2.以下程序输出内容是?

3. 以下程序输出内容是?

参考答案


1. 以下程序输出内容是?

public class Parsing {
    /**
     * Returns Integer corresponding to s, or null if s is null.
     * @throws NumberFormatException if s is nonnull and
     * doesn't represent a valid integer
     */
    public static Integer parseInt(String s) {
        return (s == null) ?
                (Integer) null : Integer.parseInt(s);
    }
    public static void main(String[] args) {
        System.out.println(parseInt("-1") + " " +
                parseInt(null) + " " +
                parseInt("1"));
    }
}

(a) 运行时异常

(b) -1 null 1

(c) -1 0 1

(d) 编译错误

2.以下程序输出内容是?

import java.util.Random;

public class Hamlet {

    public static void main(String[] args) {

        Random rnd = new Random();

        boolean toBe = rnd.nextBoolean();

        Number result = (toBe || !toBe) ?

                new Integer(3) : new Float(1);

        System.out.println(result);

    }

}

(a) 运行时异常

(b) 3

(c) 1.0

(d) 以上答案都不是

 

3. 以下程序输出内容是?

public class MyMap { public static void main(String[] args) {

    Map map = new IdentityHashMap<>();
    map.put(1, "Hello");
    map.putIfAbsent(1, "World");
    print(map.get(1));
    print(map.size());
    map.put(1024, "A");
    map.putIfAbsent(1024, "B");
    print(map.get(1024));
    print(map.size());

}

    private static void print(Object object) {
        System.out.print(object + " ");
    }
}

(a) Hello 1 null 3

(b) World 1 null 2

(c) Hello 2 null 2

(d) 以上答案都不是

参考答案

参考链接: https://glory.blog.csdn.net/article/details/117280159