目录
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) 以上答案都不是