JAVA認證歷年真題:SCJP考試真題和解析[1]

  例題1:

choose the three valid identifiers from those listed below.

a. idolikethelongnameclass

b. $byte

c. const

d. _ok

e. 3_case

解答:a, b, d

點評:java中的標示符必須是字母、美元符($)或下劃線(_)開頭。關鍵字與保留字不能作為標示符。選項c中的const是java的保留字,所以不能作標示符。選項e中的3_case以數字開頭,違反了java的規則。

例題2:

how can you force garbage collection of an object?

a. garbage collection cannot be forced

b. call system.gc().

c. call system.gc(), passing in a reference to the object to be garbage collected.

d. call runtime.gc().

e. set all references to the object to new values(null, for example).

解答:a

點評:在java中垃圾收集是不能被強迫立即執行的。調用system.gc()或runtime.gc()靜態方法不能保證垃圾收集器的立即執行,因為,也許存在著更高優先權的執行緒。所以選項b、d不正確。選項c的錯誤在於,system.gc()方法是不接受參數的。選項e中的方法可以使對象在下次垃圾收集器運行時被收集。
 例題3:
 consider the following class:

1. class test(int i) {

2. void test(int i) {

3. system.out.println(“i am an int.”);

4. }

5. void test(string s) {

6. system.out.println(“i am a string.”);

7. }

8.

9. public static void main(string args[]) {

10. test t=new test();

11. char ch=“y”;

12. t.test(ch);

13. }

14. }

which of the statements below is true?(choose one.)

a. line 5 will not compile, because void methods cannot be overridden.

b. line 12 will not compile, because there is no version of test() that rakes a char argument.

c. the code will compile but will throw an exception at line 12.

d. the code will compile and produce the following output: i am an int.

e. the code will compile and produce the following output: i am a string.

解答:d

點評:在第12行,16位長的char型變數ch在編譯時會自動轉化為一個32位長的int型,並在運行時傳給void test(int i)方法。

例題4:

which of the following lines of code will compile without error?

a.
int i=0;
if (i) {
system.out.println(“hi”);
}

b.
boolean b=true;
boolean b2=true;
if(b==b2) {
system.out.println(“so true”);
}

c.
int i=1;
int j=2;
if(i==1|| j==2)
system.out.println(“ok”);

d.
int i=1;
int j=2;
if (i==1 &| j==2)
system.out.println(“ok”);

解答:b, c

點評:選項a錯,因為if語句後需要一個boolean類型的表達式。邏輯操作有^、&、| 和 &&、||,但是“&|”是非法的,所以選項d不正確。

例題5:

which two demonstrate a "has a" relationship? (choose two)

a. public interface person { }

public class employee extends person{ }

b. public interface shape { }
public interface rectandle extends shape { }

c. public interface colorable { }
public class shape implements colorable
{ }

d. public class species{ }
public class animal{private species species;}

e. interface component{ }
class container implements component{
private component[] children;
}

解答:d, e

點評: 在java中代碼重用有兩種可能的方式,即組合(“has a”關係)和繼承(“is a”關係)。“has a”關係是通過定義類的屬性的方式實現的;而“is a”關係是通過類繼承實現的。本例中選項a、b、c體現了“is a”關係;選項d、e體現了“has a”關係。

例題6:

which two statements are true for the class java.util.treeset? (choose two)

a. the elements in the collection are ordered.

b. the collection is guaranteed to be immutable.

c. the elements in the collection are guaranteed to be unique.

d. the elements in the collection are accessed using a unique key.
e. the elements in the collection are guaranteed to be synchronized
解答:a, c

點評:treeset類實現了set接口。set的特點是其中的元素惟一,選項c正確。由於採用了樹形存儲方式,將元素有序地組織起來,所以選項a也正確。

例題7:

true or false: readers have methods that can read and return floats and doubles.

a. ture

b. false

解答:b

點評: reader/writer只處理unicode字元的輸入輸出。float和double可以通過stream進行i/o.

例題8:

what does the following paint() method draw?

1. public void paint(graphics g) {

2. g.drawstring(“any question”, 10, 0);

3. }

a. the string “any question?”, with its top-left corner at 10,0

