Вы находитесь на странице: 1из 9

9/15/2015

00:29:58

Like

Share

Java Programming Test 3


JavaProgramming

1.

Result&Statistics

publicclassTest{}

Marks:0/20

Whatistheprototypeofthedefaultconstructor?
A.

Test()

B.

Test(void)

Totalnumberofquestions

: 20

Numberofansweredquestions

Numberofunansweredquestions : 20
C.

publicTest()

D.

publicTest(void)

Answer:OptionC

Feedback

Explanation:

QualityoftheTest

Select

OptionAandBarewrongbecausetheyusethedefaultaccessmodifierandtheaccess
modifierfortheclassispublic(remember,thedefaultconstructorhasthesameaccess
modifierastheclass).

DifficultyoftheTest

Select

Comments:
...

OptionDiswrong.Thevoidmakesthecompilerthinkthatthisisamethodspecificationin
factifitwereamethodspecificationthecompilerwouldspititout.
Learnmoreproblemson:DeclarationsandAccessControl

SubmitFeedback

Discussaboutthisproblem:DiscussinForum
TakeanAnotherRandomTest!
2.

WhatisthewidestvalidreturnTypeformethodAinline3?

GotoOnlineTestPage

publicclassReturnIt
{
returnTypemethodA(bytex,doubley)/*Line3*/
{
return(long)x/y*2
}
}

GotoHomePage

A.

int

B.

byte

C.

long

D.

double

Answer:OptionD
Explanation:
HoweverA,BandCareallwrong.Eachofthesewouldresultinanarrowingconversion.
Whereaswewantawideningconversion,thereforetheonlycorrectanswerisD.Don'tbeput
offbythelongcast,thisappliesonlytothevariablexandnottherestoftheexpression.Itis
thevariabley(oftypedouble)thatforcesthewideningconversiontodouble.
Java'swideningconversionsare:
Fromabytetoashort,anint,along,afloat,oradouble.
Fromashort,anint,along,afloat,oradouble.
Fromachartoanint,along,afloat,oradouble.
Fromaninttoalong,afloat,oradouble.
Fromalongtoafloat,oradouble.
Fromafloattoadouble.
Learnmoreproblemson:DeclarationsandAccessControl
Discussaboutthisproblem:DiscussinForum

3.

Whichtwocauseacompilererror?
1. float[]f=newfloat(3)

http://www.indiabix.com/onlinetest/javaprogrammingtest/63

1/9

9/15/2015

00:29:58
2.
3.
4.
5.

floatf2[]=newfloat[]
float[]f1=newfloat[3]
floatf3[]=newfloat[3]
floatf5[]={1.0f,2.0f,2.0f}
A.

2,4

B.

3,5

C.

4,5

D.

1,2

Answer:OptionD
Explanation:
(1)causestwocompilererrors('['expectedandillegalstartofexpression)becausethewrong
typeofbracketisused,()insteadof[].Thefollowingisthecorrectsyntax:float[]f=new
float[3]
(2)causesacompilererror('{'expected)becausethearrayconstructordoesnotspecifythe
numberofelementsinthearray.Thefollowingisthecorrectsyntax:floatf2[]=new
float[3]
(3),(4),and(5)compilewithouterror.
Learnmoreproblemson:DeclarationsandAccessControl
Discussaboutthisproblem:DiscussinForum

4.

Whatwillbetheoutputoftheprogram?
publicclassTest
{
publicstaticvoidmain(Stringargs[])
{
classFoo
{
publicinti=3
}
Objecto=(Object)newFoo()
Foofoo=(Foo)o
System.out.println("i="+foo.i)
}
}
A.

i=3

B.

Compilationfails.

C.

i=5

D.

AClassCastExceptionwilloccur.

Answer:OptionA
Learnmoreproblemson:DeclarationsandAccessControl
Discussaboutthisproblem:DiscussinForum

5.

Whatwillbetheoutputoftheprogram?
publicclassA
{
voidA()/*Line3*/
{
System.out.println("ClassA")
}
publicstaticvoidmain(String[]args)
{
newA()
}
}
A.

