JAVA基础面试题3答案版.docx

上传人:b****7 文档编号:26247789 上传时间:2023-06-17 格式:DOCX 页数:25 大小:22.73KB
下载 相关 举报
JAVA基础面试题3答案版.docx_第1页
第1页 / 共25页
JAVA基础面试题3答案版.docx_第2页
第2页 / 共25页
JAVA基础面试题3答案版.docx_第3页
第3页 / 共25页
JAVA基础面试题3答案版.docx_第4页
第4页 / 共25页
JAVA基础面试题3答案版.docx_第5页
第5页 / 共25页
点击查看更多>>
下载资源
资源描述

JAVA基础面试题3答案版.docx

《JAVA基础面试题3答案版.docx》由会员分享,可在线阅读,更多相关《JAVA基础面试题3答案版.docx(25页珍藏版)》请在冰豆网上搜索。

JAVA基础面试题3答案版.docx

JAVA基础面试题3答案版

JAVA语言基础笔试题-3

Question1

Whichtwoaretrue?

(Choosetwo.)

A.Anencapsulated,publicclasspromotesre-use.

B.Classesthatsharethesameinterfacearealwaystightlyencapsulated.

C.Anencapsulatedclassallowssubclassestooverloadmethods,butdoesNOTallowoverridingmethods.

D.Anencapsulatedclassallowsaprogrammertochangean

implementationwithoutaffectingoutsidecode.

答案:

AD

考点:

封装的概念

说明:

  从选项A角度,我们可看出这个是javabean的概念,javabean提高了组件重用。

共享同一接口的多个类,编程中如果我们只是依赖于接口,则就不依赖于具体的类,提高了代码的可替换性,降低了耦合,这点和封装没什么关系。

封装和重写、重载没有什么关系,被封装的类,属性是私有的,而方法是公有的,允许通过公有方法,对私有属性进行访问。

选项D,可从javabean角度进行思考,javabean的getter和setter方法可以包含大量的逻辑,而没有对外公开,外界只是调用即可。

Question2

Given:

1.packagetest;

2.

3.classTarget{

4.publicStringname=“hello”;

5.}

Whatcandirectlyaccessandchangethevalueofthevariablename?

A.anyclass

B.onlytheTargetclass

C.anyclassinthetestpackage

D.anyclassthatextendsTarget

答案:

C

考点:

类的访问修饰符、public访问修饰符

说明:

此处Target类非public,所以只能被test包中的类进行访问,其它包对该类则不可见。

public最为开放,任何类均可对某个类public属性和方法进行访问,而没有任何约束。

选项D,是选项C的子集,因为能够继承Target的类,一定也在test包中,否则根本不可能见到Target类。

Question3

Given:

1.publicclassTarget{

2.privateinti=0;

3.publicintaddOne(){

4.return++i;

5.}

6.}

And:

1.publicclassClient{

2.publicstaticvoidmain(String[]args){

3.System.out.println(newTarget().addOne());

4.}

5.}

WhichchangecanyoumaketoTargetwithoutaffectingClient?

A.Line4ofclassTargetcanbechangedtoreturni++;

B.Line2ofclassTargetcanbechangedtoprivateinti=1;