b. a little squiggle coming down from the top of the component.

解答:b

點評:drawstring(string str, int x, int y)方法是使用當前的顏色和字元,將str的內容顯示出來,並且最左的字元的基線從(x,y)開始。在本題中,y=0,所以基線位於最頂端。我們只能看到下行字母的一部分,即字母‘y’、‘q’的下半部分。

例題9:

what happens when you try to compile and run the following application? choose all correct options.

1. public class z {

2. public static void main(string[] args) {

3. new z();

4. }

5.

6. z() {

7. z alias1 = this;

8. z alias2 = this;

9. synchronized(alias1) {

10. try {

11. alias2.wait();

12. system.out.println(“done waiting”);

13. }

14. catch (interruptedexception e) {

15. system.out.println(“interr
upted”);

16. }

17. catch (exception e) {
 
18. system.out.println(“other exception”);

19. }

20. finally {

21. system.out.println
(“finally”);

22. }

23. }

24. system.out.println(“all done”);

25. }

26. }

a. the application compiles but doesn't print anything.

b. the application compiles and print “done waiting”

c. the application compiles and print “finally”

d. the application compiles and print “all done”

e. the application compiles and print “interrupted”

解答:a

點評:在java中,每一個對象都有鎖。任何時候,該鎖都至多由一個執行緒控制。由於alias1與alias2指向同一對象z,在執行第11行前,執行緒擁有對象z的鎖。在執行完第11行以後,該執行緒釋放了對象z的鎖,進入等待池。但此後沒有執行緒調用對象z的notify()和notifyall()方法,所以該進程一直處於等待狀態,沒有輸出。

例題10:

which statement or statements are true about the code listed below? choose three.

1. public class mytextarea extends textarea {

2. public mytextarea(int nrows, int ncols) {

3. enableevents(awtevent.text_
event_mask);

4. }

5.

6. public void processtextevent
(textevent te) {

7. system.out.println(“processing a text event.”);

8. }

9. }

a. the source code must appear in a file called mytextarea.java

b. between lines 2 and 3, a call should be made to super(nrows, ncols) so that the new component will have the correct size.

c. at line 6, the return type of processtextevent() should be declared boolean, not void.

d. between lines 7 and 8, the following code should appear: return true.

e. between lines 7 and 8, the following code should appear: super.processtextevent(te).

解答:a, b, e

點評:由於類是public,所以檔案名稱必須與之對應,選項a正確。如果不在2、3行之間加上super(nrows,ncols)的話,則會調用無參數構建器textarea(), 使nrows、ncols信息丟失,故選項b正確。在java2中,所有的事件處理方法都不返回值,選項c、d錯誤。選項e正確,因為如果不加super.processtextevent(te),註冊的listener將不會被喚醒。

1.which statement about the garbage collection mechanism are true?
牐燼. garbage collection require additional programe code in cases where multiple 爐hreads are running.
牐燽. the programmer can indicate that a reference through a local variable is no longer of interest.

牐燾. the programmer has a mechanism that explicity and immediately frees the memory used by java objects.
牐燿. the garbage collection mechanism can free the memory used by java object at explection time.
牐爀. the garbage collection system never reclaims memory from objects while are still accessible to running user threads.

1。b、e
牐爅ava的垃圾回收機制是通過一個後台系統級執行緒對記憶體分配情況進行跟蹤實現的,對程式設計師來說是透明的,程式設計師沒有任何方式使無用記憶體顯示的、立即的被釋放。而且它是在程式運行期間發生的。
牐牬鳶竍告訴我們程式設計師可以使一個本地變數失去任何意義,例如給本地變數賦值為“null”;答案e告訴我們在程式運行期間不可能完全釋放記憶體。


牐2. give the following method:
牐1) public void method( ){
牐2) string a,b;
牐3) a=new string(“hello world”);
牐4) b=new string(“game over”);
牐5) system.out.println(a+b+”ok”);
牐6) a=null;
牐7) a=b;
牐8) system.out.println(a);
牐9) }
牐爄n the absence of compiler optimization, which is the earliest point the object 牐燼 refered is definitely elibile to be garbage collection.
牐燼. before line 3
牐燽.before line 5
牐燾. before line 6
牐燿.before line 7
牐爀. before line 9

