Sei sulla pagina 1di 26

Java interview questions and answers

1. What is garbage collection? What is the process that is responsible for doing that in java? - Reclaiming the unused memory by the invalid objects. Garbage collector is responsible for this process 2. What kind of thread is the Garbage collector thread? - It is a daemon thread. 3. What is a daemon thread? - These are the threads hich can run ithout user intervention. The !"# can e$it hen there are daemon thread by %illing them abruptly. &. How will you invoke any external process in Java? Runtime.getRuntime'(.e$ec').( *. What is the finali e method do? - +efore the invalid objects get garbage collected, the !"# give the user a chance to clean up some resources before it got garbage collected. -. What is mutable object and immutable object? - If a object value is changeable then e can call it as #utable object. '.$., /tring+uffer, )( If you are not allo ed to change the value of an object, it is immutable object. '.$., /tring, Integer, 0loat, )( 1. What is the basic difference between string and stringbuffer object? - /tring is an immutable object. /tring+uffer is a mutable object. 2. What is the purpose of !oid class? - The "oid class is an uninstantiable placeholder class to hold a reference to the 3lass object representing the primitive !ava type void. 4. What is reflection? - Reflection allo s programmatic access to information about the fields, methods and constructors of loaded classes, and the use reflected fields, methods, and constructors to operate on their underlying counterparts on objects, ithin security restrictions. 15. What is the base class for "rror and "xception? - Thro able 11. What is the byte range? -122 to 121 12. What is the implementation of destroy method in java## is it native or java code? - This method is not implemented. 13. What is a package? - To group set of classes into a single unit is %no n as pac%aging. 6ac%ages provides ide namespace ability. 1&. What are the approaches that you will follow for making a program very efficient? - +y avoiding too much of static methods avoiding the e$cessive and unnecessary use of synchroni7ed methods /election of related classes based on the application 'meaning synchroni7ed classes for multiuser and nonsynchroni7ed classes for single user( 8sage of appropriate design patterns 8sing cache methodologies for remote invocations 9voiding creation of variables ithin a loop and lot more. 1*. What is a $atabase%eta$ata? - 3omprehensive information about the database as a hole. 1-. What is &ocale? - 9 :ocale object represents a specific geographical, political, or cultural region 11. How will you load a specific locale? - 8sing Resource+undle.get+undle')(; .Technicalsymposium.com

12. What is J'( and its use? - Really, just a very fast compiler) In this incarnation, pretty much a one-pass compiler < no offline computations. /o you can=t loo% at the hole method, ran% the e$pressions according to hich ones are re-used the most, and then generate code. In theory terms, it=s an on-line problem. 14. 's J!% a compiler or an interpreter? - Interpreter 25. When you think about optimi ation) what is the best way to findout the time*memory consuming process? - 8sing profiler 21. What is the purpose of assert keyword used in J$+,#-#x? - In order to validate certain e$pressions. It effectively replaces the if bloc% and automatically thro s the 9ssertion.rror on failure. This %ey ord should be used for the critical arguments. #eaning, ithout that the method does nothing. 22. How will you get the platform dependent values like line separator) path separator) etc#) ? - 8sing /ytem.get6roperty')( 'line.separator, path.separator, )( 23. What is skeleton and stub? what is the purpose of those? - /tub is a client side representation of the server, hich ta%es care of communicating ith the remote server. /%eleton is the server side representation. +ut that is no more in use) it is deprecated long before in !>?. 2&. What is the final keyword denotes? - final %ey ord denotes that it is the final implementation for that method or variable or class. @ou can=t override that methodAvariableAclass any more. 2*. What is the significance of &ist'terator? - @ou can iterate bac% and forth. 2-. What is the major difference between &inked&ist and .rray&ist? :in%ed:ist are meant for seBuential accessing. 9rray:ist are meant for random accessing. 21. What is nested class? - If all the methods of a inner class is static then it is a nested class. 22. What is inner class? - If the methods of the inner class can only be accessed via the instance of the inner class, then it is called inner class. 24. What is composition? - Colding the reference of the other class ithin some other class is %no n as composition. 35. What is aggregation? - It is a special type of composition. If you e$pose all the methods of a composite class and route the method call to the composite method through its reference, then it is called aggregation. 31. What are the methods in /bject? - clone, eBuals, ait, finali7e, get3lass, hash3ode, notify, notify9ll, to/tring 32. 0an you instantiate the %ath class? - @ou can=t instantiate the math class. 9ll the methods in this class are static. 9nd the constructor is not public. 33. What is singleton? - It is one of the design pattern. This falls in the creational pattern of the design pattern. There ill be only one instance for that entire !"#. @ou can achieve this by having the private constructor in the class. 0or eg., public class /ingleton D private static final /ingleton s E ne /ingleton'(; private /ingleton'( D F public static /ingleton getInstance'( D return s; F AA all non static methods ) F 3&. What is $river%anager? - The basic service to manage set of !>+3 drivers.

.Technicalsymposium.com

