第十一題:
22. StringBuilder sb1 =new StringBuilder("123");
23. String s1 = "123";
24. // 此處插入代碼
25. System.out.println(sb1 + " " + s1);
將下列哪段代碼插入到第24行處可以使輸出結果爲"123abc 123abc"?
A. sb1.append("abc"); s1.append("abc");
B. sb1.append("abc"); s1.concat("abc");
C. sb1.concat("abc"); s1.append("abc");
D. sb1.concat("abc"); s1.concat("abc");
E. sb1.append("abc"); s1 = s1.concat("abc");
F. sb1.concat("abc"); s1 = s1.concat("abc");
G. sb1.append("abc"); s1 = s1 + s1.concat("abc");
H. sb1.concat("abc"); s1 = s1 + s1.concat("abc");
答案:E
知識點:
StringBuffer和String在處理字符串連接的時候的方式是不一樣的,StringBuffer在連接之後並不產生新的對象而String則是要產生一個新對象因此題目中s1在concat之後本身的值並沒有發生改變。這裏,我給出一個測試程序,不明白的朋友可以運行一下這個程序便一目瞭然。
public class AppendConcat {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("abc");
String s = new String("abc");
System.out.println(sb.append("def").hashCode());
System.out.println(sb.hashCode());
System.out.println(s.concat("def").hashCode());
System.out.println(s.hashCode());
}
}
第十二題:
11. class Alpha {
12. publicvoid foo() {System.out.print("Afoo "); }
13. }
14. public class Beta extendsAlpha {
15. publicvoid foo() {System.out.print("Bfoo "); }
16. publicstatic void main(String[] args) {
17. Alphaa = new Beta();
18. Betab = (Beta)a;
19. a.foo();
20. b.foo();
21. }
22. }
此程序的運行結果是?
A. Afoo Afoo
B. Afoo Bfoo
C. Bfoo Afoo
D. Bfoo Bfoo
E. 編譯失敗.
F. 運行時存在異常.
答案:D
知識點:
子類爲父類對象實例化
向下轉型,沒什麼繞彎的地方,不懂的去看相關內容
第十三題:
一下哪兩個選項能夠創建並初始化一個靜態的整型數組:
A. static final int[] a ={100200 };
B. static final int[] a;
static { a=new int[2]; a[0]=100;a[1]=200;}
C. static final int[] a=new int[2]{ 100200 };
D. static final int[] a;
static void init() { a = newint[3];a[0]=100; a[1]=200; }
答案:A B
知識點:
這道題主要考的是靜態成員變量的初始化方式,一共兩種,一種是在聲明的時候直接進行初始化,另外一種方式則是通過靜態代碼塊進行初始化。
答案C中將new int[2]改爲new int[]也是正確的。
第十四題:
10. interface Foo { intbar(); }
11. public class Sprite {
12. public int fubar( Foofoo ) { returnfoo.bar(); }
13. public void testFoo() {
14. fubar(
15. // insert code here
16. );
17. }
18. }
在第15行處插入哪段代碼可以使得Sprite類完整?
A. Foo { public int bar(){return 1; }
B. new Foo { public intbar() {return 1; }
C. new Foo() { public intbar() {return 1; }
D. new class Foo { publicintbar() { return 1; }
答案:C
知識點:
實例化接口時必須實現所有的抽象函數
第十五題
1. class Alligator {
2. public staticvoid main(String[] args) {
3. int []x[] = {{12} {345} {6789}};
4. int [][]y = x;
5. System.out.println(y[2][1]);
6. }
7. }
以上程序的運行結果是?
A. 2
B. 3
C. 4
D. 6
E. 7
F. 編譯失敗
答案:E
二維數組,自己數一下就知道了
--------------------------------------------------------分割線-----------------------------------------------
本期問題:
第十六題:
當前目錄是一個空目錄,假設當前用戶擁有對此目錄讀和寫的權限,並執行如下代碼
11. import java.io.*;
12. public class DOS {
13. public static void main(String[] args){
14. File dir = new File("dir");
15. dir.mkdir();
16. File f1 = new File(dir "f1.txt");
17. try {
18. f1.createNewFile();
19. } catch (IOException e) { ; }
20. File newDir = new File("newDir");
21. dir.renameTo(newDir);
22. }
23. }
下列哪種情況是正確的?
A. 編譯失敗
B. 當前目錄下創建了一個名爲“dir”的空目錄
C. 當前目錄下創建了一個名爲“newdir”的空目錄
D. 當前目錄下創建了一個名爲“dir”的目錄,在這個目錄中包含了一個名爲f1.txt的文件
E. 當前目錄下創建了一個名爲“newdir”的目錄,在這個目錄中包含了一個名爲f1.txt的文件
第十七題:
11. class Converter {
12. public static void main(String[] args){
13. Integer i = args[0];
14. int j = 12;
15. System.out.println("Itis " + (j==i) + " that j==i.");
16. }
17. }
當程序員試圖通過執行命令“java Converter 12”來編譯並運行該程序時會出現哪種結果?
A. It is true that j==i.
B. It is false that j==i.
C. 拋出一個異常
D. 編譯失敗,因爲第13行存在錯誤
第十八題:
11. String test ="Test A. Test B. Test C.";
12. // insert code here
13. String[] result =test.split(regex);
在第12行插入下列哪個正則表達式可以將test分割爲"Test A" "Test B" and "Test
C"?
A. String regex ="";
B. String regex = "";
C. String regex =".*";
D. String regex ="\\s";
E. String regex ="\\.\\s*";
F. String regex ="\\w[ \.] +";
第十九題:
5. import java.util.Date;
6.import java.text.DateFormat;
21.DateFormat df;
22. Date date = newDate();
23. // 此處插入代碼
24. String s =df.format(date);
在第23行填入代碼使得上述代碼可以被編譯通過
A. df = new DateFormat();
B. df = Date.getFormat();
C. df = date.getFormat();
D. df =DateFormat.getFormat();
E. df =DateFormat.getInstance();
第二十題
有一個名爲Repetition的類:
1. package utils;
2
3. public class Repetition{
4. public static String twice(String s) { return s + s; }
5. }
又有另一個名爲Demo的類
1. // 此處插入代碼
2
3. public class Demo {
4. public static void main(String[] args) {
5. System.out.println(twice("pizza"));
6. }
7. }
在類Demo的第一行插入那條代碼可以打印出pizzapizza?
A. import utils.*;
B. static import utils.*;
C. importutils.Repetition.*;
D. static importutils.Repetition.*;
E. importutils.Repetition.twice();
F. import staticutils.Repetition.twice;
G. static importutils.Repetition.twice;
第二十題:
3. interface Animal { voidmakeNoise(); }
4. class Horse implementsAnimal {
5. Long weight = 1200L;
6. public void makeNoise() {System.out.println("whinny"); }
7. }
8. public class Icelandicextends Horse {
9. public void makeNoise() {System.out.println("vinny"); }
10. public static void main(String[] args){
11. Icelandic i1 = newIcelandic();
12. Icelandic i2 = newIcelandic();
13. Icelandic i3 = new Icelandic();
14. i3 = i1; i1 = i2; i2 = null;i3 = i1;
15. }
16. }
當程序運行到第15行是,一共產生多少個可以被垃圾回收器回收的對象
A. 0
B. 1
C. 2
D. 3
E. 4
F. 6