2。d
牐牭6行將null賦值給a以後,a以前保存的引用所指向的記憶體空間就失去了作用,它可能被釋放。所以對象a可能最早被垃圾回收是在第7行以前,故選擇d選項。

牐3. in the class java.awt.awtevent,which is the parent class upon which jdk1.1 awt events are based there is a method called getid which phrase accurately describes the return value of this method?
牐燼. it is a reference to the object directly affected by the cause of the event.
牐燽. it is an indication of the nature of the cause of the event.
牐燾. it is an indication of the position of the mouse when it caused the event.
牐燿. in the case of a mouse click, it is an indication of the text under the mouse at the time of the event.
牐爀. it tells the state of certain keys on the keybord at the time of the event.
牐爁. it is an indication of the time at which the event occurred.

3。b
牐犌氬樵膉ava類庫。getid方法的返回值是“event type”。在認證考試中,總會有類似的書本以外的知識,這只能靠多實踐來增長知識了。

4. which statement about listener is true?
牐燼. most component allow multiple listeners to be added.
牐燽. if multiple listener be add to a single component, the event only affected one listener.
牐燾. component don?t allow multiple listeners to be add.
牐燿. the listener mechanism allows you to call an addxxxxlistener method as many times as is needed, specifying as many different listeners as your design require.

4。a、d
牐牽丶可以同時使用多個“addxxxxlistener”方法加入多個監聽器。並且當多個監聽器加入到同一控制項中時,事件可以回響多個監聽器,回響是沒有固定順序的。

牐5.give the following code:
牐爌ublic class example{
牐爌ublic static void main(string args[] ){
牐爄nt l=0;
牐燿o{
牐爏ystem.out.println(“doing it for l is:”+l);
牐爙while(--l>0)
牐爏ystem.out.println(“finish”);
牐爙
牐爙
牐爓hich well be output:
牐燼. doing it for l is 3
牐燽. doing it for l is 1
牐燾. doing it for l is 2
牐燿. doing it for l is 0
牐爀. doing it for l is ?c1
牐爁. finish

5。d、f
牐牨咎庵饕考察考生對流程控制的掌握情況。這是當型循環,條件為真執行,條件為假則退出。循環體至少執行一次,故會輸出d。循環體以外的語句總會被執行,故輸出f。
6. give the code fragment:
牐1) switch(x){
牐2) case 1:system.out.println(“test 1”);break;
牐3) case 2:
牐4) case 3:system.out.println(“test 2”);break;
牐5) default:system.out.println(“end”);
牐6) }
牐爓hich value of x would cause “test 2” to the output:
牐燼. 1
牐燽. 2
牐燾. 3
牐燿. default

6。b.c
牐犜誑關語句中,標號總是不被當做語句的一部分,標號的作用就是做為條件判斷而已,一旦匹配成功,就執行其後的語句,一直遭遇break語句為止。(包括default語句在內)
7. give incompleted method:
牐1)
牐2) { if(unsafe()){//do something…}
牐3) else if(safe()){//do the other…}
牐4) }
牐爐he method unsafe() well throe an ioexception, which completes the method of declaration when added at line one?
牐燼. public ioexception methodname()
牐燽. public void methodname()
牐燾. public void methodname() throw ioexception
牐燿. public void methodname() throws ioexception
牐爀. public void methodname() throws exception
7。d、f
牐爄oexception異常類是exception的子類。根據多態性的定義,ioexception對象也可以被認為是exception類型。還要注意在方法聲明中拋出異常套用關鍵字“throws”。
8. give the code fragment:
牐爄f(x>4){
牐爏ystem.out.println(“test 1”);}
牐爀lse if (x>9){
牐爏ystem.out.println(“test 2”);}
牐爀lse {
牐爏ystem.out.println(“test 3”);}
牐爓hich range of value x would produce of output “test 2”?
牐燼. x<4
牐燽. x>4
牐燾. x>9
牐燿. none