3*. What is 0lass#for1ame23 does and how it is useful? - It loads the class into the 3lass:oader. It returns the 3lass. 8sing that you can get the instance ' GclassinstanceH.ne Instance'( (. 3-. 'nq adds a BuestionI .$pain the reason for each %ey ord of public static void main'/tring argsJK(

What is 0ollection .4' ? The 3ollection 96I is a set of classes and interfaces that support operation on collections of objects. These classes and interfaces are more fle$ible, more po erful, and more regular than the vectors, arrays, and hashtables if effectively replaces. .$ample of classesI Cash/et, Cash#ap, 9rray:ist, :in%ed:ist, Tree/et and Tree#ap. .$ample of interfacesI 3ollection, /et, :ist and #ap. 's 'terator a 0lass or 'nterface? What is its use? 9ns erI Iterator is an interface hich is used to step through the elements of a 3ollection. What is similarities*difference between an .bstract class and 'nterface? >ifferences are as follo sI Interfaces provide a form of multiple inheritance. 9 class can e$tend only one other class. Interfaces are limited to public methods and constants ith no implementation. 9bstract classes can have a partial implementation, protected parts, static methods, etc. 9 3lass may implement several interfaces. +ut in case of abstract class, a class may e$tend only one abstract class. Interfaces are slo as it reBuires e$tra indirection to to find corresponding method in in the actual class. 9bstract classes are fast. /imilaritiesI Leither 9bstract classes or Interface can be instantiated. Java 'nterview 5uestions 6 How to define an .bstract class? 9 class containing abstract method is called 9bstract class. 9n 9bstract class canMt be instantiated. .$ample of 9bstract classI abstract class test9bstract3lass D protected /tring my/tring; public /tring get#y/tring'( D return my/tring; F public abstract string any9bstract0unction'(; F How to define an 'nterface in Java ? In !ava Interface defines the methods but does not implement them. Interface can include .Technicalsymposium.com

constants. 9 class that implements the interfaces is bound to implement all the methods defined in Interface. .maple of InterfaceI public interface sampleInterface D public void functionNne'(; public long 3NL/T9LTONL. E 1555; F 'f a class is located in a package) what do you need to change in the /7 environment to be able to use it? @ou need to add a directory or a jar file that contains the pac%age directories to the 3:9//69TC environment variable. :etMs say a class .mployee belongs to a pac%age com.$y7.hr; and is located in the file cIPdevPcomP$y7PhrP.mployee.java. In this case, youMd need to add cIPdev to the variable 3:9//69TC. If this class contains the method main'(, you could test it from a command prompt indo as follo sI cIPQjava com.$y7.hr..mployee How many methods in the 7eriali able interface? There is no method in the /eriali7able interface. The /eriali7able interface acts as a mar%er, telling the object seriali7ation tools that your class is seriali7able. How many methods in the "xternali able interface? There are t o methods in the .$ternali7able interface. @ou have to implement these t o methods in order to ma%e your class e$ternali7able. These t o methods are read.$ternal'( and rite.$ternal'(. What is the difference between 7eriali alble and "xternali able interface? Rhen you use /eriali7able interface, your class is seriali7ed automatically by default. +ut you can override riteNbject'( and readNbject'( t o methods to control more comple$ object seraili7ation process. Rhen you use .$ternali7able interface, you have a complete control over your classMs seriali7ation process. What is a transient variable in Java? 9 transient variable is a variable that may not be seriali7ed. If you donMt ant some field to be seriali7ed, you can mar% that field transient or static. Which containers use a border layout as their default layout? The Rindo , 0rame and >ialog classes use a border layout as their default layout. How are /bserver and /bservable used? Nbjects that subclass the Nbservable class maintain a list of observers. Rhen an Nbservable object is updated, it invo%es the update'( method of each of its observers to

.Technicalsymposium.com

notify the observers that it has changed state. The Nbserver interface is implemented by objects that observe Nbservable objects.

,#what is a transient variable? 9 transient variable is a variable that may not be seriali7ed. 8#which containers use a border &ayout as their default layout? The indo , 0rame and >ialog classes use a border layout as their default layout. 9#Why do threads block on '*/? Threads bloc% on iAo 'that is enters the aiting state( so that other threads may e$ecute hile the iAo Nperation is performed. -# How are /bserver and /bservable used? Nbjects that subclass the Nbservable class maintain a list of observers. Rhen an Nbservable object is updated it invo%es the update'( method of each of its observers to notify the observers that it has changed state. The Nbserver interface is implemented by objects that observe Nbservable objects. :# What is synchroni ation and why is it important? Rith respect to multithreading, synchroni7ation is the capability to control the access of multiple threads to shared resources. Rithout synchroni7ation, it is possible for one thread to modify a shared object hile another thread is in the process of using or updating that objectMs value. This often leads to significant errors. ;# 0an a lock be acquired on a class? @es, a loc% can be acBuired on a class. This loc% is acBuired on the classMs 3lass object. <# What=s new with the stop23) suspend23 and resume23 methods in J$+ ,#8? The stop'(, suspend'( and resume'( methods have been deprecated in !>? 1.2. ># 's null a keyword? The null value is not a %ey ord.

.Technicalsymposium.com

