JAVA認證歷年真題:SCJP認證套題解析[3]

41、which of the following statements are legal?
a. long l = 4990;
b. int  i = 4l;
c. float f = 1.1;
d. double d = 34.4;
e. double t = 0.9f.
(ade)
題目:下面的哪些聲明是合法的。
此題的考點是數字的表示法和基本數據類型的類型自動轉換,沒有小數點的數字被認為是int型數,帶有小數點的數被認為是double型的數,其它的使用在數字後面加一個字母表示數據類型,加l或者l是long型,加d或者d是double,加f或者f是float,可以將低精度的數字賦值給高精度的變數,反之則需要進行強制類型轉換,例如將int,short,byte賦值給long型時不需要顯式的類型轉換,反之,將long型數賦值給byte,short,int型時需要強制轉換(int a=(int)123l;)。

42、
public class parent {
int change() {…}
}
class child extends parent {

}
which methods can be added into class child?
a. public int change(){}
b. int chang(int i){}
c. private int change(){}
d. abstract int chang(){}
(ab)
題目:哪些方法可被加入類child。
這個題目的問題在第35題中有詳盡的敘述。需要注意的是答案d的內容,子類可以重寫父類的方法並將之聲明為抽象方法,但是這引發的問題是類必須聲明為抽象類,否則編譯不能通過,而且抽象方法不能有方法體,也就是方法聲明後面不能帶上那兩個大括弧({}),這些d都不能滿足。

43、
class parent {
string one, two;
public parent(string a, string b){
  one = a;
  two = b;
}
public void print(){ system.out.println(one); }
}
public class child extends parent {
  public child(string a, string b){
  super(a,b);
  }
  public void print(){
system.out.println(one + " to " + two);
  }
  public static void main(string arg[]){
parent p = new parent("south", "north");
parent t = new child("east", "west");
p.print();
t.print();
  }
}
which of the following is correct?
a. cause error during compilation.
b. south
east
c. south to north
east to west
d. south to north
east
e. south
east to west
(e)
題目:下面的哪些正確。
a.  在編譯時出錯。
這個題目涉及繼承時的多態性問題,在前面的問題中已經有講述,要注意的是語句t.print();在運行時t實際指向的是一個child對象,即java在運行時決定變數的實際類型,而在編譯時t是一個parent對象,因此,如果子類child中有父類中沒有的方法,例如printall(),那么不能使用t.printall()。參見12題的敘述。

44、a button is positioned in a frame. only height of the button is affected by the frame while the width is not. which layout manager should be used?
a. flowlayout
b. cardlayout
c. north and south of borderlayout
d. east and west of borderlayout
e. gridlayout
(d)
題目:一個按鈕放在一個框架中,在框架改變時只影響按鈕的高度而寬度不受影響,應該使用哪個布局管理器?
這個還是布局管理器的問題,流布局管理器(flowlayout)將根據框架的大小隨時調整它裡面的組件的大小,包括高度和寬度,這個管理器不會約束組件的大小,而是允許他們獲得自己的最佳大小,一行滿了以後將在下一行放置組件;卡片管理器(cardlayout)一次顯式一個加入的組件(根據加入時的關鍵字);格線管理器(gridlayout)將容器劃分為固定的格線,容器大小的改變將影響所有組件的大小,每個組件的大小都會同等地變化;邊界管理器(borderlayout)將容器劃分為五個區域,分為東南西北和中間,東西區域的寬度為該區域裡面組件的最佳寬度,高度為容器的高度減去南北區域的高度,這是一個可能變化的值,而南北區域的寬度為容器的整個寬度,高度為組件的最佳高度,中間區域的高度為容器的高度減去南北區域的高度,寬度為容器的寬度減去東西區域的寬度。

45、given the following code:
  1) class parent {
  2) private string name;
  3) public parent(){}
  4) }
  5) public class child extends parent {
  6) private string department;
  7) public child() {}
  8) public string getvalue(){ return name; }
  9) public static void main(string arg[]) {
  10) parent p = new parent();
  11)  }
  12)  }
