Java 面试题集.docx
《Java 面试题集.docx》由会员分享,可在线阅读,更多相关《Java 面试题集.docx(35页珍藏版)》请在冰豆网上搜索。
Java面试题集
JavaInterviewQuestions
Question:
WhatisthedifferencebetweenanInterfaceandanAbstractclass?
Question:
WhatisthepurposeofgarbagecollectioninJava,andwhenisitused?
Question:
Describesynchronizationinrespecttomultithreading.
Question:
Explaindifferentwayofusingthread?
Question:
Whatarepassbyreferenceandpassbyvalue?
Question:
WhatisHashMapandMap?
Question:
DifferencebetweenHashMapandHashTable?
Question:
DifferencebetweenVectorandArrayList?
Question:
DifferencebetweenSwingandAwt?
Question:
Whatisthedifferencebetweenaconstructorandamethod?
Question:
WhatisanIterator?
Question:
Statethesignificanceofpublic,private,protected,defaultmodifiersbothsinglyandincombinationandstatetheeffectofpackagerelationshipsondeclareditemsqualifiedbythesemodifiers.
Question:
Whatisanabstractclass?
Question:
Whatisstaticinjava?
Question:
Whatisfinal?
Q:
WhatisthedifferencebetweenanInterfaceandanAbstractclass?
A:
Anabstractclasscanhaveinstancemethodsthatimplementadefaultbehavior.AnInterfacecanonlydeclareconstantsandinstancemethods,butcannotimplementdefaultbehaviorandallmethodsareimplicitlyabstract.Aninterfacehasallpublicmembersandnoimplementation.Anabstractclassisaclasswhichmayhavetheusualflavorsofclassmembers(private,protected,etc.),buthassomeabstractmethods.
.
TOP
Q:
WhatisthepurposeofgarbagecollectioninJava,andwhenisitused?
A:
Thepurposeofgarbagecollectionistoidentifyanddiscardobjectsthatarenolongerneededbyaprogramsothattheirresourcescanbereclaimedandreused.AJavaobjectissubjecttogarbagecollectionwhenitbecomesunreachabletotheprograminwhichitisused.
TOP
Q:
Describesynchronizationinrespecttomultithreading.
A:
Withrespecttomultithreading,synchronizationisthecapabilitytocontroltheaccessofmultiplethreadstosharedresources.Withoutsynchonization,itispossibleforonethreadtomodifyasharedvariablewhileanotherthreadisintheprocessofusingorupdatingsamesharedvariable.Thisusuallyleadstosignificanterrors.
TOP
Q:
Explaindifferentwayofusingthread?
A:
ThethreadcouldbeimplementedbyusingrunnableinterfaceorbyinheritingfromtheThreadclass.Theformerismoreadvantageous,'causewhenyouaregoingformultipleinheritance..theonlyinterfacecanhelp.
TOP
Q:
Whatarepassbyreferenceandpassbyvalue?
A:
PassByReferencemeans thepassingtheaddressitselfratherthanpassingthevalue.PassbyValuemeans passing acopyofthevaluetobepassed.
TOP
Q:
WhatisHashMapandMap?
A:
MapisInterfaceandHashmapisclassthatimplementsthat.
TOP
Q:
DifferencebetweenHashMapandHashTable?
A:
TheHashMapclassisroughlyequivalenttoHashtable,exceptthatitisunsynchronizedandpermitsnulls.(HashMapallowsnullvaluesaskeyandvaluewhereasHashtabledoesntallow).HashMapdoesnotguaranteethattheorderofthemapwillremainconstantovertime.HashMapisunsynchronizedandHashtableissynchronized.
TOP
Q:
DifferencebetweenVectorandArrayList?
A:
Vectorissynchronizedwhereasarraylistisnot.
TOP
Q:
DifferencebetweenSwingandAwt?
A:
AWTareheavy-weightcomponenets.Swingsarelight-weightcomponents.HenceswingworksfasterthanAWT.
TOP
Q:
Whatisthedifferencebetweenaconstructorandamethod?
A:
Aconstructorisamemberfunctionofaclassthatisusedtocreateobjectsofthatclass.Ithasthesamenameastheclassitself,hasnoreturntype,andisinvokedusingthenewoperator.
Amethodisanordinarymemberfunctionofaclass.Ithasitsownname,areturntype(whichmaybevoid),andisinvokedusingthedotoperator.
TOP
Q:
WhatisanIterator?
A:
Someofthecollectionclassesprovidetraversaloftheircontentsviaajava.util.Iteratorinterface.Thisinterfaceallowsyoutowalkthroughacollectionofobjects,operatingoneachobjectinturn.RememberwhenusingIteratorsthattheycontainasnapshotofthecollectionatthetimetheIteratorwasobtained;generallyitisnotadvisabletomodifythecollectionitselfwhiletraversinganIterator.
TOP
Q:
Statethesignificanceofpublic,private,protected,defaultmodifiersbothsinglyandincombinationandstatetheeffectofpackagerelationshipsondeclareditemsqualifiedbythesemodifiers.
A:
public:
Publicclassisvisibleinotherpackages,fieldisvisibleeverywhere(classmustbepublictoo)
private:
Privatevariablesormethodsmaybeusedonlybyaninstanceofthesameclassthatdeclaresthevariableormethod,Aprivatefeaturemayonlybeaccessedbytheclassthatownsthefeature.
protected:
Isavailabletoallclassesinthesamepackageandalsoavailabletoallsubclassesoftheclassthatownstheprotectedfeature.Thisaccessisprovidedeventosubclassesthatresideinadifferentpackagefromtheclassthatownstheprotectedfeature.
default:
Whatyougetbydefaultie,withoutanyaccessmodifier(ie,publicprivateorprotected).Itmeansthatitisvisibletoallwithinaparticularpackage.
TOP
Q:
Whatisanabstractclass?
A:
Abstractclassmustbeextended/subclassed(tobeuseful).Itservesasatemplate.Aclassthatisabstractmaynotbeinstantiated(ie,youmaynotcallitsconstructor),abstractclassmaycontainstaticdata.Anyclasswithanabstractmethodisautomaticallyabstractitself,andmustbedeclaredassuch.
Aclassmaybedeclaredabstractevenifithasnoabstractmethods.Thispreventsitfrombeinginstantiated.
TOP
Q:
Whatisstaticinjava?
A:
Staticmeansoneperclass,notoneforeachobjectnomatterhowmanyinstanceofaclassmightexist.Thismeansthatyoucanusethemwithoutcreatinganinstanceofaclass.Staticmethodsareimplicitlyfinal,becauseoverridingisdonebasedonthetypeoftheobject,andstaticmethodsareattachedtoaclass,notanobject.Astaticmethodinasuperclasscanbeshadowedbyanotherstaticmethodinasubclass,aslongastheoriginalmethodwasnotdeclaredfinal.However,youcan'toverrideastaticmethodwithanonstaticmethod.Inotherwords,youcan'tchangeastaticmethodintoaninstancemethodinasubclass.
TOP
Q:
Whatisfinal?
A:
Afinalclasscan'tbeextendedie.,finalclassmaynotbesubclassed.Afinalmethodcan'tbeoverriddenwhenitsclassisinherited.Youcan'tchangevalueofafinalvariable(isaconstant).
Question:
Whatifthemainmethodisdeclaredasprivate?
Question:
Whatifthestaticmodifierisremovedfromthesignatureofthemainmethod?
Question:
WhatifIwritestaticpublicvoidinsteadofpublicstaticvoid?
Question:
WhatifIdonotprovidetheStringarrayastheargumenttothemethod?
Question:
WhatisthefirstargumentoftheStringarrayinmainmethod?
Question:
IfIdonotprovideanyargumentsonthecommandline,thentheStringarrayofMainmethodwillbeemptyornull?
Question:
Howcanoneprovethatthearrayisnotnullbutemptyusingonelineofcode?
Question:
WhatenvironmentvariablesdoIneedtosetonmymachineinordertobeabletorunJavaprograms?
Question:
Cananapplicationhavemultipleclasseshavingmainmethod?
Question:
CanIhavemultiplemainmethodsinthesameclass?
Question:
DoIneedtoimportjava.langpackageanytime?
Why?
Question:
CanIimportsamepackage/classtwice?
WilltheJVMloadthepackagetwiceatruntime?
Question:
WhatareCheckedandUnCheckedException?
Question:
WhatisOverriding?
Question:
Whataredifferenttypesofinnerclasses?
Q:
Whatifthemainmethodisdeclaredasprivate?
A:
Theprogramcompilesproperlybutatruntimeitwillgive"Mainmethodnotpublic."message.
[ReceivedfromSandeshSadhale]
TOP
Q:
Whatifthestaticmodifierisremovedfromthesignatureofthemainmethod?
A:
Programcompiles.Butatruntimethrowsanerror"NoSuchMethodError".
[ReceivedfromSandeshSadhale]
TOP
Q:
WhatifIwritestaticpublicvoidinsteadofpublicstaticvoid?
A:
Programcompilesandrunsproperly.
[ReceivedfromSandeshSadhale]
TOP
Q:
WhatifIdonotprovidetheStringarrayastheargumenttothemethod?
A:
Programcompilesbutthrowsaruntimeerror"NoSuchMethodError".
[ReceivedfromSandeshSadhale]
TOP
Q:
WhatisthefirstargumentoftheStringarrayinmainmethod?
A:
TheStringarrayisempty.Itdoesnothaveanyelement.ThisisunlikeC/C++wherethefirstelementbydefaultistheprogramname.
[ReceivedfromSandeshSadhale]
TOP
Q:
IfIdonotprovideanyargumentsonthecommandline,thentheStringarrayofMainmethodwillbeemptyornull?
A:
Itisempty.Butnotnull.
[ReceivedfromSandeshSadhale]
TOP
Q:
Howcanoneprovethatthearrayisnotnullbutemptyusingonelineofcode?
A:
Printargs.length.Itwillprint0.Thatmeansitisempty.ButifitwouldhavebeennullthenitwouldhavethrownaNullPointerExceptiononattemptingtoprintargs.length.
[ReceivedfromSandeshSadhale]
TOP
Q:
WhatenvironmentvariablesdoIneedtosetonmymachineinordertobeabletorunJavaprograms?
A:
CLASSPATHandPATHarethetwovariables.
[ReceivedfromSandeshSadhale]
TOP
Q:
Cananapplicationhavemultipleclasseshavingmainmethod?
A:
Yesitispossible.Whilestartingtheapplicationwementiontheclassnametoberun.TheJVMwilllookfortheMainmethodonlyintheclas