8。d
牐犞揮辛街智榭觶捍笥4時輸出“test1”,小於等於4時輸出“test3”。
9. give the following method:
牐爌ublic void example(){
牐爐ry{
牐爑nsafe();
牐爏ystem.out.println(“test1”);
牐爙catch(safeexception e){system.out.println(“test 2”);
牐爙finally{system.out.println(“test 3”);}
牐爏ystem.out.println(“test 4”);
牐爓hich will display if method unsafe () run normally?
牐燼. test 1
牐燽. test 2
牐燾. test 3
牐燿. test 4

9。a、c、d
牐犜謖常情況下,列印test1、test3、test4;在產生可捕獲異常時列印test2、test3、test4;在產生不可捕獲異常時,列印test3,然後終止程式。注意finally後面的語句總是被執行。

10. which method you define as the starting point of new thread in a class from which new the thread can be excution?
牐燼. public void start()
牐燽. public void run()
牐燾. public void int()
牐燿. public static void main(string args[])
牐爀. public void runnable()

10。b
牐犗叱痰鬧蔥惺譴臃椒“run( )”開始的,該方法是由系統調用的。程式設計師手工調用方法start(),使執行緒變為可運行狀態。

11.given the following class definition:
牐燾lass a{
牐爌rotected int i;
牐燼(int i){
牐爐his.i=i;
牐爙
牐爙
牐爓hich of the following would be a valid inner class for this class?
牐爏elect all valid answers:
牐燼. class b{
牐爙
牐燽. class b extends a{
牐爙
牐燾. class b extends a{
牐燽(){system.out.println(“i=”+i);}
牐爙
牐燿. class b{
牐燾lass a{}
牐爙
牐爀. class a{}

11。a
牐牬頌飪疾檳誆坷嗉骯丶字“super”的用法。內部類不能與外部類同名。另外,當b繼承a時,a中的構造函式是帶參數的,b中預設構造函式的函式體為空;而java編譯器會為空構造函式體自動添加語句“super();”調用父類構造函式,更進一步是調用父類的參數為空的構造函式。而父類中沒有參數為空的構造函式。
12. which modifier should be applied to a method for the lock of object this to be obtained prior to excution any of the method body?
牐燼. synchronized
牐燽. abstract
牐燾. final
牐燿. static
牐爀. public

12。a
牐牬斯丶字可以在兩個執行緒同時試圖訪問某一數據時避免數據毀損。
13. the following code is entire contents of a file called example.java,causes precisely one error during compilation:
牐1) class subclass extends baseclass{
牐2) }
牐3) class baseclass(){
牐4) string str;
牐5) public baseclass(){
牐6) system.out.println(“ok”);}
牐7) public baseclass(string s){
牐8) str=s;}}
牐9) public class example{
牐10) public void method(){
牐11) subclass s=new subclass(“hello”);
牐12) baseclass b=new baseclass(“world”);
牐13) }
牐14) }

牐爓hich line would be cause the error?
牐燼. 9 b. 10 c. 11 d.12

13。c
牐牭幣桓隼嘀形聰允蕉ㄒ騫乖旌數時,預設的構造函式是以類名為函式名,參數為空,函式體為空。雖然父類中的某一構造函式有字元串參數s,但是子類繼承父類時並不繼承構造函式,所以它只能使用預設構造函式。故在第11行出錯。

14. which statement is correctly declare a variable a which is suitable for refering to an array of 50 string empty object?
牐燼. string [] a
牐燽. string a[]
牐燾. char a[][]
牐燿. string a[50]
牐爁. object a[50]