which line will cause error?
a. line 3
b. line 6
c. line 7
d. line 8
e. line 10
(d)
題目:給出下面的代碼:

哪些行將導致錯誤。
第8行的getvalue()試圖訪問父類的私有變數,錯誤,參看前面有關變數類型及其作用域的敘述。

46、the variable "result" is boolean. which expressions are legal?
a. result = true;
b. if ( result ) { // do something... }
c. if ( result!= 0 ) { // so something... }
d. result = 1
(ab)
題目:變數"result"是一個boolean型的值,下面的哪些表達式是合法的。
java的boolean不同於c或者c++中的布爾值,在java中boolean值就是boolean值,不能將其它類型的值當作boolean處理。

47、class teacher and student are subclass of class person.
  person p;
  teacher t;
  student s;
  p, t and s are all non-null.
  if(t instanceof person) {  s = (student)t; }
what is the result of this sentence?
a. it will construct a student object.
b. the expression is legal.
c. it is illegal at compilation.
d. it is legal at compilation but possible illegal at runtime.
(c)
題目:類teacher和student都是類person的子類

p,t和s都是非空值

這個語句導致的結果是什麼
a.  將構造一個student對象。
b.  表達式合法。
c.  編譯時非法。
d.  編譯時合法而在運行時可能非法。
instanceof操作符的作用是判斷一個變數是否是右運算元指出的類的一個對象,由於java語言的多態性使得可以用一個子類的實例賦值給一個父類的變數,而在一些情況下需要判斷變數到底是一個什麼類型的對象,這時就可以使用instanceof了。當左運算元是右運算元指出的類的實例或者是子類的實例時都返回真,如果是將一個子類的實例賦值給一個父類的變數,用instanceof判斷該變數是否是子類的一個實例時也將返回真。此題中的if語句的判斷沒有問題,而且將返回真,但是後面的類型轉換是非法的,因為t是一個teacher對象,它不能被強制轉換為一個student對象,即使這兩個類有共同的父類。如果是將t轉換為一個person對象則可以,而且不需要強制轉換。這個錯誤在編譯時就可以發現,因此編譯不能通過。

48、given the following class:
  public class sample{
long length;
public sample(long l){ length = l; }
public static void main(string arg[]){
  sample s1, s2, s3;
  s1 = new sample(21l);
  s2 = new sample(21l); 
  s3 = s2;
  long m = 21l;
}
  }
which expression returns true?
a. s1 == s2;
b. s2 == s3;
c. m == s1;
d. s1.equals(m).
(b)
題目:給出下面的類:

哪個表達式返回true。
前面已經敘述過==操作符和string的equals()方法的特點,另外==操作符兩邊的運算元必須是同一類型的(可以是父子類之間)才能編譯通過。

49、given the following expression about list.
  list l = new list(6,true);
which statements are ture?
a. the visible rows of the list is 6 unless otherwise constrained.
b. the maximum number of characters in a line  will be 6.
c. the list allows users to make multiple selections
d. the list can be selected only one item.
(ac)
題目:給出下面有關list的表達式:

哪些敘述是對的。
a.  在沒有其它的約束的條件下該列表將有6行可見。
b.  一行的最大字元數是6
c.  列表將允許用戶多選。
d.  列表只能有一項被選中。
list組件的該構造方法的第一個參數的意思是它的初始顯式行數,如果該值為0則顯示4行,第二個參數是指定該組件是否可以多選,如果值為true則是可以多選,如果不指定則預設是不能多選。

50、given the following code:
  class person {
string name,department;
public void printvalue(){
  system.out.println("name is "+name);
  system.out.println("department is "+department);
}
  }
  public class teacher extends person {
int salary;
public void printvalue(){
  // doing the same as in the parent method printvalue()
  // including print the value of name and department.
  system.out.println("salary is "+salary);
}
  }
which expression can be added at the "doing the same as..." part of the method printvalue()?
a. printvalue();
b. this.printvalue();
c. person.printvalue();
d. super.printvalue().
(d)
題目:給出下面的代碼:

下面的哪些表達式可以加入printvalue()方法的"doing the same as..."部分。
子類可以重寫父類的方法,在子類的對應方法或其它方法中要調用被重寫的方法需要在該方法前面加上”super.”進行調用,如果調用的是沒有被重寫的方法,則不需要加上super.進行調用,而直接寫方法就可以。這裡要指出的是java支持方法的遞歸調用,因此答案a和b在語法上是沒有錯誤的,但是不符合題目代碼中說明處的要求:即做和父類的方法中相同的事情??列印名字和部門,嚴格來說也可以選a和b。

51、which is not a method of the class inputstream?
a. int read(byte[])
b. void flush()
c. void close()
d. int available()
(b)
題目:下面哪個不是inputstream類中的方法
這個題目沒有什麼好說的,要求熟悉java api中的一些基本類,題目中的inputstream是所有輸入流的父類,所有要很熟悉,參看jdk的api文檔。方法void flush()是緩衝輸出流的基本方法,作用是強制將流緩衝區中的當前內容強制輸出。

52、which class is not subclass of filterinputstream?
a. datainputstream
b. bufferedinputstream
c. pushbackinputstream
d. fileinputstream
(d)
題目:
哪個不是filterinputstream的子類。
此題也是要求熟悉api基礎類。java基礎api中的filterinputstream 的已知子類有:bufferedinputstream, checkedinputstream, cipherinputstream, datainputstream, digestinputstream, inflaterinputstream, linenumberinputstream, progressmonitorinputstream, pushbackinputstream 。

53、which classes can be used as the argument of the constructor of the class fileinputstream?
a. inputstream
b. file
c. fileoutputstream
d. string
(bd)
題目:哪些類可以作為fileinputstream類的構造方法的參數。
此題同樣是要求熟悉基礎api,fileinputstream類的構造方法有三個,可接受的參數分別是:file、filedescriptor、string類的一個對象。

54、which classes can be used as the argument of the constructor of the class filterinputstream?
a. filteroutputstream
b. file
c. inputstream
d. randomaccessfile
(c)
題目:哪些類可以作為filterinputstream類的構造方法的參數。
filterinputstream的構造方法只有一個,其參數是一個inputstream對象。

55、given the following code fragment:
1) switch(m)
2) { case 0: system.out.println("case 0");
3) case 1: system.out.println("case 1"); break;
4) case 2:
5) default: system.out.println("default");
6) }
which value of m would cause "default" to be the output?
a. 0
b. 1
c. 2
d. 3
(cd)
題目:給出下面的代碼片斷:

