一、下列關於String類的說法,正確的是
A .String類爲final類,不能擁有子類。
B .String常量對象是用雙引號括起的字符序列。
C .在String s = new String(「Hello!」);中,對象變量s中存放的是實體。
D .System.out.println(s);能夠輸出String對象的引用。
答案:AB
解析:C項:對象變量存放的是引用。D項:輸出的是對象的實體。java
二、下列屬於引用數據類型的是
A .String
B .char
C .用戶自定義的Student類類型
D .int
答案:AC正則表達式
三、實體相同但引用不一樣的兩個字符串,使用「==」比較結果爲false。
A .true
B .false
答案:Adom
四、String類中的length()方法用來獲取一個String對象的字符序列的長度,單位爲字節。
A .true
B .false
答案:B
解析:長度等於字符串中 Unicode 代碼單元的數量。測試
五、下列經過測試的斷言語句有幾個?this
①assertEquals(true, "123".matches("\d+"));命令行
②assertEquals(true, "3.5".matches("\d\.\d"));code
③assertEquals(true, "C:\Windows".matches("C:\Windows"));對象
④assertEquals(true, "hello hello".matches("\b(?
⑤assertEquals("rplcmnt","replacement".replaceAll("[aeiou]","*"));字符串
⑥assertEquals(true, ""Hello"".matches("(["'])[^\"']*\1"));
⑦assertEquals("##","xfooxxxxxxfoo".replaceAll(".*?foo","#"));
⑧assertEquals("Let's meet at [TIME].","Let's meet at 12:38.".replaceAll("((1|0?)[0-9]|2[0-3]):([0-5][0-9])","[TIME]"));
A .7個
B .6個
C .4個
D .3個
答案:B
解析:③:要查找\自己,須要用\。⑥:注意字符轉義,應爲""Hello"".matches("(["'])[^\"']*\1")。
六、下列關於正則表達式的說法,正確的是
A .\ba\w*\b匹配以字母a開頭的單詞
B .\d+匹配1個或更多連續的數字。
C .\b\w{6}\b 匹配6個及以上字符的單詞。
D .[0-9]表明的含意與\d就是徹底一致的:一位數字
E .\S+匹配不包含空白符的字符串。
F .(\d{1,3}.){3}\d{1,3}用來匹配 IP地址。
答案:ABDE
解析:C項:\b\w{6}\b 匹配恰好6個字符的單詞。F項: IP地址中每一個數字都不能大於255,該表達式忽略了這個約束條件。正確的是((2[0-4]\d|25[0-5]|[01]?\d\d?).){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)。
七、下列關於substring方法的說法,正確的是
A .public String substring(int beginIndex)返回的子字符串從指定索引處的字符開始,直到此字符串末尾。
B .public String substring(int beginIndex, int endIndex) 子字符串從指定的 beginIndex 處開始,直到索引 endIndex處的字符。
C ."emptiness".substring(9)返回值爲""。
D ."smiles".substring(1, 5)返回值爲"mile"。
E .若beginIndex 大於 endIndex,則substring(int beginIndex, int endIndex)返回-1。
答案:ACD
解析:查詢API。B項:直到索引 endIndex - 1 處的字符。E項:拋出IndexOutOfBoundsException異常。
八、如下兩段程序的輸出結果是徹底同樣的。
//【代碼一】
StringTokenizer st = new StringTokenizer("this is a test"); while (st.hasMoreTokens()) { System.out.println(st.nextToken()); }
//【代碼二】
String[] result = "this is a test".split("\\s"); for (int x=0; x<result.length; x++) System.out.println(result[x]);
A .true
B .false
答案:A
九、What is the output of the following code?(下面代碼的運行結果是?)
LocalDate date = LocalDate.of(2018, Month.APRIL, 30); date.plusDays(2); date.plusYears(3); System.out.println(date.getYear() + " " + date.getMonth() + " "+ date.getDayOfMonth());
A .2018 APRIL 2
B .2018 APRIL 30
C .2018 MAY 2
D .2021 APRIL 2
E .2021 APRIL 30
F .2021 MAY 2
G .A runtime exception is thrown.
答案:B
解析:The date starts out as April 30, 2018. Since dates are immutable and the plus methods have their return values ignored, the result is unchanged. Therefore, Option B is correct.
十、What is the output of the following code?(下面代碼的運行結果是?)
LocalDate date = LocalDate.of(2018, Month.APRIL, 40); System.out.println(date.getYear() + " " + date.getMonth() + " "+ date.getDayOfMonth());
A .2018 APRIL 4
B .2018 APRIL 30
C .2018 MAY 10
D .Another date
E .The code does not compile.
F .A runtime exception is thrown.
答案:F
解析:Java throws an exception if invalid date values are passed. There is no 40th day in April—or any other month for that matter.
十一、調用 new Random(seed) 等效於:Random rnd = new Random(); rnd.setSeed(seed);
A .true
B .false
答案:A
解析:查詢API。
十二、對於以下代碼,下列哪一個敘述是正確的?
public class E{ public static void main(String[] args){ String strOne="bird"; String strTwo=strOne; strOne="fly"; System.out.println(strTwo); } }
A .程序編譯出現錯誤。
B .程序標註的【代碼】的輸出結果是bird。
C .程序標註的【代碼】的輸出結果是fly。
D .程序標註的【代碼】的輸出結果是null。
答案:B
1三、對於以下代碼,下列哪一個敘述是正確的?
public class E { public static void main (String args[]) { String s1 = args[1]; String s2 = args[2]; String s3 = args[3]; System.out.println(s3); } }
A .程序出現編譯錯誤。
B .無編譯錯誤,在命令行執行程序:「java E I love this game」,程序輸出this。
C .無編譯錯誤,在命令行執行程序:「java E let us go」,程序無運行異常。
D .無編譯錯誤,在命令行執行程序:「java E 0 1 2 3 4 5 6 7 8 9」程序輸出3。
答案:D
1四、下列哪一個敘述是錯誤的? A ."9dog".matches("\ddog")的值是true。 B ."12hello567".replaceAll("[123456789]+","@")返回的字符串是@hello@。 C .new Date(1000)對象含有的時間是公元后1000小時的時間 D ."\hello\n"是正確的字符串常量。 答案:C (說明:P是書上頁碼,詳情請看書)