SUN试题集锦有答案.docx
- 文档编号:3278121
- 上传时间:2022-11-21
- 格式:DOCX
- 页数:33
- 大小:29.29KB
SUN试题集锦有答案.docx
《SUN试题集锦有答案.docx》由会员分享,可在线阅读,更多相关《SUN试题集锦有答案.docx(33页珍藏版)》请在冰豆网上搜索。
SUN试题集锦有答案
索创培训(SOTRAINING)
Java试题集锦
王刚wg.1983@
无论你是个新手,还是程序设计方面的专家,你都会惊异于Sun公司Java的无穷魅力。
Java带给你的并不仅仅是面向对象、开放、平台无关、易用、安全和“Writeonce,runanywhere”等软件开发方面的优势,更重要的一点是,它提供了一种新颖的表达思想的方式,一种全新的思维模式。
随着待解决问题的规模不断膨胀,Java彻底的面向对象思想的灵活性就会凸现出来。
毋庸置疑,Java是你开发大型软件时最得心应手的利器或是你转行IT的入门首选。
试题分析
例题1:
Choosethethreevalididentifiersfromthoselistedbelow.
A.IDoLikeTheLongNameClass
B.$byte
C.const
D._ok
E.3_case
解答:
A、B、D
点评:
Java中的标示符必须是字母、美元符($)或下划线(_)开头。
关键字与保留字不能作为标示符。
选项C中的const是Java的保留字,所以不能作标示符。
选项E中的3_case以数字开头,违反了Java的规则。
例题2:
Howcanyouforcegarbagecollectionofanobject?
A.Garbagecollectioncannotbeforced
B.CallSystem.gc().
C.CallSystem.gc(),passinginareferencetotheobjecttobegarbagecollected.
D.CallRuntime.gc().
E.Setallreferencestotheobjecttonewvalues(null,forexample).
解答:
A
点评:
在Java中垃圾收集是不能被强迫立即执行的。
调用System.gc()或Runtime.gc()静态方法不能保证垃圾收集器的立即执行,因为,也许存在着更高优先级的线程。
所以选项B、D不正确。
选项C的错误在于,System.gc()方法是不接受参数的。
选项E中的方法可以使对象在下次垃圾收集器运行时被收集。
例题3:
Considerthefollowingclass:
1.publicclassTest{
2.voidtest(inti){
3.System.out.println(“Iamanint.”);
4.}
5.voidtest(Strings){
6.System.out.println(“Iamastring.”);
7.}
8.
9.publicstaticvoidmain(Stringargs[]){
10.Testt=newTest();
11.charch=’a’;
12.t.test(ch);
13.}
14.}
Whichofthestatementsbelowistrue?
(Chooseone.)
A.Line5willnotcompile,becausevoidmethodscannotbeoverridden.
B.Line12willnotcompile,becausethereisnoversionoftest()thatrakesacharargument.
C.Thecodewillcompilebutwillthrowanexceptionatline12.
D.Thecodewillcompileandproducethefollowingoutput:
Iamanint.
E.Thecodewillcompileandproducethefollowingoutput:
IamaString.
解答:
D
点评:
在第12行,16位长的char型变量ch在编译时会自动转化为一个32位长的int型,并在运行时传给voidtest(inti)方法。
例题4:
Whichofthefollowinglinesofcodewillcompilewithouterror?
A.
inti=0;
if(i){
System.out.println(“Hi”);
}
B.
booleanb=true;
booleanb2=true;
if(b==b2){
System.out.println(“Sotrue”);
}
C.
inti=1;
intj=2;
if(i==1||j==2)
System.out.println(“OK”);
D.
inti=1;
intj=2;
if(i==1&|j==2)
System.out.println(“OK”);
解答:
B,C
点评:
选项A错,因为if语句后需要一个boolean类型的表达式。
逻辑操作有^、&、|和&&、||,但是“&|”是非法的,所以选项D不正确。
例题5:
1.inta1=2;
2.inta2=2;
3.intb=(++a1)*2–(a2--)*3;
4.intc=(a1--)*3+(++a2)*2;
Whatistheresult?
A.b=0andc=13
B.b=0andc=12
C.b=3andc=12
D.b=3andc=13
解答:
A
点评:
一元运算符++,--只适用于变量,且变量位于运算符的哪一侧有不同的效果。
当运算符位于变量的左侧时,先++(或--)后运算,反之,先运算后++(或--)。
例题6:
1.publicclassTest{
2.publicstaticvoidmain(Stringargs[]){
3.classFoo{
4.publicinti=3;
5.}
6.Objecto=(Object)newFoo();
7.Foofoo=(Foo)o;
8.System.out.println(“i=“+foo.i);
9.}
10.}
Whatistheresult?
A.i=3
B.Compilationfails.
C.AClassCastExceptionisthrownatline6.
D.AClassCastExceptionisthrownatline7.
解答:
A
点评:
此题主要考查父类对象和子类对象的引用关系。
希望大家明确父类声明只能访问到子类中继承下来的属于父类的属性,而不能访问子类中特有的属性。
同时我们可以通过“打回原形”的操作,把父类引用强制转换为子类对象,从而可以访问到子类中哪些所有的属性。
例题7:
11.inti=1,j=10;
12.do{
13.if(i++>--j){
14.continue;
15.}
16.}while(i<5);
17.System.out.println(“i=“+i+“andj=“+j);
Whatistheresult?
A.i=6andj=5
B.i=5andj=5
C.i=6andj=5
D.i=5andj=6
E.i=6andj=6
解答:
D
点评:
do-while结果首先执行一下表达式,然后在去判断条件。
这样的题目最好动手画一画,结果就自然明了了。
例题8:
1.classA{
2.A(){}
3.}
4.
5.classBextendsA{
6.}
Whichtwostatementsaretrue?
(Choosetwo)
A.ClassB’sconstructorispublic.
B.ClassB’sconstructorhasnoarguments.
C.ClassB’sconstructorincludesacalltothis().
D.ClassB’sconstructorincludesacalltosuper().
解答:
B、D
点评:
由于父类是无参的构造方法,所以子类可以是无参或带有任何参数的构造方法,它们都会默认的调用super()方法。
例题9:
1.publicinterfaceFoo{
2.intk=4;
3.}
Whichthreeareequivalenttoline2?
(Choosethree)
A.finalintk=4;
B.publicintk=4;
C.staticintk=4;
D.abstractintk=4;
E.volatileintk=4;
F.protectedintk=4;
解答:
A、B、C
点评:
接口中的属性和成员方法修饰符只能是缺省的或者public,并且它们都是静态常量,所以我们一定要对其进行初始化赋值操作。
例题10:
1.packagetest1;
2.publicclassTest1{
3.staticintx=42;
4.}
1.packagetest2;
2.publicclassTest2extendstest1.Test1{
3.publicstaticvoidmain(String[]args){
4.System.out.println(“x=“+x);
5.}
6.}
Whatistheresult?
A.x=0
B.x=42
C.Compilationfailsbecauseofanerrorinline2ofclassTest2.
D.Compilationfailsbecauseofanerrorinline3ofclassTest1.
E.Compilationfailsbecauseofanerrorinline4ofclassTest2.
解答:
C
点评:
要在其它的包中访问public成员,我们只能使用import语句导入我们所需要的包。
例题11:
1.classA{
2.protectedintmethod1(inta,intb){return0;}
3.}
WhichtwoarevalidinaclassthatextendsclassA?
(Choosetwo)
A.publicintmethod1(inta,intb){return0;}
B.privateintmethod1(inta,intb){return0;}
C.privateintmethod1(inta,longb){return0;}
D.publicshortmethod1(inta,intb){return0;}
E.staticprotectedintmethod1(inta,intb){return0;}
解答:
A、C
点评:
对于重载,我们只要求方法名相同,而参数列表不同;而对于覆盖我们要求方法名,参数列表以及返回类型都相同,并且不能缩小其访问控制修饰符。
例题12:
1.publicclassTest{
2.publicintaMethod(){
3.staticinti=0;
4.i++;
5.returni;
6.}
7.publicstaticvoidmain(Stringargs[]){
8.Testtest=newTest();
9.test.aMethod();
10.intj=test.aMethod();
11.System.out.println(j);
12.}
13.}
Whatistheresult?
A.0
B.1
C.2
D.Compilationfails.
解答:
D
点评:
static属于类级别的静态成员,存放在属于类的一片共享内存区域,不能隶属于某个方法。
例题13:
1.classSuper{
2.publicfloatgetNum(){return3.0f;}
3.}
4.
5.publicclassSubextendsSuper{
6.
7.}
Whichmethod,placedatline6,causescompilationtofail?
A.publicvoidgetNum(){}
B.publicvoidgetNum(doubled){}
C.publicfloatgetNum(){return4.0f;}
D.publicdoublegetNum(floatd){return4.0d;}
解答:
A
点评:
对于同名的方法,在继承上只有重载和覆盖,选项B和D都属于重载,选项C属于覆盖,而选项A则会发生编译错误。
例题14:
1.packagefoo;
2.
3.importjava.util.Vector;
4.
5.privateclassMyVectorextendsVector{
6.inti=1;
7.publicMyVector(){
8.i=2;
9.}
10.}
11.
12.publicclassMyNewVectorextendsMyVector{
13.publicMyNewVector(){
14.i=4;
15.}
16.publicstaticvoidmain(Stringargs[]){
17.MyVectorv=newMyNewVector();
18.}
19.}
Whatistheresult?
A.Compilationsucceeds.
B.Compilationfailsbecauseofanerroratline5.
C.Compilationfailsbecauseofanerroratline6.
D.Compilationfailsbecauseofanerroratline14.
E.Compilationfailsbecauseofanerroratline17.
解答:
B
点评:
类前的修饰符可以是:
缺省、public、abstract和final。
不能被private和protected所修饰。
例题15:
1.classTestSuper{
2.TestSuper(inti){}
3.}
4.classTestSubextendsTestSuper{}
5.classTestAll{
6.publicstaticvoidmain(String[]args){
7.newTestSub();
8.}
9.}
Whichistrue?
A.Compilationfails.
B.Thecoderunswithoutexception.
C.Anexceptionisthrownatline7.
D.Anexceptionisthrownatline2.
解答:
A
点评:
由于在行4会默认的调用super()方法,而父类中没有使用无参的构造方法,所以会导致编译错误。
例题16:
10.inti=0;
11.for(;i<4;i+=2){
12.System.out.print(i+“”);
13.}
14.System.out.println(i);
Whatistheresult?
A.024
B.0245
C.01234
D.Compilationfails.
E.Anexceptionisthrownatruntime.
解答:
A
点评:
主要考查for语句的使用。
例题17:
1.publicclassSwitchTest{
2.publicstaticvoidmain(String[]args){
3.System.out.println(“value=“+switchIt(4));
4.}
5.publicstaticintswitchIt(intx){
6.intj=1;
7.switch(x){
8.case1:
j++;
9.case2:
j++;
10.case3:
j++;
11.case4:
j++;
12.case5:
j++;
13.default:
j++;
14.}
15.returnj+x;
16.}
17.}
Whatistheresult?
A.value=3
B.value=4
C.value=5
D.value=6
E.value=7
F.value=8
解答:
F
点评:
在分支语句switch中,如果没有break语句跳出循环,则其会继续朝下执行,直至default语句退出。
例题18:
1.classBase{
2.Base(){System.out.print(“Base”);}
3.}
4.publicclassAlphaextendsBase{
5.publicstaticvoidmain(String[]args){
6.newAlpha();
7.newBase();
8.}
9.}
Whatistheresult?
A.Base
B.BaseBase
C.Compilationfails.
D.Thecoderunswithnooutput.
E.Anexceptionisthrownatruntime.
解答:
B
点评:
主要考查继承的使用,子类会继承父类除private修饰的所有成员变量及方法。
例题19:
Whichtwoarevaliddeclarationswithinaninterfacedefinition?
(Choosetwo)
A.voidmethoda();
B.publicdoublemethoda();
C.publicfinaldoublemethoda();
D.staticvoidmethoda(doubled1);
E.protectedvoidmethoda(doubled1);
解答:
A、B
点评:
定义接口的主要目的就是要让其他的类去实现,所以在方法前不能使用像final,static,protected等关键字。
例题20:
11.for(inti=0;i<3;i++){
12.switch(i){
13.case0:
break;
14.case1:
System.out.print(“one“);
15.case2:
System.out.print(“two“);
16.case3:
System.out.print(“three“);
17.}
18.}
19.System.out.println(“done”);
Whatistheresult?
A.done
B.onetwodone
C.onetwothreedone
D.onetwothreetwothreedone
E.Compilationfails.
解答:
C
点评:
考查for循环以及switch,break的用法。
例题21:
1.publicclassOuter{
2.publicvoidsomeOuterMethod(){
3.//Line3
4.}
5.publicclassInner{}
6.publicstaticvoidmain(String[]argv){
7.Outero=newOuter();
8.//Line8
9.}
10.}
WhichinstantiatesaninstanceofInner?
A.newInner();//Atline3
B.newInner();//Atline8
C.newo.Inner();//Atline8
D.newOuter.Inner();//Atline8
解答:
A
点评:
关于内部类的声明,我们可以直接在非静态的方法中line3:
newInner或利用外部类对象oline8:
o.newInner()产生内部类实例。
例题22:
1.publicclassTest{
2.publicstaticvoidaMethod()throwsException{
3.try{
4.thrownewException();
5.}finally{
6.System.out.println(“finally”);
7.}
8.}
9.publicstaticvoidmain(Stringargs[]){
10.try{
11.aMethod();
12.}catch(Exceptione){
13.System.out.println(“exception”);
14.}
15.System.out.println(“finished”);
16.}
17.}
Whatistheresult?
A.finally
B.exception
finished
C.finally
exception
finished
D.Compila
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- SUN 试题 集锦 答案