SCJP認證套題解析之三 (轉)

gugu99發表於2008-04-23
SCJP認證套題解析之三 (轉)[@more@]

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):namespace prefix = o ns = "urn:schemas--com::office" />

題目:下面的哪些宣告是合法的。

此題的考點是數字的表示法和基本資料型別的型別自動轉換,沒有小數點的數字被認為是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,即在執行時決定變數的實際型別,而在編譯時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 .

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 ions

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 中的一些基本類,題目中的InputStream是所有輸入流的父類,所有要很熟悉,參看的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. RanAccesile

(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 srunning 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的構造方法,而該類沒有這樣的構造方法。


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10748419/viewspace-1002868/,如需轉載,請註明出處,否則將追究法律責任。

相關文章