m為哪些值將導致"default"輸出。
此題考察switch語句的用法,switch的判斷的條件必須是一個int型值,也可以是byte、short、char型的值,case中需要注意的是一個case後面一般要接一個break語句才能結束判斷,否則將繼續執行其它case而不進行任何判斷,如果沒有任何值符合case列出的判斷,則執行default的語句,default是可選的,可以沒有,如果沒有default而又沒有任何值匹配case中列出的值則switch不執行任何語句。

56、given the uncompleted method:
1)
2) { success = connect();
3) if (success==-1) {
4) throw new timedoutexception();
5) }
6)}
timedoutexception is not a runtimeexception.  which can complete the method of declaration when added at line 1?
a. public void method()
b. public void method() throws exception
c. public void method() throws timedoutexception
d. public void method() throw timedoutexception
e. public throw timedoutexception void method()
(bc)
題目:給出下面的不完整的方法:

timedoutexception 不是一個runtimeexception。下面的哪些聲明可以被加入第一行完成此方法的聲明。
如果程式在運行的過程中拋出異常,而這個異常又不是runtimeexception或者error,那么程式必須捕獲這個異常進行處理或者聲明拋棄(throws)該異常,捕獲異常可以使用try{}catch(){}語句,而拋棄異常在方法聲明是聲明,在方法的聲明後面加上throws xxxxexception,拋棄多個異常時在各異常間使用逗號(,)分隔,題目中的程式在運行時拋出的不是一個runtimeexception,所有必須捕獲或者拋棄,而程式又沒有捕獲,所有應該在方法聲明中聲明拋棄。由於exception是所有異常的父類,所有當然也可以代表runtimeexception了。

