You are on page 1of 28

Core Java Interview Questions with Answers 1.what is a transient variable?

A transient variable is a variable that may not be serialized. 2.which containers use a border Layout as their default layout? The window, Frame and Dialog classes use a border layout as their default layout. 3.Why do threads bloc on I!"?

Threads block on i/o (that is enters the waiting state so that other threads may e!ecute while the i/o "#eration is #erformed. #. $ow are "bserver and "bservable used? "b$ects that subclass the "bservable class maintain a list of observers. %hen an "bservable ob$ect is u#dated it invokes the u#date( method of each of its observers to notify the observers that it has changed state. The "bserver interface is im#lemented by ob$ects that observe "bservable ob$ects. %. What is synchroni&ation and why is it i'(ortant? %ith res#ect to multithreading, synchronization is the ca#ability to control the access of multi#le threads to shared resources. %ithout synchronization, it is #ossible for one thread to modify a shared ob$ect while another thread is in the #rocess of using or u#dating that ob$ect&s value. This often leads to significant errors. ). Can a loc be ac*uired on a class?

'es, a lock can be ac(uired on a class. This lock is ac(uired on the class&s )lass ob$ect. +. What,s new with the sto(-./ sus(end-. and resu'e-. 'ethods in J01 1.2? The sto#( , sus#end( and resume( methods have been de#recated in *D+ ,.-.

2. Is null a

eyword?

The null value is not a keyword. 3. What is the (referred si&e of a co'(onent? The #referred size of a com#onent is the minimum com#onent size that will allow the com#onent to dis#lay normally. 14. What 'ethod is used to s(ecify a container,s layout? The set.ayout( method is used to s#ecify a container&s layout. 11. Which containers use a 5lowLayout as their default layout? The /anel and A##let classes use the Flow.ayout as their default layout. 12. What state does a thread enter when it ter'inates its (rocessin6? %hen a thread terminates its #rocessing, it enters the dead state. 13. What is the Collections A7I? The )ollections A/0 is a set of classes and interfaces that su##ort o#erations on collections of ob$ects. 1#. Which characters 'ay be used as the second character of an identifier, but not as the first character of an identifier1 The digits 2 through 3 may not be used as the first character of an identifier but they may be used after the first character of an identifier. 1%. What is the List interface? The .ist interface #rovides su##ort for ordered collections of ob$ects. 1). $ow does Java handle inte6er overflows and underflows?

0t uses those low order bytes of the result that can fit into the size of the ty#e allowed by the o#eration. 1+. What is the 8ector class? The 4ector class #rovides the ca#ability to im#lement a growable array of ob$ects 12. What 'odifiers 'ay be used with an inner class that is a 'e'ber of an outer class? A (non5local inner class may be declared as #ublic, #rotected, #rivate, static, final, or abstract. 13. What is an Iterator interface? The 0terator interface is used to ste# through the elements of a )ollection. 24. What is the difference between the 99 and 999 o(erators? The 66 o#erator carries the sign bit when shifting right. The 666 zero5fills bits that have been shifted out. 21. Which 'ethod of the Co'(onent class is used to set the (osition and si&e of a co'(onent? set7ounds( 22. $ow 'any bits are used to re(resent :nicode/ A;CII/ :<5= 1)/ and :<5=2 characters? 8nicode re(uires ,9 bits and A:)00 re(uire ; bits. Although the A:)00 character set uses only ; bits, it is usually re#resented as < bits. 8TF5< re#resents characters using <, ,9, and ,< bit #atterns. 8TF5,9 uses ,95bit and larger bit #atterns. 23.What is the difference between yieldin6 and slee(in6? %hen a task invokes its yield( method, it returns to the ready state. %hen a task invokes its slee#( method, it returns to the waiting state.

2#. Which >ava.util classes and interfaces su((ort event handlin6? The =vent"b$ect class and the =vent.istener interface su##ort event #rocessing. 2%. Is si&eof a eyword?

The sizeof o#erator is not a keyword. 2). What are wra((ed classes? %ra##ed classes are classes that allow #rimitive ty#es to be accessed as ob$ects. 2+. 0oes 6arba6e collection 6uarantee that a (ro6ra' will not run out of 'e'ory? >arbage collection does not guarantee that a #rogram will not run out of memory. 0t is #ossible for #rograms to use u# memory resources faster than they are garbage collected. 0t is also #ossible for #rograms to create ob$ects that are not sub$ect to garbage collection 22. What restrictions are (laced on the location of a (ac a6e state'ent within a source code file1 A #ackage statement must a##ear as the first line in a source code file (e!cluding blank lines and comments . 23. Can an ob>ect,s finali&e-. 'ethod be invo ed while it is reachable? An ob$ect&s finalize( method cannot be invoked by the garbage collector while the ob$ect is still reachable. ?owever, an ob$ect&s finalize( method may be invoked by other ob$ects. 34. What is the i''ediate su(erclass of the A((let class? /anel

31. What is the difference between (ree'(tive schedulin6 and ti'e slicin6? 8nder #reem#tive scheduling, the highest #riority task e!ecutes until it enters the waiting or dead states or a higher #riority task comes into e!istence. 8nder time slicing, a task e!ecutes for a #redefined slice of time and then reenters the #ool of ready tasks. The scheduler then determines which task should e!ecute ne!t, based on #riority and other factors. 32. ?a'e three Co'(onent subclasses that su((ort (aintin6. The )anvas, Frame, /anel, and A##let classes su##ort #ainting. 33. What value does readLine-. return when it has reached the end of a file? The read.ine( method returns null when it has reached the end of a file. 3#. What is the i''ediate su(erclass of the 0ialo6 class? %indow 3%. What is cli((in6? )li##ing is the #rocess of confining #aint o#erations to a limited area or sha#e. 3). What is a native 'ethod? A native method is a method that is im#lemented in a language other than *ava. 3+. Can a for state'ent loo( indefinitely? 'es, a for statement can loo# indefinitely. For e!am#le, consider the following@ for(AA A 32. What are order of (recedence and associativity/ and how are they used?

"rder of #recedence determines the order in which o#erators are evaluated in e!#ressions. Associatity determines whether an e!#ression is evaluated left5to5right or right5to5left 33. When a thread bloc s on I!"/ what state does it enter? A thread enters the waiting state when it blocks on 0/". #4. <o what value is a variable of the ;trin6 ty(e auto'atically initiali&ed? The default value of an :tring ty#e is null. #1. What is the catch or declare rule for 'ethod declarations? 0f a checked e!ce#tion may be thrown within the body of a method, the method must either catch the e!ce#tion or declare it in its throws clause. #2. What is the difference between a @enuIte' and a Chec boA@enuIte'? The )heckbo!Benu0tem class e!tends the Benu0tem class to su##ort a menu item that may be checked or unchecked. #3. What is a tas ,s (riority and how is it used in schedulin6? A task&s #riority is an integer value that identifies the relative order in which it should be e!ecuted with res#ect to other tasks. The scheduler attem#ts to schedule higher #riority tasks before lower #riority tasks. ##. What class is the to( of the AW< event hierarchy? The $ava.awt.A%T=vent class is the highest5level class in the A%T event5class hierarchy. #%. When a thread is created and started/ what is its initial state? A thread is in the ready state after it has been created and started. #). Can an anony'ous class be declared as i'(le'entin6 an interface and eAtendin6 a class?

An anonymous class may im#lement an interface or e!tend a su#erclass, but may not be declared to do both. #+. What is the ran6e of the short ty(e? The range of the short ty#e is 5(-C,D to -C,D 5 ,. #2. What is the ran6e of the char ty(e? The range of the char ty#e is 2 to -C,9 5 ,. #3. In which (ac a6e are 'ost of the AW< events that su((ort the event=dele6ation 'odel defined? Bost of the A%T5related events of the event5delegation model are defined in the $ava.awt.event #ackage. The A%T=vent class is defined in the $ava.awt #ackage. %4. What is the i''ediate su(erclass of @enu? Benu0tem %1. What is the (ur(ose of finali&ation? The #ur#ose of finalization is to give an unreachable ob$ect the o##ortunity to #erform any cleanu# #rocessing before the ob$ect is garbage collected. %2. Which class is the i''ediate su(erclass of the @enuCo'(onent class. "b$ect %3. What invo es a thread,s run-. 'ethod? After a thread is started, via its start( method or that of the Thread class, the *4B invokes the thread&s run( method when the thread is initially e!ecuted. %#. What is the difference between the Boolean C o(erator and the CC o(erator?

0f an e!#ression involving the 7oolean E o#erator is evaluated, both o#erands are evaluated. Then the E o#erator is a##lied to the o#erand. %hen an e!#ression involving the EE o#erator is evaluated, the first o#erand is evaluated. 0f the first o#erand returns a value of true then the second o#erand is evaluated. The EE o#erator is then a##lied to the first and second o#erands. 0f the first o#erand evaluates to false, the evaluation of the second o#erand is ski##ed. %%. ?a'e three subclasses of the Co'(onent class. 7o!.Filler, 7utton, )anvas, )heckbo!, )hoice, )ontainer, .abel, .ist, :crollbar, or Te!t)om#onent %). What is the Dre6orianCalendar class? The >regorian)alendar #rovides su##ort for traditional %estern calendars. %+. Which Container 'ethod is used to cause a container to be laid out and redis(layed? validate-. %2. What is the (ur(ose of the Eunti'e class? The #ur#ose of the Funtime class is to #rovide access to the *ava runtime system. %3. $ow 'any ti'es 'ay an ob>ect,s finali&e-. 'ethod be invo ed by the 6arba6e collector? An ob$ect&s finalize( method may only be invoked once by the garbage collector. )4. What is the (ur(ose of the finally clause of a try=catch= finally state'ent? The finally clause is used to #rovide the ca#ability to e!ecute code no matter whether or not an e!ce#tion is thrown or caught. )1. What is the ar6u'ent ty(e of a (ro6ra',s 'ain-. 'ethod?

A #rogram&s main( method takes an argument of the :tringGH ty#e. )2. Which Java o(erator is ri6ht associative? The I o#erator is right associative. )3. What is the Locale class? The .ocale class is used to tailor #rogram out#ut to the conventions of a #articular geogra#hic, #olitical, or cultural region. )#. Can a double value be cast to a byte? 'es, a double value can be cast to a byte. )%. What is the difference between a brea continue state'ent? state'ent and a

A break statement results in the termination of the statement to which it a##lies (switch, for, do, or while . A continue statement is used to end the current loo# iteration and return control to the loo# statement. )). What 'ust a class do to i'(le'ent an interface? 0t must #rovide all of the methods in the interface and identify the interface in its im#lements clause. )+. What 'ethod is invo ed to cause an ob>ect to be6in eAecutin6 as a se(arate thread? The start( method of the Thread class is invoked to cause an ob$ect to begin e!ecuting as a se#arate thread. )2. ?a'e two subclasses of the <eAtCo'(onent class. Te!tField and Te!tArea )3. What is the advanta6e of the event=dele6ation 'odel over the earlier event=inheritance 'odel? The event5delegation model has two advantages over the event5 inheritance model. First, it enables event handling to be handled by ob$ects other than the ones that generate the events (or their containers . This allows a clean se#aration between a com#onent&s

design and its use. The other advantage of the event5delegation model is that it #erforms much better in a##lications where many events are generated. This #erformance im#rovement is due to the fact that the event5delegation model does not have to re#eatedly #rocess unhandled events, as is the case of the event5inheritance model. +4. Which containers 'ay have a @enuBar? Frame +1. $ow are co''as used in the intiali&ation and iteration (arts of a for state'ent? )ommas are used to se#arate multi#le statements within the initialization and iteration #arts of a for statement. +2. What is the (ur(ose of the wait-./ notify-./ and notifyAll-. 'ethods? The wait( ,notify( , and notifyAll( methods are used to #rovide an efficient way for threads to wait for a shared resource. %hen a thread e!ecutes an ob$ect&s wait( method, it enters the waiting state. 0t only enters the ready state after another thread invokes the ob$ect&s notify( or notifyAll( methods. +3. What is an abstract 'ethod? An abstract method is a method whose im#lementation is deferred to a subclass. +#. $ow are Java source code files na'ed? A *ava source code file takes the name of a #ublic class or interface that is defined within the file. A source code file may contain at most one #ublic class or interface. 0f a #ublic class or interface is defined within a source code file, then the source code file must take the name of the #ublic class or interface. 0f no #ublic class or interface is defined within a source code file, then the file must take on a name that is different than its classes and interfaces. :ource code files use the .$ava e!tension. +%. What is the relationshi( between the Canvas class and the Dra(hics class?

A )anvas ob$ect #rovides access to a >ra#hics ob$ect via its #aint( method. +). What are the hi6h=level thread states? The high5level thread states are ready, running, waiting, and dead. ++. What value does read-. return when it has reached the end of a file? The read( method returns 5, when it has reached the end of a file. +2. Can a Byte ob>ect be cast to a double value? Jo, an ob$ect cannot be cast to a #rimitive value. +3. What is the difference between a static and a non=static inner class? A non5static inner class may have ob$ect instances that are associated with instances of the class&s outer class. A static inner class does not have any ob$ect instances. 24. What is the difference between the ;trin6 and ;trin6Buffer classes? :tring ob$ects are constants. :tring7uffer ob$ects are not. 21. If a variable is declared as (rivate/ where 'ay the variable be accessed? A #rivate variable may only be accessed within the class in which it is declared. 22. What is an ob>ect,s loc and which ob>ect,s have loc s?

An ob$ect&s lock is a mechanism that is used by multi#le threads to obtain synchronized access to the ob$ect. A thread may e!ecute a synchronized method of an ob$ect only after it has ac(uired the ob$ect&s lock. All ob$ects and classes have locks. A class&s lock is ac(uired on the class&s )lass ob$ect. 23. What is the 0ictionary class?

The Dictionary class #rovides the ca#ability to store key5value #airs. 2#. $ow are the ele'ents of a BorderLayout or6ani&ed? The elements of a 7order.ayout are organized at the borders (Jorth, :outh, =ast, and %est and the center of a container. 2%. What is the F o(erator? 0t is referred to as the modulo or remainder o#erator. 0t returns the remainder of dividing the first o#erand by the second o#erand. 2). When can an ob>ect reference be cast to an interface reference? An ob$ect reference be cast to an interface reference when the ob$ect im#lements the referenced interface. 2+. What is the difference between a Window and a 5ra'e? The Frame class e!tends %indow to define a main a##lication window that can have a menu bar. 22. Which class is eAtended by all other classes? The "b$ect class is e!tended by all other classes. 23. Can an ob>ect be 6arba6e collected while it is still reachable? A reachable ob$ect cannot be garbage collected. "nly unreachable ob$ects may be garbage collected.. 34. Is the ternary o(erator written A G y ? & or A ? y G & ? 0t is written ! 1 y @ z. 31. What is the difference between the 5ont and 5ont@etrics classes? The FontBetrics class is used to define im#lementation5s#ecific #ro#erties, such as ascent and descent, of a Font ob$ect. 32. $ow is roundin6 (erfor'ed under inte6er division?

The fractional #art of the result is truncated. This is known as rounding toward zero. 33. What ha((ens when a thread cannot ac*uire a loc ob>ect? on an

0f a thread attem#ts to e!ecute a synchronized method or synchronized statement and is unable to ac(uire an ob$ect&s lock, it enters the waiting state until the lock becomes available. 3#. What is the difference between the Eeader!Writer class hierarchy and the In(ut;trea'!"ut(ut;trea' class hierarchy? The Feader/%riter class hierarchy is character5oriented, and the 0n#ut:tream/"ut#ut:tream class hierarchy is byte5oriented. 3%. What classes of eAce(tions 'ay be cau6ht by a catch clause? A catch clause can catch any e!ce#tion that may be assigned to the Throwable ty#e. This includes the =rror and =!ce#tion ty#es. 3). If a class is declared without any access 'odifiers/ where 'ay the class be accessed? A class that is declared without any access modifiers is said to have #ackage access. This means that the class can only be accessed by other classes and interfaces that are defined within the same #ackage. 3+. What is the ;i'(le<i'eHone class? The :im#leTimeKone class #rovides su##ort for a >regorian calendar. 32. What is the @a( interface? The Ba# interface re#laces the *D+ ,., Dictionary class and is used associate keys with values. 33. 0oes a class inherit the constructors of its su(erclass? A class does not inherit constructors from any of its su#erclasses.

144. 5or which state'ents does it 'a e sense to use a label? The only statements for which it makes sense to use a label are those statements that can enclose a break or continue statement. 141. What is the (ur(ose of the ;yste' class? The #ur#ose of the :ystem class is to #rovide access to system resources. 142. Which <eAtCo'(onent 'ethod is used to set a <eAtCo'(onent to the read=only state? set=ditable( 143. $ow are the ele'ents of a CardLayout or6ani&ed? The elements of a )ard.ayout are stacked, one on to# of the other, like a deck of cards. 14#. Is CCI a valid Java o(erator? Jo, it is not. 14%. ?a'e the ei6ht (ri'itive Java ty(es. The eight #rimitive ty#es are byte, char, short, int, long, float, double, and boolean. 14). Which class should you use to obtain desi6n infor'ation about an ob>ect? The )lass class is used to obtain information about an ob$ect&s design. 14+. What is the relationshi( between cli((in6 and re(aintin6? %hen a window is re#ainted by the A%T #ainting thread, it sets the cli##ing regions to the area of the window that re(uires re#ainting. 142. Is JabcJ a (ri'itive value? The :tring literal LabcL is not a #rimitive value. 0t is a :tring ob$ect.

143. What is the relationshi( between an event=listener interface and an event=ada(ter class? An event5listener interface defines the methods that must be im#lemented by an event handler for a #articular kind of event. An event ada#ter #rovides a default im#lementation of an event5listener interface. 114. What restrictions are (laced on the values of each case of a switch state'ent? During com#ilation, the values of each case of a switch statement must evaluate to a value that can be #romoted to an int value. 111. What 'odifiers 'ay be used with an interface declaration? An interface may be declared as #ublic or abstract. 112. Is a class a subclass of itself? A class is a subclass of itself. 113. What is the hi6hest=level event class of the event= dele6ation 'odel? The $ava.util.=vent"b$ect class is the highest5level class in the event5 delegation class hierarchy. 11#. What event results fro' the clic in6 of a button? The Action=vent event is generated as the result of the clicking of a button. 11%. $ow can a D:I co'(onent handle its own events? A com#onent can handle its own events by im#lementing the re(uired event5listener interface and adding itself as its own event listener. 11). What is the difference between a while state'ent and a do state'ent?

A while statement checks at the beginning of a loo# to see whether the ne!t loo# iteration should occur. A do statement checks at the end of a loo# to see whether the ne!t iteration of a loo# should occur. The do statement will always e!ecute the body of a loo# at least once. 11+. $ow are the ele'ents of a DridBa6Layout or6ani&ed? The elements of a >rid7ag.ayout are organized according to a grid. ?owever, the elements are of different sizes and may occu#y more than one row or column of the grid. 0n addition, the rows and columns may have different sizes. 112. What advanta6e do Java,s layout 'ana6ers (rovide over traditional windowin6 syste's? *ava uses layout managers to lay out com#onents in a consistent manner across all windowing #latforms. :ince *ava&s layout managers aren&t tied to absolute sizing and #ositioning, they are able to accomodate #latform5s#ecific differences among windowing systems. 113. What is the Collection interface? The )ollection interface #rovides su##ort for the im#lementation of a mathematical bag 5 an unordered collection of ob$ects that may contain du#licates. 124. What 'odifiers can be used with a local inner class? A local inner class may be final or abstract. 121. What is the difference between static and non=static variables? A static variable is associated with the class as a whole rather than with s#ecific instances of a class. Jon5static variables take on uni(ue values with each ob$ect instance. 122. What is the difference between the (aint-. and re(aint-. 'ethods? The #aint( method su##orts #ainting via a >ra#hics ob$ect. The re#aint( method is used to cause #aint( to be invoked by the A%T #ainting thread.

123. What is the (ur(ose of the 5ile class? The File class is used to create ob$ects that #rovide access to the files and directories of a local file system. 12#. Can an eAce(tion be rethrown? 'es, an e!ce#tion can be rethrown. 12%. Which @ath 'ethod is used to calculate the absolute value of a nu'ber? The abs( method is used to calculate absolute values. 12). $ow does 'ultithreadin6 ta e (lace on a co'(uter with a sin6le C7:? The o#erating system&s task scheduler allocates e!ecution time to multi#le tasks. 7y (uickly switching between e!ecuting tasks, it creates the im#ression that tasks e!ecute se(uentially. 12+. When does the co'(iler su((ly a default constructor for a class? The com#iler su##lies a default constructor for a class if no other constructors are #rovided. 122. When is the finally clause of a try=catch=finally state'ent eAecuted? The finally clause of the try5catch5finally statement is always e!ecuted unless the thread of e!ecution terminates or an e!ce#tion occurs within the e!ecution of the finally clause. 123. Which class is the i''ediate su(erclass of the Container class? )om#onent 134. If a 'ethod is declared as (rotected/ where 'ay the 'ethod be accessed? A #rotected method may only be accessed by classes or interfaces of the same #ackage or by subclasses of the class in which it is declared.

131. $ow can the Chec boA class be used to create a radio button? 7y associating )heckbo! ob$ects with a )heckbo!>rou#. 132. Which non=:nicode letter characters 'ay be used as the first character of an identifier? The non58nicode letter characters M and N may a##ear as the first character of an identifier 133. What restrictions are (laced on 'ethod overloadin6? Two methods may not have the same name and argument list but different return ty#es. 13#. What ha((ens when you invo e a thread,s interru(t 'ethod while it is slee(in6 or waitin6? %hen a task&s interru#t( method is e!ecuted, the task enters the ready state. The ne!t time the task enters the running state, an 0nterru#ted=!ce#tion is thrown. 13%. What is castin6? There are two ty#es of casting, casting between #rimitive numeric ty#es and casting between ob$ect references. )asting between numeric ty#es is used to convert larger values, such as double values, to smaller values, such as byte values. )asting between ob$ect references is used to refer to an ob$ect by a com#atible class, interface, or array ty#e reference. 13). What is the return ty(e of a (ro6ra',s 'ain-. 'ethod? A #rogram&s main( method has a void return ty#e. 13+. ?a'e four Container classes. %indow, Frame, Dialog, FileDialog, /anel, A##let, or :croll/ane

132. What is the difference between a Choice and a List? A )hoice is dis#layed in a com#act form that re(uires you to #ull it down to see the list of available choices. "nly one item may be selected from a )hoice. A .ist may be dis#layed in such a way that several .ist items are visible. A .ist su##orts the selection of one or more .ist items. 133. What class of eAce(tions are 6enerated by the Java run= ti'e syste'? The *ava runtime system generates Funtime=!ce#tion and =rror e!ce#tions. 1#4. What class allows you to read ob>ects directly fro' a strea'? The "b$ect0n#ut:tream class su##orts the reading of ob$ects from in#ut streams. 1#1. What is the difference between a field variable and a local variable? A field variable is a variable that is declared as a member of a class. A local variable is a variable that is declared local to a method. 1#2. :nder what conditions is an ob>ect,s finali&e-. 'ethod invo ed by the 6arba6e collector? The garbage collector invokes an ob$ect&s finalize( method when it detects that the ob$ect has become unreachable. 1#3. $ow are this-. and su(er-. used with constructors? this( is used to invoke a constructor of the same class. su#er( is used to invoke a su#erclass constructor. 1##. What is the relationshi( between a 'ethod,s throws clause and the eAce(tions that can be thrown durin6 the 'ethod,s eAecution?

A method&s throws clause must declare any checked e!ce#tions that are not caught within the body of the method. 1#%. What is the difference between the J01 1.42 event 'odel and the event=dele6ation 'odel introduced with J01 1.1? The *D+ ,.2- event model uses an event inheritance or bubbling a##roach. 0n this model, com#onents are re(uired to handle their own events. 0f they do not handle a #articular event, the event is inherited by (or bubbled u# to the com#onent&s container. The container then either handles the event or it is bubbled u# to its container and so on, until the highest5level container has been tried. 0n the event5delegation model, s#ecific ob$ects are designated as event handlers for >80 com#onents. These ob$ects im#lement event5 listener interfaces. The event5delegation model is more efficient than the event5inheritance model because it eliminates the #rocessing re(uired to su##ort the bubbling of unhandled events. 1#). $ow is it (ossible for two ;trin6 ob>ects with identical values not to be e*ual under the II o(erator? The II o#erator com#ares two ob$ects to determine if they are the same ob$ect in memory. 0t is #ossible for two :tring ob$ects to have the same value, but located indifferent areas of memory. 1#+. Why are the 'ethods of the @ath class static? :o they can be invoked as if they are a mathematical code library. 1#2. What Chec boA 'ethod allows you to tell if a Chec boA is chec ed? 6et;tate-. 1#3. What state is a thread in when it is eAecutin6? An e!ecuting thread is in the running state. 1%4. What are the le6al o(erands of the instanceof o(erator?

The left o#erand is an ob$ect reference or null value and the right o#erand is a class, interface, or array ty#e. 1%1. $ow are the ele'ents of a DridLayout or6ani&ed? The elements of a >rid7ad layout are of e(ual size and are laid out using the s(uares of a grid. 1%2. What an I!" filter? An 0/" filter is an ob$ect that reads from one stream and writes to another, usually altering the data in some way as it is #assed from one stream to another. 1%3. If an ob>ect is 6arba6e collected/ can it beco'e reachable a6ain? "nce an ob$ect is garbage collected, it ceases to e!ist. 0t can no longer become reachable again. 1%#. What is the ;et interface? The :et interface #rovides methods for accessing the elements of a finite mathematical set. :ets do not allow du#licate elements. 1%%. What classes of eAce(tions 'ay be thrown by a throw state'ent? A throw statement may throw any e!#ression that may be assigned to the Throwable ty#e. 1%). What are K and 7I? = is the base of the natural logarithm and /0 is mathematical value #i. 1%+. Are true and false eywords?

The values true and false are not keywords. 1%2. What is a void return ty(e? A void return ty#e indicates that a method does not return a value.

1%3. What is the (ur(ose of the enableKvents-. 'ethod? The enable=vents( method is used to enable an event for a #articular ob$ect. Jormally, an event is enabled when a listener is added to an ob$ect for a #articular event. The enable=vents( method is used by ob$ects that handle events by overriding their event5dis#atch methods. 1)4. What is the difference between the 5ile and Eando'Access5ile classes? The File class enca#sulates the files and directories of the local file system. The FandomAccessFile class #rovides the methods needed to directly access data contained in any #art of a file. 1)1. What ha((ens when you add a double value to a ;trin6? The result is a :tring ob$ect. 1)2. What is your (latfor',s default character encodin6? 0f you are running *ava on =nglish %indows #latforms, it is #robably )#,-D-. 0f you are running *ava on =nglish :olaris #latforms, it is most likely <<D3N,.. 1)3. Which (ac a6e is always i'(orted by default? The $ava.lang #ackage is always im#orted by default. 1)#. What interface 'ust an ob>ect i'(le'ent before it can be written to a strea' as an ob>ect? An ob$ect must im#lement the :erializable or =!ternalizable interface before it can be written to a stream as an ob$ect. 1)%. $ow are this and su(er used? this is used to refer to the current ob$ect instance. su#er is used to refer to the variables and methods of the su#erclass of the current ob$ect instance. 1)). What is the (ur(ose of 6arba6e collection?

The #ur#ose of garbage collection is to identify and discard ob$ects that are no longer needed by a #rogram so that their resources may be reclaimed and reused. 1)+. What is a co'(ilation unit? A com#ilation unit is a *ava source code file. 1)2. What interface is eAtended by AW< event listeners? All A%T event listeners e!tend the $ava.util.=vent.istener interface. 1)3. What restrictions are (laced on 'ethod overridin6? "verridden methods must have the same name, argument list, and return ty#e. The overriding method may not limit the access of the method it overrides. The overriding method may not throw any e!ce#tions that may not be thrown by the overridden method. 1+4. $ow can a dead thread be restarted? A dead thread cannot be restarted. 1+1. What ha((ens if an eAce(tion is not cau6ht? An uncaught e!ce#tion results in the uncaught=!ce#tion( method of the thread&s Thread>rou# being invoked, which eventually results in the termination of the #rogram in which it is thrown. 1+2. What is a layout 'ana6er? A layout manager is an ob$ect that is used to organize com#onents in a container. 1+3. Which arith'etic o(erations can result in the throwin6 of an Arith'eticKAce(tion? 0nteger / and O can result in the throwing of an Arithmetic=!ce#tion. 1+#. What are three ways in which a thread can enter the waitin6 state? A thread can enter the waiting state by invoking its slee#( method, by blocking on 0/", by unsuccessfully attem#ting to ac(uire an ob$ect&s

lock, or by invoking an ob$ect&s wait( method. 0t can also enter the waiting state by invoking its (de#recated sus#end( method. 1+%. Can an abstract class be final? An abstract class may not be declared as final. 1+). What is the EesourceBundle class? The Fesource7undle class is used to store locale5s#ecific resources that can be loaded by a #rogram to tailor the #rogram&s a##earance to the #articular locale in which it is being run. 1++. What ha((ens if a try=catch=finally state'ent does not have a catch clause to handle an e!ce#tion that is thrown within the body of the try statement1 The e!ce#tion #ro#agates u# to the ne!t higher level try5catch statement (if any or results in the #rogram&s termination. 1+2. What is nu'eric (ro'otion? Jumeric #romotion is the conversion of a smaller numeric ty#e to a larger numeric ty#e, so that integer and floating5#oint o#erations may take #lace. 0n numerical #romotion, byte, char, and short values are converted to int values. The int values are also converted to long values, if necessary. The long and float values are converted to double values, as re(uired. 1+3. What is the difference between a ;crollbar and a ;croll7ane? A :crollbar is a )om#onent, but not a )ontainer. A :croll/ane is a )ontainer. A :croll/ane handles its own events and #erforms its own scrolling. 124. What is the difference between a (ublic and a non=(ublic class? A #ublic class may be accessed outside of its #ackage. A non5#ublic class may not be accessed outside of its #ackage.

121. <o what value is a variable of the boolean ty(e auto'atically initiali&ed? The default value of the boolean ty#e is false. 122. Can try state'ents be nested? Try statements may be tested. 123. What is the difference between the (refiA and (ostfiA for's of the LL o(erator? The #refi! form #erforms the increment o#eration and returns the value of the increment o#eration. The #ostfi! form returns the current value all of the e!#ression and then #erforms the increment o#eration on that value. 12#. What is the (ur(ose of a state'ent bloc ? A statement block is used to organize a se(uence of statements as a single statement grou#. 12%. What is a Java (ac a6e and how is it used? A *ava #ackage is a naming conte!t for classes and interfaces. A #ackage is used to create a se#arate name s#ace for grou#s of classes and interfaces. /ackages are also used to organize related classes and interfaces into a single A/0 unit and to control accessibility to these classes and interfaces. 12). What 'odifiers 'ay be used with a to(=level class? A to#5level class may be #ublic, abstract, or final. 12+. What are the "b>ect and Class classes used for? The "b$ect class is the highest5level class in the *ava class hierarchy. The )lass class is used to re#resent the classes and interfaces that are loaded by a *ava #rogram. 122. $ow does a try state'ent deter'ine which catch clause should be used

to handle an eAce(tion? %hen an e!ce#tion is thrown within the body of a try statement, the catch clauses of the try statement are e!amined in the order in which they a##ear. The first catch clause that is ca#able of handling the e!ce#tion is e!ecuted. The remaining catch clauses are ignored. 123. Can an unreachable ob>ect beco'e reachable a6ain? An unreachable ob$ect may become reachable again. This can ha##en when the ob$ect&s finalize( method is invoked and the ob$ect #erforms an o#eration which causes it to become accessible to reachable ob$ects. 134. When is an ob>ect sub>ect to 6arba6e collection? An ob$ect is sub$ect to garbage collection when it becomes unreachable to the #rogram in which it is used. 131. What 'ethod 'ust be i'(le'ented by all threads? All tasks must im#lement the run( method, whether they are a subclass of Thread or im#lement the Funnable interface. 132. What 'ethods are used to 6et and set the teAt label dis(layed by a Button ob>ect? 6etLabel-. and setLabel-. 133. Which Co'(onent subclass is used for drawin6 and (aintin6? )anvas 13#. What are synchroni&ed 'ethods and synchroni&ed state'ents? :ynchronized methods are methods that are used to control access to an ob$ect. A thread only e!ecutes a synchronized method after it has ac(uired the lock for the method&s ob$ect or class. :ynchronized statements are similar to synchronized methods. A synchronized statement can only be e!ecuted after a thread has ac(uired the lock for the ob$ect or class referenced in the synchronized statement.

13%. What are the two basic ways in which classes that can be run as threads 'ay be defined? A thread class may be declared as a subclass of Thread, or it may im#lement the Funnable interface. 13). What are the (roble's faced by Java (ro6ra''ers who don,t use layout 'ana6ers? %ithout layout managers, *ava #rogrammers are faced with determining how their >80 will be dis#layed across multi#le windowing systems and finding a common sizing and #ositioning that will work within the constraints im#osed by each windowing system. 13+. What is the difference between an if state'ent and a switch state'ent? The if statement is used to select among two alternatives. 0t uses a boolean e!#ression to decide which alternative should be e!ecuted. The switch statement is used to select among multi#le alternatives. 0t uses an int e!#ression to determine which alternative should be e!ecuted. 132. What ha((ens when you add a double value to a ;trin6? The result is a :tring ob$ect. 133. What is the List interface? The .ist interface #rovides su##ort for ordered collections of ob$ects.

You might also like