重载和覆盖.docx
《重载和覆盖.docx》由会员分享,可在线阅读,更多相关《重载和覆盖.docx(14页珍藏版)》请在冰豆网上搜索。
![重载和覆盖.docx](https://file1.bdocx.com/fileroot1/2023-1/29/211bd03c-c194-437f-89e3-cfeeef3a4bc1/211bd03c-c194-437f-89e3-cfeeef3a4bc11.gif)
重载和覆盖
1.Methodsandvariblesthatdothebackgroundworkofaclassareknownasimplementationdetails.Theimplementation
detailsencapsulatedclassshouldbegivenwhichaccess?
c
A.Public
B.Thedefault
C.Private/*˽ÓеÄÄÜʵÏÖϸ½Ú*/
D.Protected
E.Itdoesnotmatter
2.Whichofthefollowingisnotabenefitofencapsulation?
A.Clarityofcode
B.Codeefficiency
C.Theabilitytoaddfuntionalitylateron
D.Modificationrequireslesscodingchanges
3.Given:
1.publicclassRectangle{
2.}
3.
4.publicclassSquareextendsRectangle{
5.}
WhatistherelationshipbetweenRectangleandSquare?
b/*¾ØÐÎÊÇÒ»¸öÕý·½ÐÎ*/
A.hasa
B.isa
C.It'sbothanisaandhasarelationship.
D.Therelationshipisneitheranisaorahasarelationship.
4.Given:
1.publicclassVehicle{
2.privateEnginetheEngine;
3.}
4.
5.publicclassEngine{
6.}
WhatistherelationshipbetweenVehicleandEngine?
a/*¿¨³µÓÐÒ»¸ö·¢¶¯»ú*/
A.hasa
B.isa
C.It'sbothanisaandhasarelationship.
D.Therelationshipisneitheranisaorahasarelationship.
5.Given:
1.publicinterfaceMoveable{
2.publicvoidmoveObject();
3.}
4.
5.publicclassBitmapimplementsMoveable{
6.publicvoidmoveObject(){
7.//bodyomitted
8.}
9.}
WhatistherelationshipbetweenMovableandBitmap?
b
A.hasa
B.isa
C.It'sbothanisaandhasarelationship.
D.Therelationshipisneitheranisanorahasarelationship.
6.Given:
1.classSuperTest{
2.publicdoublegetNumber(){
3.returnMath.random()*100;
4.}
5.}
6.
7.classTestextendsSuperTest{
8.publicintgetNumber(){
9.return(int)(Math.random()*100);
10.}
11.publicstaticvoidmain(String[]args){
12.Testt=newTest();
13.System.out.println(t.getNumber());
14.}
15.}
Whichoftheseresultscouldoccurfromthiscode?
(Chooseallthatapply)f/*×ÓÀà·½·¨µÄ·µ»ØÀàÐÍСÓÚ¸¸Àà*/
A.59
B.34.234
C.134.234
D.134
E.100
F.Thiscodewillnotcompile.
7.Given:
1.classSuperTest{
2.publicdoublegetNumber(){
3.returnMath.random()*100;
4.}
5.}
6.
7.classTestextendsSuperTest{
8.publicintgetNumber(intmax){
9.return(int)(Math.random()*max);
10.}
11.
12.publicIntegergetNumber(intmax){
13.inttemp=(int)(Math.random()*max);
14.returnnewInteger(temp);
15.}
16.
17.publicstaticvoidmain(String[]args){
18.Testt=newTest();
19.System.out.println(t.getNumber());
20.}
21.}
Whichofthesestatementsistrue?
b
A.Thecodewillnotcompilecorrectlybecauseline19istryingtocallamethodthatdoesnotexistfortheclassTest.
B.Thecodedoesnotcompilebecauseofline12
C.Thecodedoesnotcompilebecauseofline14
D.Thereisnoambiguityastothereutrntypeinline12;therefore,thiscodewillcompileandoutputadoublevalue
greaterthanorequalto0andlessthan100.
E.ThiscodewillnotcompilebecausethegetNumber()methodsintheTestclassmustallhavereturntypesofdouble.
8.Given:
1.publicclassA{
2.publicstaticvoidmain(String[]args){
3.newA().printResult(1,"abc",4);
4.}
5.privatevoidprintResult(inta,longb,intc){
6.System.out.println(a+b+c);
7.}
8.privatevoidprintResult(longa,Stringb,intc){
9.System.out.println(a+b+c);
10.}
11.}
Whichofthesestatementsistrue?
b
A.ThecodewillnotcompilecorrectlybecausealongandaStringmaynotbeaddedtogetherinline9.
B.Thecodewillcompileandrunandwilldisplay1abc4inthestandardoutput.
B.Thecodewillcompileandrunandwilldisplay11234inthestandardoutput.
D.ThecodewillcompileandwillthrowaNumberFormatException.
9.Whenasubclassmethodhasthesamename,parameterlist,andreturntypeasamethodintheparentclass,thismethodissaidtobewhat?
d
A.Extended
B.Overloaded
C.Overextended
D.Overridden/*¸²¸Ç*/
10.Whichofthefollowingmustmatchexactlyforoverloadedmethodstocompilecorreclty?
(Choosethatallapply)d
A.Theparameterlist
B.Thereturntype
C.Theexceptionthrown
D.Noneoftheabove
11.Given:
1.publicclassTest{
2.publicstaticvoidmain(String[]args){
3.newTest().foo(1,2);
4.}
5.privatevoidfoo(inta,intb){
6.System.out.println("int");
7.}
8.privatevoidfoo(longa,intb){
9.System.out.println("long");
10.}
11.}
Whatwillhappenwhenyoucompileandrunthisprogram?
a
A.Thecodewillcompileandrun,anddisplayint.
B.Thecodewillcompileandrun,anddisplaylong.
C.Thecodewillnotcompile.
D.Thecodewillcompile,butthrowaruntimeexceptionwhenexecuted.
12.Given:
1.publicclassParent{
2.publicvoidexecute()throwsException{
3.//bodyomitted
4.}
5.}
6.publicclassChildextendsParent{
7.privatevoidexecute()throwsClassCastException{
8.//bodyomitted
9.}
10.}
Whatwillhappenwhenyoucompilethisprogram?
a
A.Theprogramwillcompilesuccessfully.
B.Theprogramwillnotcompilebecausetheexecute()inthesubclassthrowsadifferentexceptionthanintheparentclass.
C.Theprogramwillnotcompilebecausetheexecute()inthesubclasshaslessaccessibilitythanintheparentclass.
D.Theprogramwillnotcompilebecausetheexecute()inthesubclasshaslessaccessibliityandthrowsadifferentexceptionthanin
theparentclass.
13.Given:
1.publicclassA{
2.publicvoidbaz(){
3.System.out.println("A");
4.}
5.}
6.publicclassBextendsA{
7.publicstaticvoidmain(String[]args){
8.Aa=newB();
9.a.baz();
10.}
11.publicvodbaz(){
12.System.out.println("B");
13.}
14.}
Whatwillhappenwhenyoucompileandrunthisprogram?
b
A.Theprogramcompiles,runs,anddisplayAinthestandardoutput.
B.Theprogramcompiles,runs,anddisplayBinthestandardoutput.
C.Theprogramcompiles,butthrowsaruntimeexception.
D.Theprogramdoesnotcompile.
14.Given:
1.publicclassA{
2.publicstaticvoidmain(String[]args){
3.newA().baz(1,2);
4.}
5.privatevoidbaz(inta,intb){
6.System.out.println("baz");
7.}
8.privatevoidfoo(inta,intb){
9.System.out.println("first");
10.}
11.privatebooleanfoo(inta,intb){
12.System.out.println("second");
13.returntrue;
14.}
15.}
Whichofthefollowingstatementsistrue?
A.Theprogramcompiles,runs,anddisplaysfirstinthestandardoutput.
B.Theprogramcompiles,runs,anddisplayssecondinthestandardoutput.
C.Theprogramcompiles,runs,anddisplaysbazinthestandardoutput.
D.Theprogramdoesnotcompile.
15.Given:
1.publicclassTest{
2.protectedvoidstart()throwsClassCastException{
3.//bodyomitted
4.}
5.}
6.
7.publicclassChildextendsTest{
8.publicvoidstart()throwsException{
9.//bodyomitted
10.}
11.}
Whatwillhappenwhenyoucompilethisprogram?
A.Theprogramwillcompilesuccessfully.
B.Theprogramwillnotcompilebecausethestart()inthesubclassthrowsadifferentexceptionthanintheparentclass.
C.Theprogramwillnotcompilebecausethestart()inthesubclassispublic,whereasitisprotectedintheparentclass.
D.Theprogramwillnotcompilebecausethestart()inthesubclasshasdifferentaccessibilityandthrowsadifferentexceptionthanintheparentclass.
16.Whentwoormoremethodsinthesameclasshavethesamename,theyaresaidtobewhat?
A.Animplementationdetail
B.Overridden
C.Aninterface
D.Overloaded
17.Given:
1.publicclassExample{
2.publicstaticvoidmain(String[]args){
3.newExample().locate(1.0,2);
4.}
5.privatevoidlocate(doublea,longb){
6.System.out.println("double-long");
7.}
8.privatevoidlocate(longa,intb){
9.System.out.println("long-int");
10.}
11.}
Whatwillhappenwhenyoucompileandrunthisprogram?
b
A.Thecodewillcompile,runanddisplaydouble-long.
B.Thecodewillcompile,runanddisplaylong-int.
C.Thecodewillnotcompile.
D.Thecodewillcompile,butthrowaruntimeexceptionwhenexecuted.
18.Given:
1.classSuperTest{
2.publicSuperTest(){
3.System.out.println("Super!
");
4.}
5.}
6.
7.classTestextendsSuperTest{
8.publicstaticvoidmain(String[]args){
9.newTest();
10.}
11.publicTest(){
12.super();
13.this("Great!
");
14.}
15.
16.publicTest(Stringname){
17.System.out.println(name);
18.}
19.}
Whatistheresultofthiscode?
f/*thisºÍsuperÒª·ÅÔÚµÚÒ»ÐУ¬²»ÄÜÔÙÒ»Æð³öÏÖ*/
A.Great!
Super!
B.Super!
Great!
C.Super!
Super!
Great!
D.Great!
E.Super!
F.Thiscodewillnotcompile.
19.Given:
1.publicclassTest{
2.publicstaticvoidmain(String[]args){
3.newTest(4);
4.}
5.publicTest(){
6.System.out.println("okay");
7.}
8.publicTest(inti){
9.
10.}
11.}
Whichofthefollowinglinesofcodeshouldbeinsertedatline11sothattheoutputoftheprogramisokay?
c
A.super();
B.this;
C.Test();
D.this();
E.super();
20.Given:
1.publicclass