java interview questions collection

Upload: anil

Post on 06-Jul-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/17/2019 Java Interview Questions Collection

    1/7

    Core Java Interview Questions:

    **************

    1) What is immutable object? Can you create immutable object??

    Ans:

    An object is considered immutable i its state can not be chan!ed ater

    it is constructed"

    Immutable objects are #articularly useul in concurrent a##lications"

    since they can not chan!e the state$ they cannot be corru#ted by thread

    interace or observed in in%consistent state"

    &odi'cation o immutable object results in new object" (or eam#le

    strin! is immutable in java

    Immutable objects are automatically thread sae as they can not be

    chan!ed once created

    &ostly immutable objects are 'nal in java$ inorder to #revent sub%class

    rom overridin! methods in java which can com#romise immutability"

    We can achieve same unctionality by main! member non%'nal but

    #rivate and not modiyin! them ece#t in constructor"

    +ro!rammers are oten reluctant to em#loy immutable objects$because they worry about the cost o creatin! a new object as o##osed to

    u#datin! an object in #lace" ,he im#act o object creation is oten

    overestimated$ and can be o-set by some o the e.ciencies associated with

    immutable objects" ,hese include decreased overhead due to !arba!e

    collection$ and the elimination o code needed to #rotect mutable objects

    rom corru#tion"

    /am#les: All wra##er classes in java"lan! are immutable 0 trin!$

    Inte!er$ 2oolean$ Character$ 2yte$ hort$ 3on!$ (loat$ 4ouble$ 2i!4ecimal$

    2i!Inte!er

     ,he reerences to the immutable objects can be easily shared or

    cached without havin! to co#y or clone them as there state can not be

    chan!ed ever ater construction

     ,he best use o the immutable objects is as the eys o a ma#

    *********************

  • 8/17/2019 Java Interview Questions Collection

    2/7

    5) 6ow to create Immutable class??

    Ans: ,o create an immutable class ollowin! ste#s should be ollowed:

    1) Create a 'nal class"

    5) et the values o #ro#erties usin! constructor only"

    7) &ae the #ro#erties o the class 'nal and #rivate

    8) 4o not #rovide any setters or these #ro#erties"

    9) I the instance 'elds include reerences to mutable objects$ dont

    allow those objects to be chan!ed:

    a" 4ont #rovide methods that modiy the mutable objects"

    b" 4ont share reerences to the mutable objects" ;ever store

    reerences to eternal$ mutable objects #assed to the constructor< inecessary$ create co#ies$ and store reerences to the co#ies" imilarly$ create

    co#ies o your internal mutable objects when necessary to avoid returnin! the

    ori!inals in your methods"

    /"!"

    #ublic 'nal class (inal+ersonClass =

      #rivate 'nal trin! name<

    #rivate 'nal int a!e<

    #ublic (inal+ersonClass>'nal trin! name$ 'nal int a!e) =

    su#er>)<

    this"name name<

    this"a!e a!e<

    @

    #ublic int !etA!e>) =

    return a!e<

    @

  • 8/17/2019 Java Interview Questions Collection

    3/7

      #ublic trin! !et;ame>) =

    return name<

    @

    @

    ******************

    7) 4i-erence between creatin! the strin! as new and literal??

    Ans: When we create strin! with new>) #erator$ itBs created in hea# and not

    added into strin! #ool while trin! created usin! literal are created in trin!

    #ool itsel which eists in +ermen area o hea#"

    trin! s new trin!>D,estD)<

    does not #ut the object in trin! #ool $ we need to call trin!"intern>) method

    which is used to #ut them into trin! #ool e#licitly" its only when you create

    trin! object as trin! literal e"!" trin! s D,estD Java automatically #ut that

    into trin! #ool"

    Eead more: htt#:FFjavarevisited"blo!s#ot"comF5G11FG8Fto#%5G%core%java%

    interview%Huestions"htmli5;WhmwKEl

    ******************

    Collections:

    **********

    1) 4i-erence between arraylist and vector??

    Ans: a) Lector is synchronied where as arraylist is non%synchronied" hence

    vector is thread sae

    b) Lector is slower than arraylist$ since the cost should be #aid or

    synchroniation

    c) Lector and arraylist both uses array internally and are dynamically

    re%siable" 2y deault vector doubles the sie o its array$ where as arraylist

    increases its sie by hal" >ie or arraylist o sie n$ sie becomes 7F5nM1"

    http://javarevisited.blogspot.com/2011/04/top-20-core-java-interview-questions.html#ixzz2NWhmwURlhttp://javarevisited.blogspot.com/2011/04/top-20-core-java-interview-questions.html#ixzz2NWhmwURlhttp://javarevisited.blogspot.com/2011/04/top-20-core-java-interview-questions.html#ixzz2NWhmwURlhttp://javarevisited.blogspot.com/2011/04/top-20-core-java-interview-questions.html#ixzz2NWhmwURl

  • 8/17/2019 Java Interview Questions Collection

    4/7

    d) Lector is rom le!acy amily$ but ater arrival o collections

    ramewor$ vecotr is reactored to im#lement the collections list interace"

    e) Lector uses enumarator to iterate over it where as arraylist user

    iterator or list iterator to iterate" Iterator and listIterator are ail%ast where as

    enumerators are not"

    *** Ksin! collections"synchroniedlist>new Ayyar3ist>)) we can mae

    the arraylist synchronied"

    5) 4i-erence between lined3ist and Array3ist?

    Ans: a) 3ined3ist allow seHuential access o the elements >n) where as

    Array3ist allows random access >1)"

    b) Insertions and removals rom the lined3ist is o constant time >1)

    where as or Array3ist i the #lace to insert or remove is otehr than end$ the

    time com#leity is o >n) in worstcase and constant in avera!e case"

    c) 3ined3ist has more overhead in memory as it need to store the

    #ointers to the net element are stored" Array3ist does not ahve overhead"

    d) deault initial sie o the arraylist is 1G>accordin! to J41"8)

    7) 4i-erence between 3ist and et?

    Ans: a) 3ist is ordered where as et is un%ordered

    b) 3ist can store du#licates where as et does not allow du#licates" I

    you insert du#licate in et it will re#lace the older value"

    c) 3ist can store any number o null elements where as et allows

    atmost one null element

    d) 3ined3ist and Array3ist im#lement 3ist interace" 6ashet$

    3ined6ashet and ,ree et im#lement et interace

    8) 4i-erence between 6ashet and 6ash&a#?

    Ans: a) 6ash&a# is im#lementation o &A+ interace$ 6ashet is

    im#lementation o et interace

    b) 6ash&a# stores ey value #air$ where as 6ashet stores objects

    c) +ut method is used to add element whereas in hashset add method

    is used

    d) in 6ash&a# hashcode is !enerated usin! Ney object" (or 6ashet

    member object is used to calculate hash value and it may be same or two

  • 8/17/2019 Java Interview Questions Collection

    5/7

    not eHual objects$ so eHuals>) method is used to 'nd the di-erent elements

    while insertin! in 6ashet

    O e) 6ash&a# is aster than 6ashet because uniHue ey is used to store

    the object

    5) 4i-erence between iterator and enumerator??

    Ans: a) Iterator has remove method and enumerator doesnt have"

    b) Iterator is more secure and sae over iterator as it does not allow

    other threads to modiy the collection when other thread is iteratin! and

    throws current&odi'cation/ce#tion

    7) 4i-erence between iterator and listIterator??

    Ans: a) 3istIterator is a subclass which etends Iterator class

    b) 3istIterator allows traversal in both directions where as Iterator

    allows orward direction only"

    c) 3istIterator allows add and set >addition o new elements and

    modi'cation o elements) where as iterator only allows removin! o elements"

    d) 3istIterator is used only or lists whereas iterator is used or ma#$ set

    and list

    e) 3istIterator has no current element$ its cursor is #laced in between

    the #revious and net elements where as or iterator cursor always #oints to

    s#eci'c inde

    8) 4i-erence between ail%ast and ail%sae?

    Ans: a) (ail%ast iterators throw an ece#tion

    >concurrent&odi'cation/ce#tion) while one thread is iteratin! over the

    collection where as ail%sae iterators does not do that"

    b) concurrent6ash&a# and co#ynWriteArray3ist uses ail%sae

    iterators"

    erialiable and eternaliable:

  • 8/17/2019 Java Interview Questions Collection

    6/7

    4i-erence between com#arator and com#arable and when to use them

    orted &a#" how to sort the elements in a &a#"

    Im#lement custom stac in java

    Im#lement custom Hueue in java

    Im#lament Array3ist asumin! you dont have 3ist interace de'ned in java

    Why multi#le inheritances are not su##orted in Java?

    6ow to detect deadloc and ' it?

    Im#lement sin!leton class in java

    Why eHuals method should be com#atible with com#are,o>)

    Can we call static method with null object?

    6ow does volatile variable wors in Java?

    6ow to detect memory lea in Java"

    di-erence between modi'ers and identi'ers in java

    Can you override #rivate or static method in Java ?

    htt#:FFjavaP"blo!s#ot"inF5G15FGRFto#%1G%tricy%java%interview%Huestions%

    answers"html

    What will ha##en i you call return statement or ystem"eit on try or catch

    bloc ? will 'nally bloc eecute?

    What will ha##en i we #ut a ey object in a 6ash&a# which is already there ?

    I a method throws ;ull+ointer/ce#tion in su#er class$ can we override it

    with a method which throws Euntime/ce#tion?

    Why static methods can not access non%static variables in java?

    htt#:FFjavarevisited"blo!s#ot"s!F5G15FGPF1G%interview%Huestions%on%java%

    !enerics"html

    di-erence between wiat$ slee#

    why wait$ notiy and notiyall are de'ned in object

    can we call the run method directly instead o callin! start

    ways to start thread in java

    http://java67.blogspot.in/2012/09/top-10-tricky-java-interview-questions-answers.htmlhttp://java67.blogspot.in/2012/09/top-10-tricky-java-interview-questions-answers.htmlhttp://javarevisited.blogspot.sg/2012/06/10-interview-questions-on-java-generics.htmlhttp://javarevisited.blogspot.sg/2012/06/10-interview-questions-on-java-generics.htmlhttp://java67.blogspot.in/2012/09/top-10-tricky-java-interview-questions-answers.htmlhttp://java67.blogspot.in/2012/09/top-10-tricky-java-interview-questions-answers.htmlhttp://javarevisited.blogspot.sg/2012/06/10-interview-questions-on-java-generics.htmlhttp://javarevisited.blogspot.sg/2012/06/10-interview-questions-on-java-generics.html

  • 8/17/2019 Java Interview Questions Collection

    7/7

     ,hread Huestions reerence: htt#:FFjavarevisited"blo!s#ot"s!F5G11FGFjava%

    multi%threadin!%interview"html

    why notiy and wait needs to be called in synchronied blocFmethod

    di-erence between strin!$ strin!bu-er and strin! builder

    is java #ass by reerence or #ass by value?? #ass by value

    why main in java is declared as #ublic static void strin!ST

    inner classes and anonymous inner classes and Huestions on it

    can interace be 'nal? ;o" reason it have to im#lemented by someother class"

    Also can interace be #rivate?? i believe no

    what is eterniable

    loclal$ member and static variables

    di-erence between #reem#tive schedulin! and slice schedulin!

    di-erence between U and UU o#erators in java

    what is meant by shallow co#y

    di-erence between abstract class and interace

    Can you override #rivate or static method in Java ?

    ****

    Can interaces contain inner classes?

    htt#:FFjava%success"blo!s#ot"inF#Fjava%jee%tutorials"html

    ****

    http://javarevisited.blogspot.sg/2011/07/java-multi-threading-interview.htmlhttp://javarevisited.blogspot.sg/2011/07/java-multi-threading-interview.htmlhttp://java-success.blogspot.in/p/java-jee-tutorials.htmlhttp://javarevisited.blogspot.sg/2011/07/java-multi-threading-interview.htmlhttp://javarevisited.blogspot.sg/2011/07/java-multi-threading-interview.htmlhttp://java-success.blogspot.in/p/java-jee-tutorials.html