57、which of the following answer is correct to express the value 10 in hexadecimal number?
a. 0xa
b. 0x16
c. 0a
d. 016
(a)
題目:下面的哪些答案可以正確表示一個十六進制數字10。
十六進制數以0x開頭,以0開頭的是八進制數。十六進制表示中的a,b,c,d,e,f依次為10,11,12,13,14,15。

58、which statements about thread are true?
a. once a thread is created, it can star running immediately.
b. to use the start() method makes a thread runnable, but it does not necessarily start immediately.
c. when a thread stops running because of pre-emptive, it is placed at the front end of the runnable queue.
d. a thread may cease to be ready for a variety of reasons.
(bd)
題目:有關執行緒的哪些敘述是對的。
a.  一旦一個執行緒被創建,它就立即開始運行。
b.  使用start()方法可以使一個執行緒成為可運行的,但是它不一定立即開始運行。
c.  當一個執行緒因為搶先機制而停止運行,它被放在可運行佇列的前面。
d.  一個執行緒可能因為不同的原因停止(cease)並進入就緒狀態。
一個新創建的執行緒並不是自動的開始運行的,必須調用它的start()方法使之將執行緒放入可運行態(runnable state),這只是意味著該執行緒可為jvm的執行緒調度程式調度而不是意味著它可以立即運行。執行緒的調度是搶先式的,而不是分時間片式的。具有比當前運行執行緒高優先權的執行緒可以使當前執行緒停止運行而進入就緒狀態,不同優先權的執行緒間是搶先式的,而同級執行緒間是輪轉式的。一個執行緒停止運行可以是因為不同原因,可能是因為更高優先權執行緒的搶占,也可能是因為調用sleep()方法,而即使是因為搶先而停止也不一定就進入可運行佇列的前面,因為同級執行緒是輪換式的,它的運行可能就是因為輪換,而它因搶占而停止後只能在輪換佇列中排隊而不能排在前面。

59、the method resume() is responsible for resuming which thread's execution?
a. the thread which is stopped by calling method stop()
b. the thread which is stopped by calling method sleep()
c. the thread which is stopped by calling method wait()
d. the thread which is stopped by calling method suspend()
(d)
題目:方法resume()負責恢復哪些執行緒的執行。
a.  通過調用stop()方法而停止的執行緒。
b.  通過調用sleep()方法而停止運行的執行緒。
c.  通過調用wait()方法而停止運行的執行緒。
d.  通過調用suspend()方法而停止運行的執行緒。
thread的api文檔中的說明是該方法恢復被掛起的(suspended)執行緒。該方法首先調用該執行緒的無參的checkaccess() 方法,這可能在當前執行緒上拋出securityexception 異常,如果該執行緒是活著的(alive)但是被掛起(suspend),它被恢復並繼續它的執行進程。

60、given the following code:
1) public class test {
2)  int m, n;
3)  public test() {}
4)  public test(int a) { m=a; }
5)  public static void main(string arg[]) {
6) test t1,t2;
7) int j,k;
8) j=0; k=0;
9) t1=new test();
10) t2=new test(j,k);
11) }
12) }
which line would cause one error during compilation?
a. line 3
b. line 5
c. line 6
d. line 10
(d)
題目:給出下面的代碼:

在編譯時哪行將導致一個錯誤。
第10行的聲明調用一個帶兩個參數的test的構造方法,而該類沒有這樣的構造方法。