解答H大的公佈的 10 道 Java 測試題

package constxiong.interview;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * 解答阿里的十道題
 * @author ConstXiong
 * @date 2020-04-24 09:29:51
 */
public class TestAli10Questions {
	
	public static void main(String[] args) {
		t1();
		t2();
		t3();
		t4();
		t5();
		t7();
//		t8();
//		t9();
		t10();
	}
	
	/**
	 * float a = 0.125f; double b = 0.125d; System.out.println((a - b) == 0.0); 代碼的輸出結果是什麼?
	 * A. true
	 * B. false
	 * 
	 * 答案:A
	 */
	private static void t1() {
		float a = 0.125f;
		double b = 0.125d;
		System.out.println((a - b) == 0.0);
	}
	
	/**
	 * double c = 0.8; double d = 0.7; double e = 0.6; 那麼 c-d 與 d-e 是否相等?
	 * A. true
	 * B. false
	 * 
	 * 答案:B
	 */
	private static void t2() {
		double c = 0.8;
		double d = 0.7;
		double e = 0.6;
		System.out.println(c-d == d-e);
	}
	
	/**
	 * System.out.println(1.0 / 0); 的結果是什麼?
	 * A. 拋出異常
	 * B. Infinity
	 * C. NaN
	 * 
	 * 答案:B
	 */
	private static void t3() {
		System.out.println(1.0 / 0);
	}
	
	/**
	 * System.out.println(0.0 / 0.0); 的結果是什麼?
	 * A. 拋出異常
	 * B. Infinity
	 * C. NaN
	 * D. 1.0
	 * 
	 * 答案:C
	 */
	private static void t4() {
		System.out.println(0.0 / 0.0);
	}
	
	/**
	 * >> 和 >>> 的區別是?
	 * A. 任何整數沒有區別
	 * B. 負整數一定沒有區別
	 * C. 浮點數可以 >> 運算,但是不可以 >>> 運算
	 * D. 正整數一定沒有區別
	 * 
	 * 分析:A 肯定不正確; >>> 是無符號右移,負數經過 >>> 後,變爲正數,B 不正確; C 編譯出錯; D 正解
	 * 答案:D 
	 */
	private static void t5() {
		float f = -1.1f;
		System.out.println( -8 >>> 1);
//		f >> 1; //Syntax error on token ">>", invalid AssignmentOperator
//		f >>> 1;//Syntax error on token ">>>", >>>= expected
		int i = 10;
		System.out.println(i >> 10);
		System.out.println(i >>> 10);
	}
	
	/**
	 * 某個類有兩個重載方法:void f(String s) 和 void f(Integer i),那麼 f(null) 的會調用哪個方法?
	 * A. 前者
	 * B. 後者
	 * C. 隨機調用
	 * D. 編譯出錯
	 * 
	 * 直接傳 null,編譯器無法根據字段類型辨別調用哪個方法;改爲 String s = null 或 Integer i = null 可以
	 * 答案:D
	 */
	private static void t6() {
		TestAli10Questions test = new TestAli10Questions();
//		test.f(null);//編譯報錯 The method f(String) is ambiguous for the type TestAli10Questions
		String s = null;
		Integer i = null;
		test.f(s);
		test.f(i);
	}
	
	/**
	 * 某個類有兩個重載方法:void g(double d) 和 void g(Integer i),那麼 g(1) 的會調用哪個方法?
	 * A. 前者
	 * B. 後者
	 * C. 隨機調用
	 * D. 編譯出錯
	 * 
	 * 答案:A
	 */
	private static void t7() {
		TestAli10Questions test = new TestAli10Questions();
		test.g(1);
	}
	
	/**
	 * String a = null; switch(a) 匹配 case 中的哪一項?
	 * A. null
	 * B. "null"
	 * C. 不與任何東西匹配,但不拋出異常
	 * D. 直接拋出異常
	 * 
	 * 分析:拋出Exception in thread "main" java.lang.NullPointerException
	 * 答案:D
	 */
	private static void t8() {
		String a = null; 
		switch(a) {
			case "null" :
				System.out.println(a);
				break;
		}
	}
	
	/**
	 * <String, T, Alibaba> String get(String string, T t) { return string; } 此方法:
	 * A. 編譯錯誤,從左往右第一個 String 處
	 * B. 編譯錯誤,T 處
	 * C. 編譯錯誤,Alibaba 處
	 * D. 編譯正確
	 * 
	 * 答案:D
	 */
	private static void t9() {
	}
	
	/**
	 * HashMap 初始容量 10000 即 new HashMap(10000),當往裏 put 10000 個元素時,需要 resize 幾次(初始化的那次不算)?
	 * A. 1 次
	 * B. 2 次
	 * C. 3 次
	 * D. 0 次
	 * 
	 * 分析:初始化:HashMap 構造方法只會調用 tableSizeFor(initialCapacity) 方法,不會調用 resize() 方法, threshold=16384 table=null;
	 *     第一次 put 時:調用resize()方法,threshold=12288 table=HashMap.Node[16384]
	 *     之後的put不會進行擴容
	 * 答案:A
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private static void t10() {
		Map map = new HashMap(10000);
		printMapCapacity(map);
		for (int i = 0; i < 10000; i++) {
			map.put(i, i);
//			printMapCapacity(map);
		}
		printMapCapacity(map);
	}
	
	/**
	 * 打印map信息
	 * @param map
	 */
	private static void printMapCapacity(Map map) {
		Class<? extends Map> clazz = map.getClass();
		try {
			Field thresholdFeild = clazz.getDeclaredField("threshold");
			Field modCountFeild = clazz.getDeclaredField("modCount");
			Field tableFeild = clazz.getDeclaredField("table");
			thresholdFeild.setAccessible(true);
			modCountFeild.setAccessible(true);
			System.out.println("threshold:" + thresholdFeild.get(map));
			System.out.println("modCount:" + modCountFeild.get(map));
		} catch(Exception e) {
			e.printStackTrace();
		}
		
	}
	
	void f(String s) {
	}
	
	void f(Integer i) {
	}
	
	void g(double d) {
		System.out.println("param is double");
	}
	
	void g(Integer d) {
		System.out.println("param is Integer");
	}

	<String, T, Alibaba> String get(String string, T t) {
		return string;
	}
	
}

 

打印結果:

true
false
Infinity
NaN
2147483644
0
0
param is double
threshold:16384
modCount:0
threshold:12288
modCount:10000

 

PS:

  • 這 10 道題,不借助 IDE 調試,能答對到 8 道以上,基本算大神級別了...
  • 解題使用 JDK  1.8.0_141-b15,不同版本的 JDK,可能不會完全相同

 

 


【Java面試題與答案】整理推薦

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章