ClassA

B.

Compilationfails.

C.

Anexceptionisthrownatline3.

http://www.indiabix.com/onlinetest/javaprogrammingtest/63

2/9

9/15/2015

00:29:58
D.

Thecodeexecuteswithnooutput.

Answer:OptionD
Explanation:
OptionDiscorrect.Thespecificationatline3isforamethodandnotaconstructorandthis
methodisnevercalledthereforethereisnooutput.Theconstructorthatiscalledisthe
defaultconstructor.
Learnmoreproblemson:DeclarationsandAccessControl
Discussaboutthisproblem:DiscussinForum

6.

Whatwillbetheoutputoftheprogram?
classTest
{
publicstaticvoidmain(String[]args)
{
intx=0
inty=0
for(intz=0z<5z++)
{
if((++x>2)||(++y>2))
{
x++
}
}
System.out.println(x+""+y)
}
}
A.

53

B.

82

C.

83

D.

85

Answer:OptionB
Explanation:
Thefirsttwoiterationsoftheforloopbothxandyareincremented.Onthethirditerationxis
incremented,andforthefirsttimebecomesgreaterthan2.Theshortcircuitoroperator||
keepsyfromeverbeingincrementedagainandxisincrementedtwiceoneachofthelast
threeiterations.
Learnmoreproblemson:OperatorsandAssignments
Discussaboutthisproblem:DiscussinForum

7.

importjava.awt.Button
classCompareReference
{
publicstaticvoidmain(String[]args)
{
floatf=42.0f
float[]f1=newfloat[2]
float[]f2=newfloat[2]
float[]f3=f1
longx=42
f1[0]=42.0f
}
}
whichthreestatementsaretrue?
1.
2.
3.
4.
5.

f1==f2
f1==f3
f2==f1[1]
x==f1[0]
f==f1[0]
A.

1,2and3

B.

2,4and5

C.

3,4and5

D.

1,4and5

http://www.indiabix.com/onlinetest/javaprogrammingtest/63

3/9

9/15/2015

00:29:58
Answer:OptionB
Explanation:
(2)iscorrectbecausethereferencevariablesf1andf3refertothesamearrayobject.
(4)iscorrectbecauseitislegaltocompareintegerandfloatingpointtypes.
(5)iscorrectbecauseitislegaltocompareavariablewithanarrayelement.
(3)isincorrectbecausef2isanarrayobjectandf1[1]isanarrayelement.
Learnmoreproblemson:OperatorsandAssignments
Discussaboutthisproblem:DiscussinForum

8.

Whatwillbetheoutputoftheprogram?
publicclassMyProgram
{
publicstaticvoidmain(Stringargs[])
{
try
{
System.out.print("Helloworld")
}
finally
{
System.out.println("Finallyexecuting")
}
}
}
A.

Nothing.Theprogramwillnotcompilebecausenoexceptionsarespecified.

B.

Nothing.Theprogramwillnotcompilebecausenocatchclausesarespecified.

C.

Helloworld.

D.

HelloworldFinallyexecuting

Answer:OptionD
Explanation:
Finallyclausesarealwaysexecuted.Theprogramwillfirstexecutethetryblock,printingHello
world,andwillthenexecutethefinallyblock,printingFinallyexecuting.
OptionA,B,andCareincorrectbasedontheprogramlogicdescribedabove.Rememberthat
eitheracatchorafinallystatementmustfollowatry.Sincethefinallyispresent,thecatchis
notrequired.
Learnmoreproblemson:Exceptions
Discussaboutthisproblem:DiscussinForum

9.

System.out.print("Start")
try
{
System.out.print("Helloworld")
thrownewFileNotFoundException()
}
System.out.print("CatchHere")/*Line7*/
catch(EOFExceptione)
{
System.out.print("Endoffileexception")
}
catch(FileNotFoundExceptione)
{
System.out.print("Filenotfound")
}
andgiventhatEOFExceptionandFileNotFoundExceptionarebothsubclassesofIOException,and
furtherassumingthisblockofcodeisplacedintoaclass,whichstatementismosttrue
concerningthiscode?
A.

