java scjp 试题_JAVA认证历年真题:SCJP考试真题和解析[1]

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?

a. garbage collection require additional programe code in cases where multiple threads are running.

b. the programmer can indicate that a reference through a local variable is no longer of interest.

c. the programmer has a mechanism that explicity and immediately frees the memory used by java objects.

d. the garbage collection mechanism can free the memory used by java object at explection time.

e. the garbage collection system never reclaims memory from objects while are still accessible to running user threads.

1。b、e

java的垃圾回收机制是通过一个后台系统级线程对内存分配情况进行跟踪实现的,对程序员来说是透明的,程序员没有任何方式使无用内存显示的、立即的被释放。而且它是在程序运行期间发生的。

答案b告诉我们程序员可以使一个本地变量失去任何意义,例如给本地变量赋值为“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) }

in the absence of compiler optimization, which is the earliest point the object a refered is definitely elibile to be garbage collection.

a. before line 3

b.before line 5

c. before line 6

d.before line 7

e. 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?

a. it is a reference to the object directly affected by the cause of the event.

b. it is an indication of the nature of the cause of the event.

c. it is an indication of the position of the mouse when it caused the event.

d. in the case of a mouse click, it is an indication of the text under the mouse at the time of the event.

e. it tells the state of certain keys on the keybord at the time of the event.

f. it is an indication of the time at which the event occurred.

3。b

请查阅java类库。getid方法的返回值是“event type”。在认证考试中,总会有类似的书本以外的知识,这只能靠多实践来增长知识了。

4. which statement about listener is true?

a. most component allow multiple listeners to be added.

b. if multiple listener be add to a single component, the event only affected one listener.

c. component don?t allow multiple listeners to be add.

d. 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:

public class example{

public static void main(string args[] ){

int l=0;

do{

system.out.println(“doing it for l is:”+l);

}while(--l>0)

system.out.println(“finish”);

}

}

which well be output:

a. doing it for l is 3

b. doing it for l is 1

c. doing it for l is 2

d. doing it for l is 0

e. doing it for l is ?c1

f. 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) }

which value of x would cause “test 2” to the output:

a. 1

b. 2

c. 3

d. default

6。b.c

在开关语句中,标号总是不被当做语句的一部分,标号的作用就是做为条件判断而已,一旦匹配成功,就执行其后的语句,一直遭遇break语句为止。(包括default语句在内)

7. give incompleted method:

1)

2) { if(unsafe()){//do something…}

3) else if(safe()){//do the other…}

4) }

the method unsafe() well throe an ioexception, which completes the method of declaration when added at line one?

a. public ioexception methodname()

b. public void methodname()

c. public void methodname() throw ioexception

d. public void methodname() throws ioexception

e. public void methodname() throws exception

7。d、f

ioexception异常类是exception的子类。根据多态性的定义,ioexception对象也可以被认为是exception类型。还要注意在方法声明中抛出异常应用关键字“throws”。

8. give the code fragment:

if(x>4){

system.out.println(“test 1”);}

else if (x>9){

system.out.println(“test 2”);}

else {

system.out.println(“test 3”);}

which range of value x would produce of output “test 2”?

a. x<4

b. x>4

c. x>9

d. none

8。d

只有两种情况:大于4时输出“test1”,小于等于4时输出“test3”。

9. give the following method:

public void example(){

try{

unsafe();

system.out.println(“test1”);

}catch(safeexception e){system.out.println(“test 2”);

}finally{system.out.println(“test 3”);}

system.out.println(“test 4”);

which will display if method unsafe () run normally?

a. test 1

b. test 2

c. test 3

d. 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?

a. public void start()

b. public void run()

c. public void int()

d. public static void main(string args[])

e. public void runnable()

10。b

线程的执行是从方法“run( )”开始的,该方法是由系统调用的。程序员手工调用方法start(),使线程变为可运行状态。

11.given the following class definition:

class a{

protected int i;

a(int i){

this.i=i;

}

}

which of the following would be a valid inner class for this class?

select all valid answers:

a. class b{

}

b. class b extends a{

}

c. class b extends a{

b(){system.out.println(“i=”+i);}

}

d. class b{

class a{}

}

e. 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?

a. synchronized

b. abstract

c. final

d. static

e. 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) }

which line would be cause the error?

a. 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?

a. string [] a

b. string a[]

c. char a[][]

d. string a[50]

f. object a[50]

14。a、b

注意,题中问的是如何正确声明一个一维数组,并非实例化或者初始化数组

15. give the following java source fragement:

//point x

public class interesting{

//do something

}

which statement is correctly java syntax at point x?

a. import java.awt.*;

b.package mypackage

c. static int pi=3.14

d. public class myclass{//do other thing…} e. class myclass{//do something…}

15。a、e

x处可以是一个输入,包的定义,类的定义。由于常量或变量的声明只能在类中或方法中,故不能选择c;由于在一个文件中只能有一个public类,故不能选择d。

16. give this class outline:

class example{

private int x;

//rest of class body…

}

assuming that x invoked by the code java example, which statement can made x be directly accessible in main() method of example.java?

a. change private int x to public int x

b. change private int x to static int x

c. change private int x to protected int x

d. 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?

a. vector

b. int

c. string

d. color

e. date

17。a、b、d

polygon的顶点信息存放在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?

a. protected

b. public

c. no modifer

d. private

18。c

此题考点是高级访问控制。请考生查阅高级访问控制说明表格。

19.which declares for native method in a java class corrected?

a. public native void method(){}

b. public native void method();

c. public native method();

d. public void method(){native;}

e. public void native method();

19。b

native关键字指明是对本地方法的调用,在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

定义常量的方法是在变量定义前加final关键字。

21. which is the main() method return of a application?

a. string

b. byte

c. char

d. void

21。d

main()方法没有返回值,所以必须用void修饰。main()方法的返回值不能任意修改。

22. which is corrected argument of main() method of application?

a. string args

b. string ar[]

c. char args[][]

d. stringbuffer arg[]

22。b

main()方法的参数是字符串数组,参数名可以任意定义。

23. “the employee object is a person, an employee has appointment store in a vector, a hire date and a number of dependent”

short 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:

public class example{

public example(){//do something}

protected example(int i){//do something}

protected void method(){//do something}

}

public class hello extends example{//member method and member variable}

which methods are corrected added to the class hello?

a. public void example(){}

b. public void method(){}

c. protected void method(){}

d. private void method(){}

24。a、b、c

考察的知识点是方法覆盖,其中要注意的是方法覆盖时,子类方法的访问权限不能小于父类方法的访问权限。另外,选项a并不是父类构造函数,它是子类中的新方法。

25. float s=new float(0.9f);

float t=new float(0.9f);

double u=new double(0.9);

which expression?s result is true?

a. s==t

b. s.equals(t)

c. s==u

d. t.equals(u)

25。a、b

考察“==”及方法“equals()”的用法。注意以下几点区别:

1) 引用类型比较引用;基本类型比较值。

2) equals()方法只能比较引用类型,“==”可比较引用及基本类型。

3) 当用equals()方法进行比较时,对类file、string、date及封装类(wrapper class)来说,是比较类型及内容。

4) 用“==”进行比较时,符号两边的数据类型必须一致(可相互转换的基本类型除外),否则编译出错。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值