14。a、b
牐犠⒁猓題中問的是如何正確聲明一個一維數組,並非實例化或者初始化數組
15. give the following java source fragement:
牐//point x
牐爌ublic class interesting{
牐//do something
牐爙
牐爓hich statement is correctly java syntax at point x?
牐燼. import java.awt.*;

牐燽.package mypackage
牐燾. static int pi=3.14
牐燿. public class myclass{//do other thing…} e. class myclass{//do something…}

15。a、e
牐爔處可以是一個輸入,包的定義,類的定義。由於常量或變數的聲明只能在類中或方法中,故不能選擇c;由於在一個檔案中只能有一個public類,故不能選擇d。
16. give this class outline:
牐燾lass example{
牐爌rivate int x;
牐//rest of class body…
牐爙
牐燼ssuming that x invoked by the code java example, which statement can made x be directly accessible in main() method of example.java?
牐燼. change private int x to public int x
牐燽. change private int x to static int x
牐燾. change private int x to protected int x
牐燿. change private int x to final int x

16。b
牐牼蔡方法除了自己的參數外只能直接訪問靜態成員。訪問非靜態成員,必須先實例化本類的一個實例,再用實例名點取。
17. the piece of preliminary analsis work describes a class that will be used frequently in many unrelated parts of a project
“the polygon object is a drawable, a polygon has vertex information stored in a vector, a color, length and width.”
which data type would be used?
牐燼. vector
牐燽. int
牐燾. string
牐燿. color
牐爀. date

17。a、b、d
牐爌olygon的頂點信息存放在vector類型的對象內部,color定義為color,length和width定義為int。
牐犠⒁猓這是考試中常見的題型。
18. a class design requires that a member variable should be accessible only by same package, which modifer word should be used?
牐燼. protected
牐燽. public
牐燾. no modifer
牐燿. private
18。c
牐牬頌飪嫉閌歉嘸斗夢士刂啤G肟忌查閱高級訪問控制說明表格。

19.which declares for native method in a java class corrected?
牐燼. public native void method(){}
牐燽. public native void method();
牐燾. public native method();
牐燿. public void method(){native;}
牐爀. public void native method();

19。b
牐爊ative關鍵字指明是對本地方法的調用,在java中是只能訪問但不能寫的方法,它的位置在訪問許可權修飾語的後面及返回值的前面。
20. which modifer should be applied to a declaration of a class member variable for the value of variable to remain constant after the creation of the object?

20。final
牐牰ㄒ宄A康姆椒ㄊ竊詒淞慷ㄒ邇凹觙inal關鍵字。
21. which is the main() method return of a application?
牐燼. string
牐燽. byte
牐燾. char
牐燿. void


21。d
牐爉ain()方法沒有返回值,所以必須用void修飾。main()方法的返回值不能任意修改。
22. which is corrected argument of main() method of application?
牐燼. string args
牐燽. string ar[]
牐燾. char args[][]
牐燿. stringbuffer arg[]

22。b
牐爉ain()方法的參數是字元串數組,參數名可以任意定義。
23. “the employee object is a person, an employee has appointment store in a 牐牐爒ector, a hire date and a number of dependent”

牐爏hort answer: use shortest statement declare a class of employee.

23。public class employee extends person
牐犝庖彩欽媸悼際災諧<的一種題型。要注意題目敘述中“is a”表示 “extends”的含義。

24. give the following class defination inseparate source files:
牐爌ublic class example{
牐爌ublic example(){//do something}
牐爌rotected example(int i){//do something}
牐爌rotected void method(){//do something}
牐爙
牐爌ublic class hello extends example{//member method and member variable}
牐爓hich methods are corrected added to the class hello?
牐燼. public void example(){}
牐燽. public void method(){}
牐燾. protected void method(){}
牐燿. private void method(){}

24。a、b、c
牐牽疾斕鬧識點是方法覆蓋,其中要注意的是方法覆蓋時,子類方法的訪問許可權不能小於父類方法的訪問許可權。另外,選項a並不是父類構造函式,它是子類中的新方法。
25. float s=new float(0.9f);
牐爁loat t=new float(0.9f);
牐燿ouble u=new double(0.9);
牐爓hich expression?s result is true?
牐燼. s==t
牐燽. s.equals(t)
牐燾. s==u
牐燿. t.equals(u)
25。a、b
牐牽疾“==”及方法“equals()”的用法。注意以下幾點區別:
牐1) 引用類型比較引用;基本類型比較值。
牐2) equals()方法只能比較引用類型,“==”可比較引用及基本類型。
牐3) 當用equals()方法進行比較時,對類file、string、date及封裝類(wrapper class)來說,是比較類型及內容。
牐4) 用“==”進行比較時,符號兩邊的數據類型必須一致(可相互轉換的基本類型除外),否則編譯出錯。