Java 面试题集.docx
- 文档编号:30057325
- 上传时间:2023-08-04
- 格式:DOCX
- 页数:35
- 大小:34.43KB
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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 面试题集 试题
![提示](https://static.bdocx.com/images/bang_tan.gif)