Thecodewillnotcompile.

http://www.indiabix.com/onlinetest/javaprogrammingtest/63

4/9

9/15/2015

00:29:58
B.

Codeoutput:StartHelloworldFileNotFound.

C.

Codeoutput:StartHelloworldEndoffileexception.

D.

Codeoutput:StartHelloworldCatchHereFilenotfound.

Answer:OptionA
Explanation:
Line7willcauseacompilererror.Theonlylegalstatementsaftertryblocksareeithercatchor
finallystatements.
OptionB,C,andDareincorrectbasedontheprogramlogicdescribedabove.Ifline7was
removed,thecodewouldcompileandthecorrectanswerwouldbeOptionB.
Learnmoreproblemson:Exceptions
Discussaboutthisproblem:DiscussinForum

10. Whichclassdoesnotoverridetheequals()andhashCode()methods,inheritingthemdirectly
fromclassObject?
A.

java.lang.String

B.

java.lang.Double

C.

java.lang.StringBuffer

D.

java.lang.Character

Answer:OptionC
Explanation:
java.lang.StringBufferistheonlyclassinthelistthatusesthedefaultmethodsprovidedby
classObject.
Learnmoreproblemson:ObjectsandCollections
Discussaboutthisproblem:DiscussinForum

11. Youneedtostoreelementsinacollectionthatguaranteesthatnoduplicatesarestoredandall
elementscanbeaccessedinnaturalorder.Whichinterfaceprovidesthatcapability?
A.

java.util.Map

B.

java.util.Set

C.

java.util.List

D.

java.util.Collection

Answer:OptionB
Explanation:
OptionBiscorrect.Asetisacollectionthatcontainsnoduplicateelements.Theiterator
returnstheelementsinnoparticularorder(unlessthissetisaninstanceofsomeclassthat
providesaguarantee).Amapcannotcontainduplicatekeysbutitmaycontainduplicate
values.ListandCollectionallowduplicateelements.
OptionAiswrong.Amapisanobjectthatmapskeystovalues.Amapcannotcontain
duplicatekeyseachkeycanmaptoatmostonevalue.TheMapinterfaceprovidesthree
collectionviews,whichallowamap'scontentstobeviewedasasetofkeys,collectionof
values,orsetofkeyvaluemappings.Theorderofamapisdefinedastheorderinwhichthe
iteratorsonthemap'scollectionviewsreturntheirelements.Somemapimplementations,like
theTreeMapclass,makespecificguaranteesastotheirorder(ascendingkeyorder)others,
liketheHashMapclass,donot(doesnotguaranteethattheorderwillremainconstantover
time).
OptionCiswrong.Alistisanorderedcollection(alsoknownasasequence).Theuserofthis
interfacehasprecisecontroloverwhereinthelisteachelementisinserted.Theusercan
accesselementsbytheirintegerindex(positioninthelist),andsearchforelementsinthelist.
Unlikesets,liststypicallyallowduplicateelements.
OptionDiswrong.Acollectionisalsoknownasasequence.Theuserofthisinterfacehas
precisecontroloverwhereinthelisteachelementisinserted.Theusercanaccesselements
bytheirintegerindex(positioninthelist),andsearchforelementsinthelist.Unlikesets,lists
typicallyallowduplicateelements.
Learnmoreproblemson:ObjectsandCollections
Discussaboutthisproblem:DiscussinForum

12. /*MissingStatement?*/

http://www.indiabix.com/onlinetest/javaprogrammingtest/63

5/9

9/15/2015

00:29:58
publicclassfoo
{
publicstaticvoidmain(String[]args)throwsException
{
java.io.PrintWriterout=newjava.io.PrintWriter()
newjava.io.OutputStreamWriter(System.out,true)
out.println("Hello")
}
}
Whatlineofcodeshouldreplacethemissingstatementtomakethisprogramcompile?
A.

Nostatementrequired.

B.

importjava.io.*

C.

includejava.io.*

D.

importjava.io.PrintWriter

