This document provides a summary of 24 core Java interview questions and answers. Some key points covered include: interfaces can have inner classes and static variables; the differences between abstract classes and interfaces; transient variables cannot be serialized; synchronization is used to control access to shared resources by multiple threads; and the Collection API provides classes and interfaces for working with collections of objects like lists, sets, and maps.
Download as DOC, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
179 views
Core Java Interview Questions
This document provides a summary of 24 core Java interview questions and answers. Some key points covered include: interfaces can have inner classes and static variables; the differences between abstract classes and interfaces; transient variables cannot be serialized; synchronization is used to control access to shared resources by multiple threads; and the Collection API provides classes and interfaces for working with collections of objects like lists, sets, and maps.
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9
Core Java interview questions
By admin | October 9, 2004
1. Can there be an abstract class with no abstract methods in it? - Yes 2. Can an Interface be final? - No 3. Can an Interface have an inner class? - Yes. 4. public interface abc 5. { 6. static int i=0; void dd(); 7. class a1 . { 9. a1() 10. { 11. int j; 12. System.out.println("inside"); 13. }; 14. public static void main(Strin a1!") 15. { 16. System.out.println("in interfia"); 17. } 1. } 19. } 20. Can we define private and protected modifiers for variables in interfaces? - No 21. What is Externalizable? - !"terna#i$ab#e is an %nter&ace t'at e"tends (eria#i$ab#e %nter&ace. )nd sends data into (treams in *om+ressed ,ormat. %t 'as t-o met'ods, -rite!"terna#.Ob/ectO0+0t o0t1 and read!"terna#.Ob/ect%n+0t in1 22. What modifiers are allowed for methods in an Interface? - On#y +0b#ic and abstract modi&iers are a##o-ed &or met'ods in inter&aces. 23. What is a local, member and a class variable? - 2ariab#es dec#ared -it'in a met'od are 3#oca#4 5ariab#es. 2ariab#es dec#ared -it'in t'e c#ass i.e not -it'in any met'ods are 3member4 5ariab#es .6#oba# 5ariab#es1. 2ariab#es dec#ared -it'in t'e c#ass i.e not -it'in any met'ods and are de&ined as 3static4 are c#ass 5ariab#es 24. What are the different identifier states of a Thread? - 7'e di&&erent identi&iers o& a 7'read are8 9 - 90nnin6 or r0nnab#e t'read, ( - (0s+ended t'read, *: - 7'read -aitin6 on a condition 5ariab#e, ;: - 7'read -aitin6 on a monitor #oc<, ;( - 7'read s0s+ended -aitin6 on a monitor #oc< 25. What are some alternatives to inheritance? - =e#e6ation is an a#ternati5e to in'eritance. =e#e6ation means t'at yo0 inc#0de an instance o& anot'er c#ass as an instance 5ariab#e, and &or-ard messa6es to t'e instance. %t is o&ten sa&er t'an in'eritance beca0se it &orces yo0 to t'in< abo0t eac' messa6e yo0 &or-ard, beca0se t'e instance is o& a <no-n c#ass, rat'er t'an a ne- c#ass, and beca0se it doesn>t &orce yo0 to acce+t a## t'e met'ods o& t'e s0+er c#ass8 yo0 can +ro5ide on#y t'e met'ods t'at rea##y ma<e sense. On t'e ot'er 'and, it ma<es yo0 -rite more code, and it is 'arder to re-0se .beca0se it is not a s0bc#ass1. 26. Wh isn!t there operator overloadin"? - Beca0se *?? 'as +ro5en by e"am+#e t'at o+erator o5er#oadin6 ma<es code a#most im+ossib#e to maintain. %n &act t'ere 5ery near#y -asn>t e5en met'od o5er#oadin6 in @a5a, b0t it -as t'o06't t'at t'is -as too 0se&0# &or some 5ery basic met'ods #i<e +rint.1. Note t'at some o& t'e c#asses #i<e =ataO0t+0t(tream 'a5e 0no5er#oaded met'ods #i<e -rite%nt.1 and -riteByte.1. 27. What does it mean that a method or field is #static$? - (tatic 5ariab#es and met'ods are instantiated on#y once +er c#ass. %n ot'er -ords t'ey are c#ass 5ariab#es, not instance 5ariab#es. %& yo0 c'an6e t'e 5a#0e o& a static 5ariab#e in a +artic0#ar ob/ect, t'e 5a#0e o& t'at 5ariab#e c'an6es &or a## instances o& t'at c#ass. (tatic met'ods can be re&erenced -it' t'e name o& t'e c#ass rat'er t'an t'e name o& a +artic0#ar ob/ect o& t'e c#ass .t'o06' t'at -or<s too1. 7'at>s 'o- #ibrary met'ods #i<e (ystem.o0t.+rint#n.1 -or<. o0t is a static &ie#d in t'e /a5a.#an6.(ystem c#ass. 2. %ow do I convert a numeric I& address li'e ()*+(,+)-+.) into a hostname li'e /ava+sun+com? 29. Strin #ostname = $net%ddress.et&y'ame("1().1*.(+.,(").et-ost'ame(); 30. 0ifference between J1E2J342J05? 31. Wh do threads bloc' on I26? - 7'reads b#oc< on iAo .t'at is enters t'e -aitin6 state1 so t'at ot'er t'reads may e"ec0te -'i#e t'e %AO o+eration is +er&ormed. 32. What is snchronization and wh is it important? - :it' res+ect to m0#tit'readin6, sync'roni$ation is t'e ca+abi#ity to contro# t'e access o& m0#ti+#e t'reads to s'ared reso0rces. :it'o0t sync'roni$ation, it is +ossib#e &or one t'read to modi&y a s'ared ob/ect -'i#e anot'er t'read is in t'e +rocess o& 0sin6 or 0+datin6 t'at ob/ect>s 5a#0e. 7'is o&ten #eads to si6ni&icant errors. 33. Is null a 'eword? - 7'e n0## 5a#0e is not a <ey-ord. 34. Which characters ma be used as the second character of an identifier,but not as the first character of an identifier? - 7'e di6its 0 t'ro06' 9 may not be 0sed as t'e &irst c'aracter o& an identi&ier b0t t'ey may be 0sed a&ter t'e &irst c'aracter o& an identi&ier. 35. What modifiers ma be used with an inner class that is a member of an outer class? - ) .non-#oca#1 inner c#ass may be dec#ared as +0b#ic, +rotected, +ri5ate, static, &ina#, or abstract. 36. %ow man bits are used to represent 7nicode, 89CII, 7T:;(<, and 7T:;, characters? - Bnicode reC0ires 16 bits and )(*%% reC0ire 7 bits. )#t'o06' t'e )(*%% c'aracter set 0ses on#y 7 bits, it is 0s0a##y re+resented as bits. B7,- re+resents c'aracters 0sin6 , 16, and 1 bit +atterns. B7,-16 0ses 16-bit and #ar6er bit +atterns. 37. What are wrapped classes? - :ra++ed c#asses are c#asses t'at a##o- +rimiti5e ty+es to be accessed as ob/ects. 3. What restrictions are placed on the location of a pac'a"e statement within a source code file? - ) +ac<a6e statement m0st a++ear as t'e &irst #ine in a so0rce code &i#e .e"c#0din6 b#an< #ines and comments1. 39. What is the difference between preemptive schedulin" and time slicin"? - Bnder +reem+ti5e sc'ed0#in6, t'e 'i6'est +riority tas< e"ec0tes 0nti# it enters t'e -aitin6 or dead states or a 'i6'er +riority tas< comes into e"istence. Bnder time s#icin6, a tas< e"ec0tes &or a +rede&ined s#ice o& time and t'en reenters t'e +oo# o& ready tas<s. 7'e sc'ed0#er t'en determines -'ic' tas< s'o0#d e"ec0te ne"t, based on +riority and ot'er &actors. 40. What is a native method? - ) nati5e met'od is a met'od t'at is im+#emented in a #an60a6e ot'er t'an @a5a. 41. What are order of precedence and associativit, and how are the used? - Order o& +recedence determines t'e order in -'ic' o+erators are e5a#0ated in e"+ressions. )ssociatity determines -'et'er an e"+ression is e5a#0ated #e&t-to-ri6't or ri6't-to-#e&t 42. What is the catch or declare rule for method declarations? - %& a c'ec<ed e"ce+tion may be t'ro-n -it'in t'e body o& a met'od, t'e met'od m0st eit'er catc' t'e e"ce+tion or dec#are it in its t'ro-s c#a0se. 43. Can an anonmous class be declared as implementin" an interface and extendin" a class? - )n anonymo0s c#ass may im+#ement an inter&ace or e"tend a s0+erc#ass, b0t may not be dec#ared to do bot'. 44. What is the ran"e of the char tpe? - 7'e ran6e o& t'e c'ar ty+e is 0 to 2D16 - 1. 7'is entry -as +osted in @a5a, Net-or<in6. Boo<mar< t'e +erma#in<. Eost a comment or #ea5e a trac<bac<8 7rac<bac< B9F. uestion= :'at is transient 5ariab#eG 8nswer= 7ransient 5ariab#e canHt be seria#i$e. ,or e"am+#e i& a 5ariab#e is dec#ared as transient in a (eria#i$ab#e c#ass and t'e c#ass is -ritten to an Ob/ect(tream, t'e 5a#0e o& t'e 5ariab#e canHt be -ritten to t'e stream instead -'en t'e c#ass is retrie5ed &rom t'e Ob/ect(tream t'e 5a#0e o& t'e 5ariab#e becomes null. >uestion= Name t'e containers -'ic' 0ses Border Fayo0t as t'eir de&a0#t #ayo0tG 8nswer= *ontainers -'ic' 0ses Border Fayo0t as t'eir de&a0#t are8 -indo-, ,rame and =ia#o6 c#asses.
>uestion= :'at do yo0 0nderstand by (ync'roni$ationG 8nswer= (ync'roni$ation is a +rocess o& contro##in6 t'e access o& s'ared reso0rces by t'e m0#ti+#e t'reads in s0c' a manner t'at on#y one t'read can access one reso0rce at a time. %n non sync'roni$ed m0#tit'readed a++#ication, it is +ossib#e &or one t'read to modi&y a s'ared ob/ect -'i#e anot'er t'read is in t'e +rocess o& 0sin6 or 0+datin6 t'e ob/ectHs 5a#0e. (ync'roni$ation +re5ents s0c' ty+e o& data corr0+tion. E+"+ 9nchronizin" a function= +0b#ic sync'roni$ed 5oid ;et'od1 .1 I AA )++ro+riate met'od-re#ated code. J E+"+ 9nchronizin" a bloc' of code inside a function= +0b#ic my,0nction .1I sync'roni$ed .t'is1 I AA (ync'roni$ed code 'ere. J J
>uestion= :'at is *o##ection )E%G 8nswer= 7'e *o##ection )E% is a set o& c#asses and inter&aces t'at s0++ort o+eration on co##ections o& ob/ects. 7'ese c#asses and inter&aces are more &#e"ib#e, more +o-er&0#, and more re60#ar t'an t'e 5ectors, arrays, and 'as'tab#es i& e&&ecti5e#y re+#aces. Example of classes8 -as#Set, -as#.ap, %rray/ist, /in0ed/ist, 1reeSet and 1ree.ap. Example of interfaces8 2ollection, Set, /ist and .ap.
>uestion= %s %terator a *#ass or %nter&aceG :'at is its 0seG 8nswer= %terator is an inter&ace -'ic' is 0sed to ste+ t'ro06' t'e e#ements o& a *o##ection.
>uestion= :'at is simi#aritiesAdi&&erence bet-een an )bstract c#ass and %nter&aceG 8nswer= =i&&erences are as &o##o-s8 %nter&aces +ro5ide a &orm o& m0#ti+#e in'eritance. ) c#ass can e"tend on#y one ot'er c#ass. %nter&aces are #imited to +0b#ic met'ods and constants -it' no im+#ementation. )bstract c#asses can 'a5e a +artia# im+#ementation, +rotected +arts, static met'ods, etc. ) *#ass may im+#ement se5era# inter&aces. B0t in case o& abstract c#ass, a c#ass may e"tend on#y one abstract c#ass. %nter&aces are s#o- as it reC0ires e"tra indirection to to &ind corres+ondin6 met'od in in t'e act0a# c#ass. )bstract c#asses are &ast. (imi#arities8 Neit'er )bstract c#asses or %nter&ace can be instantiated.
>uestion= Ko- to de&ine an )bstract c#assG 8nswer= ) c#ass containin6 abstract met'od is ca##ed )bstract c#ass. )n )bstract c#ass canHt be instantiated. !"am+#e o& )bstract c#ass8 abstract c#ass test)bstract*#ass I +rotected (trin6 my(trin6L +0b#ic (trin6 6et;y(trin6.1 I ret0rn my(trin6L J +0b#ic abstract strin6 any)bstract,0nction.1L J
>uestion= Ko- to de&ine an %nter&aceG 8nswer= %n @a5a %nter&ace de&ines t'e met'ods b0t does not im+#ement t'em. %nter&ace can inc#0de constants. ) c#ass t'at im+#ements t'e inter&aces is bo0nd to im+#ement a## t'e met'ods de&ined in %nter&ace. !ma+#e o& %nter&ace8 +0b#ic inter&ace sam+#e%nter&ace I +0b#ic 5oid &0nctionOne.1L +0b#ic #on6 *ON(7)N7MON! N 1000L J
>uestion= !"+#ain t'e 0ser de&ined !"ce+tionsG 8nswer= Bser de&ined !"ce+tions are t'e se+arate !"ce+tion c#asses de&ined by t'e 0ser &or s+eci&ic +0r+osed. )n 0ser de&ined can created by sim+#y s0b-c#assin6 it to t'e !"ce+tion c#ass. 7'is a##o-s c0stom e"ce+tions to be 6enerated .0sin6 t'ro-1 and ca06't in t'e same -ay as norma# e"ce+tions. !"am+#e8 c#ass my*0stom!"ce+tion e"tends !"ce+tion I AA 7'e c#ass sim+#y 'as to e"ist to be an e"ce+tion J
>uestion= !"+#ain t'e ne- ,eat0res o& @=B* 2.0 *ore )E%G 8nswer= 7'e @=B* 2.0 )E% inc#0des t'e com+#ete @=B* )E%, -'ic' inc#0des bot' core and O+tiona# Eac<a6e )E%, and +ro5ides ind0ctria#-stren6t' database com+0tin6 ca+abi#ities. Ne- ,eat0res in @=B* 2.0 *ore )E%8 (cro##ab#e res0#t sets- 0sin6 ne- met'ods in t'e 9es0#t(et inter&ace a##o-s +ro6rammatica##y mo5e t'e to +artic0#ar ro- or to a +osition re#ati5e to its c0rrent +osition @=B* 2.0 *ore )E% +ro5ides t'e Batc' B+dates &0nctiona#ity to t'e /a5a a++#ications. @a5a a++#ications can no- 0se t'e 9es0#t(et.0+dateOOO met'ods. Ne- data ty+es - inter&aces ma++in6 t'e (PF3 data ty+es *0stom ma++in6 o& 0ser-de&ined ty+es .B7=s1 ;isce##aneo0s &eat0res, inc#0din6 +er&ormance 'ints, t'e 0se o& c'aracter streams, &0## +recision &or /a5a.mat'.Bi6=ecima# 5a#0es, additiona# sec0rity, and s0++ort &or time $ones in date, time, and timestam+ 5a#0es.
>uestion= !"+#ain 6arba6e co##ectionG 8nswer= Qarba6e co##ection is one o& t'e most im+ortant &eat0re o& @a5a. Qarba6e co##ection is a#so ca##ed a0tomatic memory mana6ement as @2; a0tomatica##y remo5es t'e 0n0sed 5ariab#esAob/ects .5a#0e is n0##1 &rom t'e memory. Bser +ro6ram cannHt direct#y &ree t'e ob/ect &rom memory, instead it is t'e /ob o& t'e 6arba6e co##ector to a0tomatica##y &ree t'e ob/ects t'at are no #on6er re&erenced by a +ro6ram. !5ery c#ass in'erits finalize?@ met'od &rom /ava+lan"+6b/ect, t'e &ina#i$e.1 met'od is ca##ed by 6arba6e co##ector -'en it determines no more re&erences to t'e ob/ect e"ists. %n @a5a, it is 6ood idea to e"+#icit#y assi6n null into a 5ariab#e -'en no more in 0se. % @a5a on ca##in6 9stem+"c?@ and 1untime+"c?@, @2; tries to recyc#e t'e 0n0sed ob/ects, b0t t'ere is no 60arantee -'en a## t'e ob/ects -i## 6arba6e co##ected.
>uestion= Ko- yo0 can &orce t'e 6arba6e co##ectionG 8nswer= Qarba6e co##ection a0tomatic +rocess and canHt be &orced.
>uestion= :'at is OOE(G 8nswer= OOE is t'e common abbre5iation &or Ob/ect-Oriented Ero6rammin6.
>uestion= =escribe t'e +rinci+#es o& OOE(. 8nswer= 7'ere are t'ree main +rinci+a#s o& oo+s -'ic' are ca##ed Eo#ymor+'ism, %n'eritance and !nca+s0#ation.
>uestion= !"+#ain t'e !nca+s0#ation +rinci+#e. 8nswer= !nca+s0#ation is a +rocess o& bindin6 or -ra++in6 t'e data and t'e codes t'at o+erates on t'e data into a sin6#e entity. 7'is <ee+s t'e data sa&e &rom o0tside inter&ace and mis0se. One -ay to t'in< abo0t enca+s0#ation is as a +rotecti5e -ra++er t'at +re5ents code and data &rom bein6 arbitrari#y accessed by ot'er code de&ined o0tside t'e -ra++er.
>uestion= !"+#ain t'e %n'eritance +rinci+#e. 8nswer= %n'eritance is t'e +rocess by -'ic' one ob/ect acC0ires t'e +ro+erties o& anot'er ob/ect.
>uestion= !"+#ain t'e Eo#ymor+'ism +rinci+#e. 8nswer= 7'e meanin6 o& Eo#ymor+'ism is somet'in6 #i<e one name many &orms. Eo#ymor+'ism enab#es one entity to be 0sed as as 6enera# cate6ory &or di&&erent ty+es o& actions. 7'e s+eci&ic action is determined by t'e e"act nat0re o& t'e sit0ation. 7'e conce+t o& +o#ymor+'ism can be e"+#ained as Rone inter&ace, m0#ti+#e met'odsR.
>uestion= :'at are )ccess (+eci&iers a5ai#ab#e in @a5aG 8nswer= )ccess s+eci&iers are <ey-ords t'at determines t'e ty+e o& access to t'e member o& a c#ass. 7'ese are8 E0b#ic Erotected Eri5ate =e&a0#ts
>uestion= =escribe t'e -ra++er c#asses in @a5a. 8nswer= :ra++er c#ass is -ra++er aro0nd a +rimiti5e data ty+e. )n instance o& a -ra++er c#ass contains, or -ra+s, a +rimiti5e 5a#0e o& t'e corres+ondin6 ty+e. ,o##o-in6 tab#e #ists t'e +rimiti5e ty+es and t'e corres+ondin6 -ra++er c#asses8 &rimitive Wrapper boolean /ava+lan"+Aoolean bte /ava+lan"+Ate char /ava+lan"+Character double /ava+lan"+0ouble float /ava+lan"+:loat int /ava+lan"+Inte"er lon" /ava+lan"+Bon" short /ava+lan"+9hort void /ava+lan"+3oid
>uestion= 9ead t'e &o##o-in6 +ro6ram8 +0b#ic c#ass test I +0b#ic static 5oid main.(trin6 ST ar6s1 I int " N 3L int y N 1L i& ." N y1 (ystem.o0t.+rint#n.RNot eC0a#R1L e#se (ystem.o0t.+rint#n.R!C0a#R1L J J :'at is t'e res0#tG ). 7'e o0t+0t is 3!C0a#4 B. 7'e o0t+0t in 3Not !C0a#4 *. )n error at R i& ." N y1R ca0ses com+i#ation to &a##. =. 7'e +ro6ram e"ec0tes b0t no o0t+0t is s'o- on conso#e. 8nswer= C >uestion= -'at is t'e c#ass 5ariab#es G 8nswer= :'en -e create a n0mber o& ob/ects o& t'e same c#ass, t'en eac' ob/ect -i## s'are a common co+y o& 5ariab#es. 7'at means t'at t'ere is on#y one co+y +er c#ass, no matter 'o- many ob/ects are created &rom it. *#ass 5ariab#es or static 5ariab#es are dec#ared -it' t'e static <ey-ord in a c#ass, b0t mind it t'at it s'o0#d be dec#ared o0tside o0tside a c#ass. 7'ese 5ariab#es are stored in static memory. *#ass 5ariab#es are most#y 0sed &or constants, 5ariab#e t'at ne5er c'an6e its initia# 5a#0e. (tatic 5ariab#es are a#-ays ca##ed by t'e c#ass name. 7'is 5ariab#e is created -'en t'e +ro6ram starts i.e. it is created be&ore t'e instance is created o& c#ass by 0sin6 ne- o+erator and 6ets destroyed -'en t'e +ro6rams sto+s. 7'e sco+e o& t'e c#ass 5ariab#e is same a instance 5ariab#e. 7'e c#ass 5ariab#e can be de&ined any-'ere at c#ass #e5e# -it' t'e <ey-ord static. %t initia# 5a#0e is same as instance 5ariab#e. :'en t'e c#ass 5ariab#e is de&ined as int t'en itHs initia# 5a#0e is by de&a0#t $ero, -'en dec#ared boo#ean its de&a0#t 5a#0e is &a#se and n0## &or ob/ect re&erences. *#ass 5ariab#es are associated -it' t'e c#ass, rat'er t'an -it' any ob/ect. >uestion= :'at is t'e di&&erence bet-een t'e instanceo& and 6etc#ass, t'ese t-o are same or not G 8nswer= instanceo& is a o+erator, not a &0nction -'i#e 6et*#ass is a met'od o& /a5a.#an6.Ob/ect c#ass. *onsider a condition -'ere -e 0se i&.o.6et*#ass.1.6etName.1.eC0a#s.R/a5a.#an6.;at'R11I J 7'is met'od on#y c'ec<s i& t'e c#assname -e 'a5e +assed is eC0a# to /a5a.#an6.;at'. 7'e c#ass /a5a.#an6.;at' is #oaded by t'e bootstra+ *#assFoader. 7'is c#ass is an abstract c#ass.7'is c#ass #oader is res+onsib#e &or #oadin6 c#asses. !5ery *#ass ob/ect contains a re&erence to t'e *#assFoader t'at de&ines. 6et*#ass.1 met'od ret0rns t'e r0ntime c#ass o& an ob/ect. %t &etc'es t'e /a5a instance o& t'e 6i5en &0##y C0a#i&ied ty+e name. 7'e code -e 'a5e -ritten is not necessary, beca0se -e s'o0#d not com+are 6et*#ass.6etName.1. 7'e reason be'ind it is t'at i& t'e t-o di&&erent c#ass #oaders #oad t'e same c#ass b0t &or t'e @2;, it -i## consider bot' c#asses as di&&erent c#asses so, -e canHt com+are t'eir names. %t can on#y 6i5es t'e im+#ementin6 c#ass b0t canHt com+are a inter&ace, b0t instanceo& o+erator can. 7'e instanceo& o+erator com+ares an ob/ect to a s+eci&ied ty+e. :e can 0se it to test i& an ob/ect is an instance o& a c#ass, an instance o& a s0bc#ass, or an instance o& a c#ass t'at im+#ements a +artic0#ar inter&ace. :e s'o0#d try to 0se instanceo& o+erator in +#ace o& 6et*#ass.1 met'od. 9emember instanceo& o+eator and 6et*#ass are not same. 7ry t'is e"am+#e, it -i## 'e#+ yo0 to better 0nderstand t'e di&&erence bet-een t'e t-o. %nter&ace oneI J *#ass 7-o im+#ements one I J *#ass 7'ree im+#ements one I J +0b#ic c#ass 7est I +0b#ic static 5oid main.(trin6 ar6sST1 I one test1 N ne- 7-o.1L one test2 N ne- 7'ree.1L (ystem.o0t.+rint#n.test1 instanceo& one1L AAtr0e (ystem.o0t.+rint#n.test2 instanceo& one1L AAtr0e (ystem.o0t.+rint#n.7est.6et*#ass.1.eC0a#s.test2.6et*#ass.111L AA&a#se J J