?# What is the preferred si e of a component? The preferred si7e of a component is the minimum component si7e that ill allo the component to display normally. ,@# What method is used to specify a container=s layout? The set:ayout'( method is used to specify a containerMs layout. ,,# Which containers use a Alow&ayout as their default layout? The 6anel and 9pplet classes use the 0lo :ayout as their default layout. ,8# What state does a thread enter when it terminates its processing? Rhen a thread terminates its processing, it enters the dead state. ,9# What is the 0ollections .4'? The 3ollections 96I is a set of classes and interfaces that support operations on collections of objects. ,-# Which characters may be used as the second character of an identifier) but not as the first character of an identifier? The digits 5 through 4 may not be used as the first character of an identifier but they may be used after the first character of an identifier. ,:# What is the &ist interface? The :ist interface provides support for ordered collections of objects. ,;# How does Java handle integer overflows and underflows? It uses those lo order bytes of the result that can fit into the si7e of the type allo ed by the operation. ,<# What is the !ector class? The "ector class provides the capability to implement a gro able array of objects ,># What modifiers may be used with an inner class that is a member of an outer class? 9 'non-local( inner class may be declared as public, protected, private, static, final, or abstract. .Technicalsymposium.com

,?# What is an 'terator interface? The Iterator interface is used to step through the elements of a 3ollection. 8@# What is the difference between the BB and BBB operators? The QQ operator carries the sign bit hen shifting right. The QQQ 7ero-fills bits that have been shifted out. 8,# Which method of the 0omponent class is used to set the position and si e of a component? set+ounds'( 88# How many bits are used to represent Cnicode) .70'') C(A6,;) and C(A6> characters? 8nicode reBuires 1- bits and 9/3II reBuire 1 bits. 9lthough the 9/3II character set uses only 1 bits, it is usually represented as 2 bits. 8T0-2 represents characters using 2, 1-, and 12 bit patterns. 8T0-1- uses 1--bit and larger bit patterns. 89What is the difference between yielding and sleeping? Rhen a tas% invo%es its yield'( method, it returns to the ready state. Rhen a tas% invo%es its sleep'( method, it returns to the aiting state. 8-# Which java#util classes and interfaces support event handling? The .ventNbject class and the .vent:istener interface support event processing. 8:# 's si eof a keyword? The si7eof operator is not a %ey ord. 8;# What are wrapped classes? Rrapped classes are classes that allo primitive types to be accessed as objects. 8<# $oes garbage collection guarantee that a program will not run out of memory? Garbage collection does not guarantee that a program ill not run out of memory. It is possible for programs to use up memory resources faster than they are garbage collected. It is also possible for programs to create objects that are not subject to garbage collection 8># What restrictions are placed on the location of a package statement within a source code file? .Technicalsymposium.com