Answer:OptionA
Explanation:
Theusualmethodforusing/importingthejavapackages/classesisbyusinganimport
statementatthetopofyourcode.Howeveritispossibletoexplicitlyimportthespecificclass
thatyouwanttouseasyouuseitwhichisshowninthecodeabove.Thedisadvantageofthis
howeveristhateverytimeyoucreateanewobjectyouwillhavetousetheclasspathinthe
case"java.io"thentheclassnameinthelongrunleadingtoalotmoretyping.
Learnmoreproblemson:ObjectsandCollections
Discussaboutthisproblem:DiscussinForum

13. Whichistrueaboutananonymousinnerclass?
A.

Itcanextendexactlyoneclassandimplementexactlyoneinterface.

B.

Itcanextendexactlyoneclassandcanimplementmultipleinterfaces.

C.

Itcanextendexactlyoneclassorimplementexactlyoneinterface.

D.

Itcanimplementmultipleinterfacesregardlessofwhetheritalsoextendsaclass.

Answer:OptionC
Explanation:
OptionCiscorrectbecausethesyntaxofananonymousinnerclassallowsforonlyonenamed
typeafterthenew,andthattypemustbeeitherasingleinterface(inwhichcasethe
anonymousclassimplementsthatoneinterface)orasingleclass(inwhichcasethe
anonymousclassextendsthatoneclass).
OptionA,B,D,andEareallincorrectbecausetheydon'tfollowthesyntaxrulesdescribedin
theresponseforanswerOptionC.
Learnmoreproblemson:InnerClasses
Discussaboutthisproblem:DiscussinForum

14. publicclassMyOuter
{
publicstaticclassMyInner
{
publicstaticvoidfoo(){}
}
}
whichstatement,ifplacedinaclassotherthanMyOuterorMyInner,instantiatesaninstanceof
thenestedclass?
A.

MyOuter.MyInnerm=newMyOuter.MyInner()

B.

MyOuter.MyInnermi=newMyInner()

C.

MyOuterm=newMyOuter()
MyOuter.MyInnermi=m.newMyOuter.MyInner()

http://www.indiabix.com/onlinetest/javaprogrammingtest/63

6/9

9/15/2015

00:29:58
D.

MyInnermi=newMyOuter.MyInner()

Answer:OptionA
Explanation:
MyInnerisastaticnestedclass,soitmustbeinstantiatedusingthefullyscopednameof
MyOuter.MyInner.
OptionBisincorrectbecauseitdoesn'tusetheenclosingnameinthenew.
OptionCisincorrectbecauseitusesincorrectsyntax.Whenyouinstantiateanestedclassby
invokingnewonaninstanceoftheenclosingclass,youdonotusetheenclosingname.The
differencebetweenOptionAandCisthatOptionCiscallingnewonaninstanceofthe
enclosingclassratherthanjustnewbyitself.
OptionDisincorrectbecauseitdoesn'tusetheenclosingclassnameinthevariable
declaration.
Learnmoreproblemson:InnerClasses
Discussaboutthisproblem:DiscussinForum

15. WhichtwoarevalidconstructorsforThread?
1.
2.
3.
4.
5.

Thread(Runnabler,Stringname)
Thread()
Thread(intpriority)
Thread(Runnabler,ThreadGroupg)
Thread(Runnabler,intpriority)
A.

1and3

B.

2and4

C.

1and2

D.

2and5

Answer:OptionC
Explanation:
(1)and(2)arebothvalidconstructorsforThread.
(3),(4),and(5)arenotlegalThreadconstructors,although(4)isclose.Ifyoureversethe
argumentsin(4),you'dhaveavalidconstructor.
Learnmoreproblemson:Threads
Discussaboutthisproblem:DiscussinForum

16. WhichtwoofthefollowingmethodsaredefinedinclassThread?
1.
2.
3.
4.
5.

start()
wait()
notify()
run()
terminate()
A.

1and4

B.

2and3

C.

3and4

D.

2and4

Answer:OptionA
Explanation:
(1)and(4).Onlystart()andrun()aredefinedbytheThreadclass.
(2)and(3)areincorrectbecausetheyaremethodsoftheObjectclass.(5)isincorrect
becausethere'snosuchmethodinanythreadrelatedclass.
Learnmoreproblemson:Threads
Discussaboutthisproblem:DiscussinForum