C.Line3ofclassTargetcanbechangedtoprivateintaddOne(){

D.Line2ofclassTargetcanbechangedtoprivateIntegeri=0;

答案:

D

考点:

++一元运算符、访问修饰符、自动装箱/拆箱

说明:

++在前,先自增再参与运算;++在后,则先参与运算,再自增。

从这个角度看,选项A是不对的。

选项B修改了初始值,结果出现了不同。

选项C设置了最严格的访问修饰符,导致方法不可见。

选项D在jdk1.5以上不会存在问题,这里存在自动装箱,拆箱操作。

 

Question4

Given:

1.packagegeometry;

2.publicclassHypotenuse{

3.publicInnerTriangleit=newInnerTriangle();

4.classInnerTriangle{

5.publicintbase;

6.publicintheight;

7.}

8.}

Whichistrueabouttheclassofanobjectthatcanreferencethe

variablebase?

A.Itcanbeanyclass.

B.Noclasshasaccesstobase.

C.Theclassmustbelongtothegeometrypackage.

D.TheclassmustbeasubclassoftheclassHypotenuse.

 

答案:

C

考点:

访问修饰符

说明:

由于内部类Triangle的访问修饰符为包级别,其不能在geometry包外部被访问和创建实例。

 

Question5

Given:

11.classClassA{}

12.classClassBextendsClassA{}

13.classClassCextendsClassA{}

and:

21.ClassAp0=newClassA();

22.ClassBp1=newClassB();

23.ClassCp2=newClassC();

24.ClassAp3=newClassB();

25.ClassAp4=newClassC();

Whichthreearevalid?

(Choosethree.)

A.p0=p1;

B.p1=p2;

C.p2=p4;

D.p2=(ClassC)p1;

E.p1=(ClassB)p3;

F.p2=(ClassC)p4;

 

答案:

AEF

考点:

继承环境下的引用变量指向

说明:

父类引用变量可以指向子类对象;子类引用变量不能指向父类对象。

拥有同一父类的兄弟类之间不能互相指向。

 

Question6

Given:

11.classAnimal{publicStringnoise(){return“peep”;}}

12.classDogextendsAnimal{

13.publicStringnoise(){return“bark”;}

14.}

15.classCatextendsAnimal{

16.publicStringnoise(){return“meow”;}

17.}

.....

30.Animalanimal=newDog();

31.Catcat=(Cat)animal;

32.System.out.printIn(cat.noise());

Whatistheresult?

A.peep

B.bark

C.meow

D.Compilationfails.

E.Anexceptionisthrownatruntime.

答案:

E

考点:

继承环境下的引用变量指向问题,重写的概念

说明:

第31行,企图把Dog转成Cat,这个问题在编译的过程不会被查到,但在运行时,会抛出ClassCastingException(类转换异常)

 

Question7

Given:

11.abstractclassVehicle{publicintspeed(){return0;}}

12.classCarextendsVehicle{publicintspeed(){return60;}}

13.classRaceCarextendsCar{publicintspeed(){return150;}}

......

21.RaceCarracer=newRaceCar();

22.Carcar=newRaceCar();

23.Vehiclevehicle=newRaceCar();

24.System.out.println(racer.speed()+“,‘+car.speed()

25.+“,“+vehicle.speed());

Whatistheresult?

A.0,0,0

B.150,60,0

C.Compilationfails.

D.150,150,150

E.Anexceptionisthrownatruntime.

答案:

D

考点:

多态的概念

说明:

子类重写父类方法,通过父类引用变量指向子类对象,恰好调用的重写方法,最终还是调用的是最外层的该方法定义。

 

Question8

Given:

10.abstractclassA{

11.abstractvoida1();

12.voida2(){}

13.}

14.classBextendsA{

15.voida1(){}

16.voida2(){}

17.}

18.classCextendsB{voidc1(){}}

and:

Ax=newB();Cy=newC();Az=newC();

Whichfourarevalidexamplesofpolymorphicmethodcalls?

(Choose

four.)

A.x.a2();

B.z.a2();

C.z.c1();

D.z.a1();

E.y.c1();

F.x.a1();

答案:

ABDF

考点:

多态的概念

说明:

选项C调用失败,由于类A并没有C1这个方法,不可能提出这个调用请求。

选项E不属于多态范畴。

Question9

Given:

10.interfaceA{voidx();}

11.classBimplementsA{publicvoidx(){}publicvoidy(){}}

12.classCextendsB{publicvoidx(){}}

And:

20.java.util.Listlist=newjava.util.ArrayList();

21.list.add(newB());

22.list.add(newC());

23.for(Aa:

list){

24.a.x();

25.a.y();

26.}

Whatistheresult?

A.Thecoderunswithnooutput.

B.Anexceptionisthrownatruntime.

C.Compilationfailsbecauseofanerrorinline20.

D.Compilationfailsbecauseofanerrorinline21.

E.Compilationfailsbecauseofanerrorinline23.

F.Compilationfailsbecauseofanerrorinline25.

答案:

F

考点:

接口,泛型,新版for循环

说明:

接口引用变量可以指向所有实现了该接口的对象,通过接口引用变量只能调用接口中定义的方法。

本例由于B和C均实现了A接口,故均可放入LIST结构中。

C实现了A接口,是间接进行的,因为其父类B实现了A接口。

 

Question10

Given:

1.classSuperClass{

2.publicAgetA(){

3.returnnewA();

4.}

5.}

6.classSubClassextendsSuperClass{

7.publicBgetA(){

8.returnnewB();

9.}

10.}

Whichistrue?

A.CompilationwillsucceedifAextendsB.

B.CompilationwillsucceedifBextendsA.

C.Compilationwillalwaysfailbecauseofanerrorinline7.

D.Compilationwillalwaysfailbecauseofanerrorinline8.

答案:

B

考点:

重写关于返回值定义

说明:

重写返回值一般必须一致,形成返回值类型的对称,但也可以不同,只要子类重写方法返回值签名类型是父类方法的子类即可,可以通过”is判断”。

 

Question11

Given:

1.interfaceA{publicvoidaMethod();}

2.interfaceB{publicvoidbMethod();}

3.interfaceCextendsA,B{publicvoidcMethod();}

4.classDimplementsB{

5.publicvoidbMethod(){}

6.}

7.classEextendsDimplementsC{

8.publicvoidaMethod(){}

9.publicvoidbMethod(){}

10.publicvoidcMethod(){}

11.}

Whatistheresult?

A.Compilationfailsbecauseofanerrorinline3.

B.Compilationfailsbecauseofanerrorinline7.

C.Compilationfailsbecauseofanerrorinline9.

D.IfyoudefineDe=newE(),thene.bMethod()invokestheversion

ofbMethod()definedinLine5.

E.IfyoudefineDe=(D)(newE()),thene.bMethod()invokesthe

versionofbMethod()definedinLine5.

F.IfyoudefineDe=(D)(newE()),thene.bMethod()invokesthe

versionofbMethod()definedinLine9.

答案:

F

考点:

多态

说明:

接口可以继承多个接口

多态的概念,即使进行这样的转换:

De=(D)(newE()),不能对E对象造成任何影响。

 

Question12

Given:

10.interfaceA{publicintgetValue()}

11.classBimplementsA{

12.publicintgetValue(){return1;}

13.}

14.classCextendsB{

15.//insertcodehere

16.}

Whichthreecodefragments,insertedindividuallyatline15,makeuse

ofpolymorphism?

(Choosethree.)

A.publicvoidadd(Cc){c.getValue();}

B.publicvoidadd(Bb){b.getValue();}

C.publicvoidadd(Aa){a.getValue();}

D.publicvoidadd(Aa,Bb){a.getValue();}

E.publicvoidadd(Cc1,Cc2){c1.getValue();}

答案:

BCD

考点:

多态

说明:

publicvoidadd(Aa){a.getValue();}

我们可以创建一个B的实例,作为该方法的实际参数,形成多态。

 

Question13

Given:

1.classClassA{

2.publicintnumberOfinstances;

3.protectedClassA(intnumberOfinstances){

4.this.numberOflnstances=numberOfinstances;

5.}

6.}

7.publicclassExtendedAextendsClassA{

8.privateExtendedA(intnumberOfinstances){

9.super(numberOflnstances);

10.}

11.publicstaticvoidmain(String[]args){

12.ExtendedAext=newExtendedA(420);

13.System.out.print(ext.numberOflnstances);

14.}

15.}

Whichistrue?

A.420istheoutput.

B.Anexceptionisthrownatruntime.

C.Allconstructorsmustbedeclaredpublic.

D.ConstructorsCANNOTusetheprivatemodifier.

E.ConstructorsCANNOTusetheprotectedmodifier.

答案:

A

考点:

构造方法的访问修饰符

说明:

一个类的构造方法可以使用任何访问修饰符,比如在单例设计模式中,使用private来限制构造方法的调用范围。

父类构造方法的访问修饰符对子类构造方法的访问修饰符的选择,不构成影响,不应该用重写方法的思路来考虑,不存在子类构造方法重写父类构造方法的情况,两个是没有牵扯的方法。

 

Question14

147.Given:

1.publicclassBase{

2.publicstaticfinalStringFOO=“foo”;

3.publicstaticvoidmain(String[]args){

4.Baseb=newBase();

5.Subs=newSub();

6.System.out.print(Base.FOO);

7.System.out.print(Sub.FOO);

8.System.out.print(b.FOO);

9.System.out.print(s.FOO);

10.System.out.print(((Base)s).FOO);

11.}}

12.classSubextendsBase{publicstaticfinalStringFOO=”bar”;}

Whatistheresult?

A.foofoofoofoofoo

B.foobarfoobarbar

C.foobarfoofoofoo

D.foobarfoobarfoo

E.barbarbarbarbar

F.foofoofoobarbar

G.foofoofoobarfoo

答案:

D

考点:

多态的概念

说明:

多态只针对方法有效,对于在子类和父类均出现的变量或者常量,不存在多态效应。

((Base)s).FOO表面有多态的效应上,但我们发现调用的是属性,则忽略。

 

Question15

Whichthreestatementsaretrue?

(Choosethree.)

A.AfinalmethodinclassXcanbeabstractifandonlyifXisabstract.

B.AprotectedmethodinclassXcanbeoverriddenbyanysubclassof

X.

C.Aprivatestaticmethodcanbecalledonlywithinotherstatic

methodsinclassX.

D.Anon-staticpublicfinalmethodinclassXcanbeoverriddeninany

subclassofX.

E.ApublicstaticmethodinclassXcanbecalledbyasubclassofX

withoutexplicitlyreferencingtheclassX.

F.Amethodwiththesamesignatureasaprivatefinalmethodinclass

XcanbeimplementedinasubclassofX.

G.AprotectedmethodinclassXcanbeoverriddenby

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 卡通动漫

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1