9 pac%age statement must appear as the first line in a source code file 'e$cluding blan% lines and comments(. 8?# 0an an object=s finali e23 method be invoked while it is reachable? 9n objectMs finali7e'( method cannot be invo%ed by the garbage collector hile the object is still reachable. Co ever, an objectMs finali7e'( method may be invo%ed by other objects. 9@# What is the immediate superclass of the .pplet class? 6anel 9,# What is the difference between preemptive scheduling and time slicing? 8nder preemptive scheduling, the highest priority tas% e$ecutes until it enters the aiting or dead states or a higher priority tas% comes into e$istence. 8nder time slicing, a tas% e$ecutes for a predefined slice of time and then reenters the pool of ready tas%s. The scheduler then determines hich tas% should e$ecute ne$t, based on priority and other factors. 98# 1ame three 0omponent subclasses that support painting# The 3anvas, 0rame, 6anel, and 9pplet classes support painting. 99# What value does read&ine23 return when it has reached the end of a file? The read:ine'( method returns null hen it has reached the end of a file. 9-# What is the immediate superclass of the $ialog class? Rindo 9:# What is clipping? 3lipping is the process of confining paint operations to a limited area or shape. 9;# What is a native method? 9 native method is a method that is implemented in a language other than !ava. 9<# 0an a for statement loop indefinitely? @es, a for statement can loop indefinitely. 0or e$ample, consider the follo ingI for';;( ; 9># What are order of precedence and associativity) and how are they used? .Technicalsymposium.com

Nrder of precedence determines the order in hich operators are evaluated in e$pressions. 9ssociatity determines hether an e$pression is evaluated left-to-right or right-to-left 9?# When a thread blocks on '*/) what state does it enter? 9 thread enters the aiting state hen it bloc%s on IAN. -@# (o what value is a variable of the 7tring type automatically initiali ed? The default value of an /tring type is null. -,# What is the catch or declare rule for method declarations? If a chec%ed e$ception may be thro n ithin the body of a method, the method must either catch the e$ception or declare it in its thro s clause. -8# What is the difference between a %enu'tem and a 0heckbox%enu'tem? The 3hec%bo$#enuItem class e$tends the #enuItem class to support a menu item that may be chec%ed or unchec%ed. -9# What is a task=s priority and how is it used in scheduling? 9 tas%Ms priority is an integer value that identifies the relative order in hich it should be e$ecuted ith respect to other tas%s. The scheduler attempts to schedule higher priority tas%s before lo er priority tas%s. --# What class is the top of the .W( event hierarchy? The java.a t.9RT.vent class is the highest-level class in the 9RT event-class hierarchy. -:# When a thread is created and started) what is its initial state? 9 thread is in the ready state after it has been created and started. -;# 0an an anonymous class be declared as implementing an interface and extending a class? 9n anonymous class may implement an interface or e$tend a superclass, but may not be declared to do both. -<# What is the range of the short type? The range of the short type is -'2S1*( to 2S1* - 1. .Technicalsymposium.com

-># What is the range of the char type? The range of the char type is 5 to 2S1- - 1. -?# 'n which package are most of the .W( events that support the event6delegation model defined? #ost of the 9RT-related events of the event-delegation model are defined in the java.a t.event pac%age. The 9RT.vent class is defined in the java.a t pac%age. :@# What is the immediate superclass of %enu? #enuItem :,# What is the purpose of finali ation? The purpose of finali7ation is to give an unreachable object the opportunity to perform any cleanup processing before the object is garbage collected. :8# Which class is the immediate superclass of the %enu0omponent class# Nbject :9# What invokes a thread=s run23 method? 9fter a thread is started, via its start'( method or that of the Thread class, the !"# invo%es the threadMs run'( method hen the thread is initially e$ecuted. :-# What is the difference between the Doolean E operator and the EE operator? If an e$pression involving the +oolean T operator is evaluated, both operands are evaluated. Then the T operator is applied to the operand. Rhen an e$pression involving the TT operator is evaluated, the first operand is evaluated. If the first operand returns a value of true then the second operand is evaluated. The TT operator is then applied to the first and second operands. If the first operand evaluates to false, the evaluation of the second operand is s%ipped. ::# 1ame three subclasses of the 0omponent class# +o$.0iller, +utton, 3anvas, 3hec%bo$, 3hoice, 3ontainer, :abel, :ist, /crollbar, or Te$t3omponent :;# What is the Gregorian0alendar class? The Gregorian3alendar provides support for traditional Restern calendars.

.Technicalsymposium.com

:<# Which 0ontainer method is used to cause a container to be laid out and redisplayed? validate'( :># What is the purpose of the Funtime class? The purpose of the Runtime class is to provide access to the !ava runtime system. :?# How many times may an object=s finali e23 method be invoked by the garbage collector? 9n objectMs finali7e'( method may only be invo%ed once by the garbage collector. ;@# What is the purpose of the finally clause of a try6catch6finally statement? The finally clause is used to provide the capability to e$ecute code no matter hether or not an e$ception is thro n or caught. ;,# What is the argument type of a program=s main23 method? 9 programMs main'( method ta%es an argument of the /tringJK type. ;8# Which Java operator is right associative? The E operator is right associative. ;9# What is the &ocale class? The :ocale class is used to tailor program output to the conventions of a particular geographic, political, or cultural region. ;-# 0an a double value be cast to a byte? @es, a double value can be cast to a byte. ;:# What is the difference between a break statement and a continue statement? 9 brea% statement results in the termination of the statement to hich it applies 's itch, for, do, or hile(. 9 continue statement is used to end the current loop iteration and return control to the loop statement. ;;# What must a class do to implement an interface? It must provide all of the methods in the interface and identify the interface in its implements clause. .Technicalsymposium.com

;<# What method is invoked to cause an object to begin executing as a separate thread? The start'( method of the Thread class is invo%ed to cause an object to begin e$ecuting as a separate thread. ;># 1ame two subclasses of the (ext0omponent class# Te$t0ield and Te$t9rea ;?# What is the advantage of the event6delegation model over the earlier event6 inheritance model? The event-delegation model has t o advantages over the event-inheritance model. 0irst, it enables event handling to be handled by objects other than the ones that generate the events 'or their containers(. This allo s a clean separation bet een a componentMs design and its use. The other advantage of the event-delegation model is that it performs much better in applications here many events are generated. This performance improvement is due to the fact that the event-delegation model does not have to repeatedly process unhandled events, as is the case of the event-inheritance model. <@# Which containers may have a %enuDar? 0rame <,# How are commas used in the intiali ation and iteration parts of a for statement? 3ommas are used to separate multiple statements ithin the initiali7ation and iteration parts of a for statement. <8# What is the purpose of the wait23) notify23) and notify.ll23 methods? The ait'(,notify'(, and notify9ll'( methods are used to provide an efficient ay for threads to ait for a shared resource. Rhen a thread e$ecutes an objectMs ait'( method, it enters the aiting state. It only enters the ready state after another thread invo%es the objectMs notify'( or notify9ll'( methods. <9# What is an abstract method? 9n abstract method is a method hose implementation is deferred to a subclass. <-# How are Java source code files named? 9 !ava source code file ta%es the name of a public class or interface that is defined ithin the file. 9 source code file may contain at most one public class or interface. If a public class or interface is defined ithin a source code file, then the source code file must ta%e .Technicalsymposium.com

the name of the public class or interface. If no public class or interface is defined ithin a source code file, then the file must ta%e on a name that is different than its classes and interfaces. /ource code files use the .java e$tension. <:# What is the relationship between the 0anvas class and the Graphics class? 9 3anvas object provides access to a Graphics object via its paint'( method. <;# What are the high6level thread states? The high-level thread states are ready, running, aiting, and dead. <<# What value does read23 return when it has reached the end of a file? The read'( method returns -1 hen it has reached the end of a file. <># 0an a Dyte object be cast to a double value? Lo, an object cannot be cast to a primitive value. <?# What is the difference between a static and a non6static inner class? 9 non-static inner class may have object instances that are associated ith instances of the classMs outer class. 9 static inner class does not have any object instances. >@# What is the difference between the 7tring and 7tringDuffer classes? /tring objects are constants. /tring+uffer objects are not. >,# 'f a variable is declared as private) where may the variable be accessed? 9 private variable may only be accessed ithin the class in hich it is declared. >8# What is an object=s lock and which object=s have locks? 9n objectMs loc% is a mechanism that is used by multiple threads to obtain synchroni7ed access to the object. 9 thread may e$ecute a synchroni7ed method of an object only after it has acBuired the objectMs loc%. 9ll objects and classes have loc%s. 9 classMs loc% is acBuired on the classMs 3lass object. >9# What is the $ictionary class? The >ictionary class provides the capability to store %ey-value pairs. >-# How are the elements of a Dorder&ayout organi ed?

.Technicalsymposium.com

The elements of a +order:ayout are organi7ed at the borders 'Lorth, /outh, .ast, and Rest( and the center of a container. >:# What is the G operator? It is referred to as the modulo or remainder operator. It returns the remainder of dividing the first operand by the second operand. >;# When can an object reference be cast to an interface reference? 9n object reference be cast to an interface reference hen the object implements the referenced interface. ><# What is the difference between a Window and a Arame? The 0rame class e$tends Rindo to define a main application indo that can have a menu bar. >># Which class is extended by all other classes? The Nbject class is e$tended by all other classes. >?# 0an an object be garbage collected while it is still reachable? 9 reachable object cannot be garbage collected. Nnly unreachable objects may be garbage collected.. ?@# 's the ternary operator written x H y ? or x ? y H ? It is ritten $ U y I 7. ?,# What is the difference between the Aont and Aont%etrics classes? The 0ont#etrics class is used to define implementation-specific properties, such as ascent and descent, of a 0ont object. ?8# How is rounding performed under integer division? The fractional part of the result is truncated. This is %no n as rounding to ard 7ero. ?9# What happens when a thread cannot acquire a lock on an object? If a thread attempts to e$ecute a synchroni7ed method or synchroni7ed statement and is unable to acBuire an objectMs loc%, it enters the aiting state until the loc% becomes available.

.Technicalsymposium.com

?-# What is the difference between the Feader*Writer class hierarchy and the 'nput7tream*/utput7tream class hierarchy? The ReaderARriter class hierarchy is character-oriented, and the Input/treamANutput/tream class hierarchy is byte-oriented. ?:# What classes of exceptions may be caught by a catch clause? 9 catch clause can catch any e$ception that may be assigned to the Thro able type. This includes the .rror and .$ception types. ?;# 'f a class is declared without any access modifiers) where may the class be accessed? 9 class that is declared ithout any access modifiers is said to have pac%age access. This means that the class can only be accessed by other classes and interfaces that are defined ithin the same pac%age. ?<# What is the 7imple(imeIone class? The /impleTimeVone class provides support for a Gregorian calendar. ?># What is the %ap interface? The #ap interface replaces the !>? 1.1 >ictionary class and is used associate %eys ith values. ??# $oes a class inherit the constructors of its superclass? 9 class does not inherit constructors from any of its superclasses. ,@@# Aor which statements does it make sense to use a label? The only statements for hich it ma%es sense to use a label are those statements that can enclose a brea% or continue statement. ,@,# What is the purpose of the 7ystem class? The purpose of the /ystem class is to provide access to system resources. ,@8# Which (ext0omponent method is used to set a (ext0omponent to the read6 only state? set.ditable'( ,@9# How are the elements of a 0ard&ayout organi ed? .Technicalsymposium.com

The elements of a 3ard:ayout are stac%ed, one on top of the other, li%e a dec% of cards. ,@-# 's EEJ a valid Java operator? Lo, it is not. ,@:# 1ame the eight primitive Java types# The eight primitive types are byte, char, short, int, long, float, double, and boolean. ,@;# Which class should you use to obtain design information about an object? The 3lass class is used to obtain information about an objectMs design. ,@<# What is the relationship between clipping and repainting? Rhen a indo is repainted by the 9RT painting thread, it sets the clipping regions to the area of the indo that reBuires repainting. ,@># 's KabcK a primitive value? The /tring literal WabcW is not a primitive value. It is a /tring object. ,@?# What is the relationship between an event6listener interface and an event6 adapter class? 9n event-listener interface defines the methods that must be implemented by an event handler for a particular %ind of event. 9n event adapter provides a default implementation of an event-listener interface. ,,@# What restrictions are placed on the values of each case of a switch statement? >uring compilation, the values of each case of a s itch statement must evaluate to a value that can be promoted to an int value. ,,,# What modifiers may be used with an interface declaration? 9n interface may be declared as public or abstract. ,,8# 's a class a subclass of itself? 9 class is a subclass of itself. ,,9# What is the highest6level event class of the event6delegation model?

.Technicalsymposium.com

The java.util..ventNbject class is the highest-level class in the event-delegation class hierarchy. ,,-# What event results from the clicking of a button? The 9ction.vent event is generated as the result of the clic%ing of a button. ,,:# How can a GC' component handle its own events? 9 component can handle its o n events by implementing the reBuired event-listener interface and adding itself as its o n event listener. ,,;# What is the difference between a while statement and a do statement? 9 hile statement chec%s at the beginning of a loop to see hether the ne$t loop iteration should occur. 9 do statement chec%s at the end of a loop to see hether the ne$t iteration of a loop should occur. The do statement ill al ays e$ecute the body of a loop at least once. ,,<# How are the elements of a GridDag&ayout organi ed? The elements of a Grid+ag:ayout are organi7ed according to a grid. Co ever, the elements are of different si7es and may occupy more than one ro or column of the grid. In addition, the ro s and columns may have different si7es. ,,># What advantage do Java=s layout managers provide over traditional windowing systems? !ava uses layout managers to lay out components in a consistent manner across all indo ing platforms. /ince !avaMs layout managers arenMt tied to absolute si7ing and positioning, they are able to accomodate platform-specific differences among indo ing systems. ,,?# What is the 0ollection interface? The 3ollection interface provides support for the implementation of a mathematical bag an unordered collection of objects that may contain duplicates. ,8@# What modifiers can be used with a local inner class? 9 local inner class may be final or abstract. ,8,# What is the difference between static and non6static variables? 9 static variable is associated ith the class as a hole rather than ith specific instances of a class. Lon-static variables ta%e on uniBue values ith each object instance. .Technicalsymposium.com

,88# What is the difference between the paint23 and repaint23 methods? The paint'( method supports painting via a Graphics object. The repaint'( method is used to cause paint'( to be invo%ed by the 9RT painting thread. ,89# What is the purpose of the Aile class? The 0ile class is used to create objects that provide access to the files and directories of a local file system. ,8-# 0an an exception be rethrown? @es, an e$ception can be rethro n. ,8:# Which %ath method is used to calculate the absolute value of a number? The abs'( method is used to calculate absolute values. ,8;# How does multithreading take place on a computer with a single 04C? The operating systemMs tas% scheduler allocates e$ecution time to multiple tas%s. +y Buic%ly s itching bet een e$ecuting tas%s, it creates the impression that tas%s e$ecute seBuentially. ,8<# When does the compiler supply a default constructor for a class? The compiler supplies a default constructor for a class if no other constructors are provided. ,8># When is the finally clause of a try6catch6finally statement executed? The finally clause of the try-catch-finally statement is al ays e$ecuted unless the thread of e$ecution terminates or an e$ception occurs ithin the e$ecution of the finally clause. ,8?# Which class is the immediate superclass of the 0ontainer class? 3omponent ,9@# 'f a method is declared as protected) where may the method be accessed? 9 protected method may only be accessed by classes or interfaces of the same pac%age or by subclasses of the class in hich it is declared. ,9,# How can the 0heckbox class be used to create a radio button? +y associating 3hec%bo$ objects ith a 3hec%bo$Group. .Technicalsymposium.com

,98# Which non6Cnicode letter characters may be used as the first character of an identifier? The non-8nicode letter characters X and O may appear as the first character of an identifier ,99# What restrictions are placed on method overloading? T o methods may not have the same name and argument list but different return types. ,9-# What happens when you invoke a thread=s interrupt method while it is sleeping or waiting? Rhen a tas%Ms interrupt'( method is e$ecuted, the tas% enters the ready state. The ne$t time the tas% enters the running state, an Interrupted.$ception is thro n. ,9:# What is casting? There are t o types of casting, casting bet een primitive numeric types and casting bet een object references. 3asting bet een numeric types is used to convert larger values, such as double values, to smaller values, such as byte values. 3asting bet een object references is used to refer to an object by a compatible class, interface, or array type reference. ,9;# What is the return type of a program=s main23 method? 9 programMs main'( method has a void return type. ,9<# 1ame four 0ontainer classes# Rindo , 0rame, >ialog, 0ile>ialog, 6anel, 9pplet, or /croll6ane ,9># What is the difference between a 0hoice and a &ist? 9 3hoice is displayed in a compact form that reBuires you to pull it do n to see the list of available choices. Nnly one item may be selected from a 3hoice. 9 :ist may be displayed in such a ay that several :ist items are visible. 9 :ist supports the selection of one or more :ist items. ,9?# What class of exceptions are generated by the Java run6time system? The !ava runtime system generates Runtime.$ception and .rror e$ceptions. ,-@# What class allows you to read objects directly from a stream? The NbjectInput/tream class supports the reading of objects from input streams. .Technicalsymposium.com

,-,# What is the difference between a field variable and a local variable? 9 field variable is a variable that is declared as a member of a class. 9 local variable is a variable that is declared local to a method. ,-8# Cnder what conditions is an object=s finali e23 method invoked by the garbage collector? The garbage collector invo%es an objectMs finali7e'( method hen it detects that the object has become unreachable. ,-9# How are this23 and super23 used with constructors? this'( is used to invo%e a constructor of the same class. super'( is used to invo%e a superclass constructor. ,--# What is the relationship between a method=s throws clause and the exceptions that can be thrown during the method=s execution? 9 methodMs thro s clause must declare any chec%ed e$ceptions that are not caught ithin the body of the method. ,-:# What is the difference between the J$+ ,#@8 event model and the event6 delegation model introduced with J$+ ,#,? The !>? 1.52 event model uses an event inheritance or bubbling approach. In this model, components are reBuired to handle their o n events. If they do not handle a particular event, the event is inherited by 'or bubbled up to( the componentMs container. The container then either handles the event or it is bubbled up to its container and so on, until the highest-level container has been tried. In the event-delegation model, specific objects are designated as event handlers for G8I components. These objects implement event-listener interfaces. The event-delegation model is more efficient than the event-inheritance model because it eliminates the processing reBuired to support the bubbling of unhandled events. ,-;# How is it possible for two 7tring objects with identical values not to be equal under the JJ operator? The EE operator compares t o objects to determine if they are the same object in memory. It is possible for t o /tring objects to have the same value, but located indifferent areas of memory. ,-<# Why are the methods of the %ath class static? /o they can be invo%ed as if they are a mathematical code library. .Technicalsymposium.com

,-># What 0heckbox method allows you to tell if a 0heckbox is checked? get/tate'( ,-?# What state is a thread in when it is executing? 9n e$ecuting thread is in the running state. ,:@# What are the legal operands of the instanceof operator? The left operand is an object reference or null value and the right operand is a class, interface, or array type. ,:,# How are the elements of a Grid&ayout organi ed? The elements of a Grid+ad layout are of eBual si7e and are laid out using the sBuares of a grid. ,:8# What an '*/ filter? 9n IAN filter is an object that reads from one stream and rites to another, usually altering the data in some ay as it is passed from one stream to another. ,:9# 'f an object is garbage collected) can it become reachable again? Nnce an object is garbage collected, it ceases to e$ist. It can no longer become reachable again. ,:-# What is the 7et interface? The /et interface provides methods for accessing the elements of a finite mathematical set. /ets do not allo duplicate elements. ,::# What classes of exceptions may be thrown by a throw statement? 9 thro statement may thro any e$pression that may be assigned to the Thro able type. ,:;# What are " and 4'? . is the base of the natural logarithm and 6I is mathematical value pi. ,:<# .re true and false keywords? The values true and false are not %ey ords.

.Technicalsymposium.com

,:># What is a void return type? 9 void return type indicates that a method does not return a value. ,:?# What is the purpose of the enable"vents23 method? The enable.vents'( method is used to enable an event for a particular object. Lormally, an event is enabled hen a listener is added to an object for a particular event. The enable.vents'( method is used by objects that handle events by overriding their eventdispatch methods. ,;@# What is the difference between the Aile and Fandom.ccessAile classes? The 0ile class encapsulates the files and directories of the local file system. The Random9ccess0ile class provides the methods needed to directly access data contained in any part of a file. ,;,# What happens when you add a double value to a 7tring? The result is a /tring object. ,;8# What is your platform=s default character encoding? If you are running !ava on .nglish Rindo s platforms, it is probably 3p12*2. If you are running !ava on .nglish /olaris platforms, it is most li%ely 22*4O1.. ,;9# Which package is always imported by default? The java.lang pac%age is al ays imported by default. ,;-# What interface must an object implement before it can be written to a stream as an object? 9n object must implement the /eriali7able or .$ternali7able interface before it can be ritten to a stream as an object. ,;:# How are this and super used? this is used to refer to the current object instance. super is used to refer to the variables and methods of the superclass of the current object instance. ,;;# What is the purpose of garbage collection? The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources may be reclaimed and reused.

.Technicalsymposium.com

,;<# What is a compilation unit? 9 compilation unit is a !ava source code file. ,;># What interface is extended by .W( event listeners? 9ll 9RT event listeners e$tend the java.util..vent:istener interface. ,;?# What restrictions are placed on method overriding? Nverridden methods must have the same name, argument list, and return type. The overriding method may not limit the access of the method it overrides. The overriding method may not thro any e$ceptions that may not be thro n by the overridden method. ,<@# How can a dead thread be restarted? 9 dead thread cannot be restarted. ,<,# What happens if an exception is not caught? 9n uncaught e$ception results in the uncaught.$ception'( method of the threadMs ThreadGroup being invo%ed, hich eventually results in the termination of the program in hich it is thro n. ,<8# What is a layout manager? 9 layout manager is an object that is used to organi7e components in a container. ,<9# Which arithmetic operations can result in the throwing of an .rithmetic"xception? Integer A and Y can result in the thro ing of an 9rithmetic.$ception. ,<-# What are three ways in which a thread can enter the waiting state? 9 thread can enter the aiting state by invo%ing its sleep'( method, by bloc%ing on IAN, by unsuccessfully attempting to acBuire an objectMs loc%, or by invo%ing an objectMs ait'( method. It can also enter the aiting state by invo%ing its 'deprecated( suspend'( method. ,<:# 0an an abstract class be final? 9n abstract class may not be declared as final. ,<;# What is the FesourceDundle class?

.Technicalsymposium.com

The Resource+undle class is used to store locale-specific resources that can be loaded by a program to tailor the programMs appearance to the particular locale in hich it is being run. ,<<# What happens if a try6catch6finally statement does not have a catch clause to handle an exception that is thrown within the body of the try statement? The e$ception propagates up to the ne$t higher level try-catch statement 'if any( or results in the programMs termination. ,<># What is numeric promotion? Lumeric promotion is the conversion of a smaller numeric type to a larger numeric type, so that integer and floating-point operations may ta%e place. In numerical promotion, 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 reBuired. ,<?# What is the difference between a 7crollbar and a 7croll4ane? 9 /crollbar is a 3omponent, but not a 3ontainer. 9 /croll6ane is a 3ontainer. 9 /croll6ane handles its o n events and performs its o n scrolling. ,>@# What is the difference between a public and a non6public class? 9 public class may be accessed outside of its pac%age. 9 non-public class may not be accessed outside of its pac%age. ,>,# (o what value is a variable of the boolean type automatically initiali ed? The default value of the boolean type is false. ,>8# 0an try statements be nested? Try statements may be tested. ,>9# What is the difference between the prefix and postfix forms of the LL operator? The prefi$ form performs the increment operation and returns the value of the increment operation. The postfi$ form returns the current value all of the e$pression and then performs the increment operation on that value. ,>-# What is the purpose of a statement block?

.Technicalsymposium.com

9 statement bloc% is used to organi7e a seBuence of statements as a single statement group. ,>:# What is a Java package and how is it used? 9 !ava pac%age is a naming conte$t for classes and interfaces. 9 pac%age is used to create a separate name space for groups of classes and interfaces. 6ac%ages are also used to organi7e related classes and interfaces into a single 96I unit and to control accessibility to these classes and interfaces. ,>;# What modifiers may be used with a top6level class? 9 top-level class may be public, abstract, or final. ,><# What are the /bject and 0lass classes used for? The Nbject class is the highest-level class in the !ava class hierarchy. The 3lass class is used to represent the classes and interfaces that are loaded by a !ava program. ,>># How does a try statement determine which catch clause should be used to handle an exception? Rhen an e$ception is thro n ithin the body of a try statement, the catch clauses of the try statement are e$amined in the order in hich they appear. The first catch clause that is capable of handling the e$ception is e$ecuted. The remaining catch clauses are ignored. ,>?# 0an an unreachable object become reachable again? 9n unreachable object may become reachable again. This can happen hen the objectMs finali7e'( method is invo%ed and the object performs an operation hich causes it to become accessible to reachable objects. ,?@# When is an object subject to garbage collection? 9n object is subject to garbage collection hen it becomes unreachable to the program in hich it is used. ,?,# What method must be implemented by all threads? 9ll tas%s must implement the run'( method, hether they are a subclass of Thread or implement the Runnable interface. ,?8# What methods are used to get and set the text label displayed by a Dutton object? get:abel'( and set:abel'( .Technicalsymposium.com

,?9# Which 0omponent subclass is used for drawing and painting? 3anvas ,?-# What are synchroni ed methods and synchroni ed statements? /ynchroni7ed methods are methods that are used to control access to an object. 9 thread only e$ecutes a synchroni7ed method after it has acBuired the loc% for the methodMs object or class. /ynchroni7ed statements are similar to synchroni7ed methods. 9 synchroni7ed statement can only be e$ecuted after a thread has acBuired the loc% for the object or class referenced in the synchroni7ed statement. ,?:# What are the two basic ways in which classes that can be run as threads may be defined? 9 thread class may be declared as a subclass of Thread, or it may implement the Runnable interface. ,?;# What are the problems faced by Java programmers who don=t use layout managers? Rithout layout managers, !ava programmers are faced ith determining ho their G8I ill be displayed across multiple indo ing systems and finding a common si7ing and positioning that ill or% ithin the constraints imposed by each indo ing system. ,?<# What is the difference between an if statement and a switch statement? The if statement is used to select among t o alternatives. It uses a boolean e$pression to decide hich alternative should be e$ecuted. The s itch statement is used to select among multiple alternatives. It uses an int e$pression to determine hich alternative should be e$ecuted. ,?># What happens when you add a double value to a 7tring? The result is a /tring object. ,??# What is the &ist interface? The :ist interface provides support for ordered collections of objects.

.Technicalsymposium.com

Potrebbero piacerti anche