17. Underwhichconditionswillacurrentlyexecutingthreadstop?
1. Whenaninterruptedexceptionoccurs.
2. Whenathreadofhigherpriorityisready(becomesrunnable).

http://www.indiabix.com/onlinetest/javaprogrammingtest/63

7/9

9/15/2015

00:29:58
3. Whenthethreadcreatesanewthread.
4. Whenthestop()methodiscalled.
A.

1and3

B.

2and4

C.

1and4

D.

2and3

Answer:OptionB
Explanation:
Thestatements(2)and(4)makescurrentlyexecutingthreadtostop.
Learnmoreproblemson:Threads
Discussaboutthisproblem:DiscussinForum

18. Whichstatementistrue?
A.

ThenotifyAll()methodmustbecalledfromasynchronizedcontext.

B.

Tocallwait(),anobjectmustownthelockonthethread.

C.

Thenotify()methodisdefinedinclassjava.lang.Thread.

D.

Thenotify()methodcausesathreadtoimmediatelyreleaseitslocks.

Answer:OptionA
Explanation:
OptionAiscorrectbecausethenotifyAll()method(alongwithwait()andnotify())mustalways
becalledfromwithinasynchronizedcontext.
OptionBisincorrectbecausetocallwait(),thethreadmustownthelockontheobjectthat
wait()isbeinginvokedon,nottheotherwayaround.
OptionCiswrongbecausenotify()isdefinedinjava.lang.Object.
OptionDiswrongbecausenotify()willnotcauseathreadtoreleaseitslocks.Thethreadcan
onlyreleaseitslocksbyexitingthesynchronizedcode.
Learnmoreproblemson:Threads
Discussaboutthisproblem:DiscussinForum

19. publicObjectm()
{
Objecto=newFloat(3.14F)
Object[]oa=newObject[l]
oa[0]=o/*Line5*/
o=null/*Line6*/
oa[0]=null/*Line7*/
returno/*Line8*/
}
WhenistheFloatobject,createdinline3,eligibleforgarbagecollection?
A.

justafterline5

B.

justafterline6

C.

justafterline7

D.

justafterline8

Answer:OptionC
Explanation:
OptionAiswrong.Thissimplycopiestheobjectreferenceintothearray.
OptionBiswrong.Thereferenceoissettonull,but,oa[0]stillmaintainsthereferencetothe
Floatobject.
OptionCiscorrect.Thethreadofexecutionwillthennothaveaccesstotheobject.
Learnmoreproblemson:GarbageCollections
Discussaboutthisproblem:DiscussinForum

20. Whatwillbetheoutputoftheprogram?

http://www.indiabix.com/onlinetest/javaprogrammingtest/63

8/9

9/15/2015

00:29:58
Stringa="ABCD"
Stringb=a.toLowerCase()
b.replace('a','d')
b.replace('b','c')
System.out.println(b)
A.

abcd

B.

ABCD

C.

dccd

D.

dcba

Answer:OptionA
Explanation:
Stringobjectsareimmutable,theycannotbechanged,inthiscasewearetalkingaboutthe
replacemethodwhichreturnsanewStringobjectresultingfromreplacingalloccurrencesof
oldCharinthisstringwithnewChar.
b.replace(charoldChar,charnewChar)
ButsincethisisonlyatemporaryStringitmusteitherbeputtousestraightawayi.e.
System.out.println(b.replace('a','d'))
Oranewvariablemustbeassigneditsvaluei.e.
Stringc=b.replace('a','d')
Learnmoreproblemson:Java.langClass
Discussaboutthisproblem:DiscussinForum

SubmitTest

20082015byIndiaBIXTechnologies.AllRightsReserved|Copyright|TermsofUse&PrivacyPolicy

Contactus:info@indiabix.com
Bookmarkto:

http://www.indiabix.com/onlinetest/javaprogrammingtest/63

Followusontwitter!

9/9

Вам также может понравиться