Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
78 views

Java

The document contains questions and answers related to Java interview questions. Some of the topics covered include the differences between interfaces and abstract classes, the purpose of garbage collection, synchronization in multithreading, pass by reference vs pass by value, iterators, access modifiers, abstract classes, static vs final, checked and unchecked exceptions, method overriding, and inner classes.

Uploaded by

deepakishere
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

Java

The document contains questions and answers related to Java interview questions. Some of the topics covered include the differences between interfaces and abstract classes, the purpose of garbage collection, synchronization in multithreading, pass by reference vs pass by value, iterators, access modifiers, abstract classes, static vs final, checked and unchecked exceptions, method overriding, and inner classes.

Uploaded by

deepakishere
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 27

Java Interview Questions

Question: What is the difference between an Interface and an Abstract class? What is the purpose of garbage collection in Java, and when is it used? Question: Question: Describe synchronization in respect to ultithreading! Question: Question: Question: Question: Question: Question: Question: Question: Question: "#plain different way of using thread? What are pass by reference and passby value? What is $ash%ap and %ap? Difference between $ash%ap and $ash&able? Difference between 'ector and Array(ist? Difference between )wing and Awt? What is the difference between a constructor and a ethod? What is an Iterator? )tate the significance of public, private, protected, default odifiers both singly and in co bination and state the effect of pac*age relationships on declared ite s +ualified by these odifiers! Question: What is an abstract class? Question: What is static in ,ava? What is final? Question: Q: What is the difference between an Interface and an Abstract class? A: An abstract class can have instance ethods that i ple ent a default behavior! An Interface can only declare constants and instance ethods, but cannot i ple ent default behavior and all ethods are i plicitly abstract! An interface has all public e bers and no i ple entation! An abstract class is a class which ay have the usual flavors of class e bers -private, protected, etc!., but has so e abstract ethods! !

Q: What is the purpose of garbage collection in Java, and when is it used? A: &he purpose of garbage collection is to identify and discard ob,ects that are no longer needed by a progra so that their resources can be reclai ed and reused! A Java ob,ect is sub,ect to garbage collection when it beco es unreachable to the progra in which it is used!

Q: Describe synchronization in respect to ultithreading! A: With respect to ultithreading, synchronization is the capability to control the access of ultiple threads to shared resources! Without synchonization, it is possible for one thread to odify a shared variable while another thread is in the process of using or updating sa e shared variable! &his usually leads to significant

errors!

Q: "#plain different way of using thread? A: &he thread could be i ple ented by using runnable interface or by inheriting fro the &hread class! &he for er is ore advantageous, /cause when you are going for ultiple inheritance!!the only interface can help!

Q: What are pass by reference and passby value? A: 0ass 1y 2eference eans the passing the address itself rather than passing the value! 0assby 'alue eans passing a copy of the value to be passed!

Q: What is $ash%ap and %ap? A: %ap is Interface and $ash ap is class that i ple ents that!

Q: Difference between $ash%ap and $ash&able? A: &he $ash%ap class is roughly e+uivalent to $ashtable, e#cept that it is unsynchronized and per its nulls! -$ash%ap allows null values as *ey and value whereas $ashtable doesnt allow.! $ash%ap does not guarantee that the order of the ap will re ain constant over ti e! $ash%ap is unsynchronized and $ashtable is synchronized!

Q: Difference between 'ector and Array(ist? A: 'ector is synchronized whereas arraylist is not!

Q: Difference between )wing and Awt? A: AW& are heavy3weight co ponenets! )wings are light3weight co ponents! $ence swing wor*s faster than AW&!

Q: What is the difference between a constructor and a ethod?

A: A constructor is a e ber function of a class that is used to create ob,ects of that class! It has the sa e na e as the class itself, has no return type, and is invo*ed using the new operator! A ethod is an ordinary e ber function of a class! It has its own na e, a return type -which ay be void., and is invo*ed using the dot operator!

Q: What is an Iterator? A: )o e of the collection classes provide traversal of their contents via a ,ava!util!Iterator interface! &his interface allows you to wal* through a collection of ob,ects, operating on each ob,ect in turn! 2e e ber when using Iterators that they contain a snapshot of the collection at the ti e the Iterator was obtained4 generally it is not advisable to odify the collection itself while traversing an Iterator!

Q: )tate the significance of public, private, protected, default odifiers both singly and in co bination and state the effect of pac*age relationships on declared ite s +ualified by these odifiers! A: public : 0ublic class is visible in other pac*ages, field is visible everywhere -class ust be public too. private : 0rivate variables or ethods ay be used only by an instance of the sa e class that declares the variable or ethod, A private feature ay only be accessed by the class that owns the feature! protected : Is available to all classes in the sa e pac*age and also available to all subclasses of the class that owns the protected feature!&his access is provided even to subclasses that reside in a different pac*age fro the class that owns the protected feature! default :What you get by default ie, without any access odifier -ie, public private or protected.!It eans that it is visible to all within a particular pac*age!

Q: What is an abstract class? A: Abstract class ust be e#tended5subclassed -to be useful.! It serves as a te plate! A class that is abstract ay not be instantiated -ie, you ay not call its constructor., abstract class ay contain static data! Any class with an abstract ethod is auto atically abstract itself, and ust be declared as such! A class ay be declared abstract even if it has no abstract ethods! &his prevents it fro being instantiated!

Q: What is static in ,ava?

A: )tatic eans one per class, not one for each ob,ect no atter how any instance of a class ight e#ist! &his eans that you can use the without creating an instance of a class!)tatic ethods are i plicitly final, because overriding is done based on the type of the ob,ect, and static ethods are attached to a class, not an ob,ect! A static ethod in a superclass can be shadowed by another static ethod in a subclass, as long as the original ethod was not declared final! $owever, you can/t override a static ethod with a nonstatic ethod! In other words, you can/t change a static ethod into an instance ethod in a subclass!

Q: What is final? A: A final class can/t be e#tended ie!, final class ay not be subclassed! A final ethod can/t be overridden when its class is inherited! 6ou can/t change value of a final variable -is a constant.!

Java Interview Questions


Question: What if the ain ethod is declared as private? Question: What if the static odifier is re oved fro the signature of the ain ethod? Question: What if I write static public void instead of public static void? Question: What if I do not provide the )tring array as the argu ent to the ethod? Question: What is the first argu ent of the )tring array in ain ethod? Question: If I do not provide any argu ents on the co and line, then the )tring array of %ain ethod will be e pty or null? Question: $ow can one prove that the array is not null but e pty using one line of code? Question: What environ ent variables do I need to set on y achine in order to be able to run Java progra s? Question: 7an an application have ultiple classes having ain ethod? Question: 7an I have ultiple ain ethods in the sa e class? Question: Do I need to i port ,ava!lang pac*age any ti e? Why ? Question: 7an I i port sa e pac*age5class twice? Will the J'% load the pac*age twice at runti e? Question: What are 7hec*ed and 8n7hec*ed "#ception? Question: What is 9verriding? Question: What are different types of inner classes? Q: What if the ain ethod is declared as private? A: &he progra co piles properly but at runti e it will give :%ain public!: essage! ; 2eceived fro )andesh )adhale< Q: What if the static odifier is re oved fro the signature of the ain ethod? A: 0rogra co piles! 1ut at runti e throws an error :=o)uch%ethod"rror:! ; 2eceived fro )andesh )adhale< ethod not

Q: What if I write static public void instead of public static void? A: 0rogra co piles and runs properly! ; 2eceived fro )andesh )adhale< Q: What if I do not provide the )tring array as the argu ent to the A: 0rogra co piles but throws a runti e error :=o)uch%ethod"rror:! ; 2eceived fro )andesh )adhale< Q: What is the first argu ent of the )tring array in ain ethod? A: &he )tring array is e pty! It does not have any ele ent! &his is unli*e 757>> where the first ele ent by default is the progra na e! ; 2eceived fro )andesh )adhale< Q: If I do not provide any argu ents on the co array of %ain ethod will be e pty or null? A: It is e pty! 1ut not null! ; 2eceived fro )andesh )adhale< Q: $ow can one prove that the array is not null but e pty using one line of code? A: 0rint args!length! It will print ?! &hat eans it is e pty! 1ut if it would have been null then it would have thrown a =ull0ointer"#ception on atte pting to print args!length! ; 2eceived fro )andesh )adhale< Q: What environ ent variables do I need to set on be able to run Java progra s? A: 7(A))0A&$ and 0A&$ are the two variables! ; 2eceived fro )andesh )adhale< Q: -an an application have ultiple classes having ain ethod? A: 6es it is possible! While starting the application we ention the class na e to be run! &he J'% will loo* for the %ain ethod only in the class whose na e you have entioned! $ence there is not conflict a ongst the ultiple classes having ain ethod! ; 2eceived fro )andesh )adhale< Q: -an I have ultiple ain ethods in the sa e class? A: =o the progra fails to co pile! &he co piler says that the ain ethod is y achine in order to and line, then the )tring ethod?

already defined in the class! ; 2eceived fro )andesh )adhale< Q: Do I need to i port ,ava!lang pac*age any ti A: =o! It is by default loaded internally by the J'%! ; 2eceived fro )andesh )adhale< Q: -an I i port sa e pac*age.class twice? Will the J'% load the pac*age twice at runti e? A: 9ne can i port the sa e pac*age or sa e class ultiple ti es! =either co piler nor J'% co plains abt it! And the J'% will internally load the class only once no atter how any ti es you i port the sa e class! ; 2eceived fro )andesh )adhale< Q: What are -hec*ed and /n-hec*ed "#ception? A: A chec*ed e#ception is so e subclass of "#ception -or "#ception itself., e#cluding class 2unti e"#ception and its subclasses! %a*ing an e#ception chec*ed forces client progra ers to deal with the possibility that the e#ception will be thrown! eg, I9"#ception thrown by ,ava!io!@ileInput)trea /s read-. ethodA 8nchec*ed e#ceptions are 2unti e"#ception and any of its subclasses! 7lass "rror and its subclasses also are unchec*ed! With an unchec*ed e#ception, however, the co piler doesn/t force client progra ers either to catch the e#ception or declare it in a throws clause! In fact, client progra ers ay not even *now that the e#ception could be thrown! eg, )tringInde#9ut9f1ounds"#ception thrown by )tring/s charAt-. ethodA 7hec*ed e#ceptions ust be caught at co pile ti e! 2unti e e#ceptions do not need to be! "rrors often cannot be! e? Why ?

Q: What is 0verriding? A: When a class defines a ethod using the sa e na e, return type, and argu ents as a ethod in its superclass, the ethod in the class overrides the ethod in the superclass! When the ethod is invo*ed for an ob,ect of the class, it is the new definition of the ethod that is called, and not the ethod definition fro superclass! %ethods ay be overridden to be ore public, not ore private!

Q: What are different types of inner classes? A: Nested -level classes, Member classes, Local classes, Anonymous classes Nested -level classes3 If you declare a class within a class and specify the static odifier, the co piler treats the class ,ust li*e any other 3level class! Any class outside the declaring class accesses the nested class with the declaring

class na e acting si ilarly to a pac*age! eg, outer!inner! 3level inner classes i plicitly have access only to static variables!&here can also be inner interfaces! All of these are of the nested 3level variety! Member classes 3 %e ber inner classes are ,ust li*e other e ber ethods and e ber variables and access to the e ber class is restricted, ,ust li*e ethods and variables! &his eans a public e ber class acts si ilarly to a nested 3level class! &he pri ary difference between e ber classes and nested 3level classes is that e ber classes have access to the specific instance of the enclosing class! Local classes 3 (ocal classes are li*e local variables, specific to a bloc* of code! &heir visibility is only within the bloc* of their declaration! In order for the class to be useful beyond the declaration bloc*, it would need to i ple ent a ore publicly available interface!1ecause local classes are not e bers, the odifiers public, protected, private, and static are not usable! Anonymous classes 3 Anony ous inner classes e#tend local inner classes one level further! As anony ous classes have no na e, you cannot provide a constructor!

Java Interview Questions


Question:Are the i ports chec*ed for validity at co pile ti e? e!g! will the code containing an i port such as ,ava!lang!A17D co pile? Question:Does i porting a pac*age i ports the subpac*ages as well? e!g! Does i porting co !%y&est!B also i port co !%y&est!8nit&ests!B? Question:What is the difference between declaring a variable and defining a variable? Question:What is the default value of an ob,ect reference declared as an instance variable? Question:7an a level class be private or protected? Question:What type of para eter passing does Java support? Question:0ri itive data types are passed by reference or pass by value? Question:9b,ects are passed by value or by reference? Question:What is serialization? Question:$ow do I serialize an ob,ect to a file? Question:Which ethods of )erializable interface should I i ple ent? Question:$ow can I custo ize the seralization process? i!e! how can one have a control over the serialization process? Question:What is the co on usage of serialization? Question:What is "#ternalizable interface? Question:When you serialize an ob,ect, what happens to the ob,ect references included in the ob,ect? Question:What one should ta*e care of while serializing the ob,ect? Question:What happens to the static fields of a class during serialization?

Q: Are the i ports chec*ed for validity at co pile ti e? e!g! will the code containing an i port such as ,ava!lang!A1-D co pile? A: 6es the i ports are chec*ed for the se antic validity at co pile ti e! &he code containing above line of i port will not co pile! It will throw an error saying,can not resolve sy bol sy bol : class A17D location: pac*age io i port ,ava!io!A17D4 ; 2eceived fro )andesh )adhale< Q: Does i porting a pac*age i ports the subpac*ages as well? e!g! Does i porting co !%y&est!2 also i port co !%y&est!/nit&ests!2? A: =o you will have to i port the subpac*ages e#plicitly! I porting co !%y&est!B will i port classes in the pac*age %y&est only! It will not i port any class in any of it/s subpac*age! ; 2eceived fro )andesh )adhale< Q: What is the difference between declaring a variable and defining a variable? A: In declaration we ,ust ention the type of the variable and it/s na e! We do not initialize it! 1ut defining eans declaration > initialization! e!g )tring s4 is ,ust a declaration while )tring s C new )tring -:abcd:.4 9r )tring s C :abcd:4 are both definitions! ; 2eceived fro )andesh )adhale< Q: What is the default value of an ob,ect reference declared as an instance variable? A: null unless we define it e#plicitly! ; 2eceived fro )andesh )adhale< Q: -an a level class be private or protected? A: =o! A level class can not be private or protected! It can have either :public: or no odifier! If it does not have a odifier it is supposed to have a default access!If a level class is declared as private the co piler will co plain that the : odifier private is not allowed here:! &his eans that a level class can not be private! )a e is the case with protected! ; 2eceived fro )andesh )adhale< Q: What type of para eter passing does Java support? A: In Java the argu ents are always passed by value ! ; 8pdate fro "*i and Jyothish 'enu<

Q: 3ri itive data types are passed by reference or pass by value? A: 0ri itive data types are passed by value! ; 2eceived fro )andesh )adhale< Q: 0b,ects are passed by value or by reference? A: Java only supports pass by value! With ob,ects, the ob,ect reference itself is passed by value and so both the original reference and para eter copy both refer to the sa e ob,ect ! ; 8pdate fro "*i and Jyothish 'enu< Q: What is serialization? A: )erialization is a echanis by which you can save the state of an ob,ect by converting it to a byte strea ! ; 2eceived fro )andesh )adhale< Q: $ow do I serialize an ob,ect to a file? A: &he class whose instances are to be serialized should i ple ent an interface )erializable! &hen you pass the instance to the 9b,ect9utput)trea which is connected to a fileoutputstrea ! &his will save the ob,ect to a file! ; 2eceived fro )andesh )adhale< Q: Which ethods of )erializable interface should I i ple ent? A: &he serializable interface is an e pty interface, it does not contain any ethods! )o we do not i ple ent any ethods! ; 2eceived fro )andesh )adhale< Q: $ow can I custo ize the seralization process? i!e! how can one have a control over the serialization process? A: 6es it is possible to have control over serialization process! &he class should i ple ent "#ternalizable interface! &his interface contains two ethods na ely read"#ternal and write"#ternal! 6ou should i ple ent these ethods and write the logic for custo izing the serialization process! ; 2eceived fro )andesh )adhale< Q: What is the co on usage of serialization? A: Whenever an ob,ect is to be sent over the networ*, ob,ects need to be serialized! %oreover if the state of an ob,ect is to be saved, ob,ects need to be serilazed! ; 2eceived fro )andesh )adhale< Q: What is "#ternalizable interface?

A: "#ternalizable is an interface which contains two ethods read"#ternal and write"#ternal! &hese ethods give you a control over the serialization echanis ! &hus if your class i ple ents this interface, you can custo ize the serialization process by i ple enting these ethods! ; 2eceived fro )andesh )adhale< Q: When you serialize an ob,ect, what happens to the ob,ect references included in the ob,ect? A: &he serialization echanis generates an ob,ect graph for serialization! &hus it deter ines whether the included ob,ect references are serializable or not! &his is a recursive process! &hus when an ob,ect is serialized, all the included ob,ects are also serialized alongwith the original obect! ; 2eceived fro )andesh )adhale< Q: What one should ta*e care of while serializing the ob,ect? A: 9ne should a*e sure that all the included ob,ects are also serializable! If any of the ob,ects is not serializable then it throws a =ot)erializable"#ception! ; 2eceived fro )andesh )adhale< Q: What happens to the static fields of a class during serialization? A: &here are three e#ceptions in which serialization doesnot necessarily read and write to the strea ! &hese are D! )erialization ignores static fields, because they are not part of ay particular state state! E! 1ase class fields are only hendled if the base class itself is serializable! F! &ransient fields! ; 2eceived fro )andesh )adhale %odified after 0!John David co ents!<

Java Interview Questions


Question:Does Java provide any construct to find out the size of an ob,ect? Question:Give a si plest way to find out the ti e a ethod ta*es for e#ecution without using any profiling tool? Question:What are wrapper classes? Question:Why do we need wrapper classes? Question:What are chec*ed e#ceptions? Question:What are runti e e#ceptions? Question:What is the difference between error and an e#ception?? Question:$ow to create custo e#ceptions? Question:If I want an ob,ect of y class to be thrown as an e#ception ob,ect, what should I do? Question:If y class already e#tends fro so e other class what should I do if I want an instance of y class to be thrown as an e#ception ob,ect? Question:$ow does an e#ception per eate through the code? Question:What are the different ways to handle e#ceptions?

Question:What is the basic difference between the E approaches to e#ception handling!!!DH try catch bloc* and EH specifying the candidate e#ceptions in the throws clause? When should you use which approach? Question:Is it necessary that each try bloc* ust be followed by a catch bloc*? Question:If I write return at the end of the try bloc*, will the finally bloc* still e#ecute? Question:If I write )yste !e#it -?.4 at the end of the try bloc*, will the finally bloc* still e#ecute?

Q: Does Java provide any construct to find out the size of an ob,ect? A: =o there is not sizeof operator in Java! )o there is not direct way to deter ine the size of an ob,ect directly in Java! ; 2eceived fro )andesh )adhale< Q: 4ive a si plest way to find out the ti e a ethod ta*es for e#ecution without using any profiling tool? A: 2ead the syste ti e ,ust before the ethod is invo*ed and i ediately after ethod returns! &a*e the ti e difference, which will give you the ti e ta*en by a ethod for e#ecution!

&o put it in code!!!


long start C )yste !current&i e%illis -.4 ethod -.4 long end C )yste !current&i e%illis -.4 )yste !out!println -:&i e ta*en for e#ecution is : > -end 3 start..4 2e e ber that if the ti e ta*en for e#ecution is too s all, it ight show that it is ta*ing zero illiseconds for e#ecution! &ry it on a ethod which is big enough, in the sense the one which is doing considerable a out of processing! ; 2eceived fro )andesh )adhale< Q: What are wrapper classes? A: Java provides specialized classes corresponding to each of the pri itive data types! &hese are called wrapper classes! &hey are e!g! Integer, 7haracter, Double etc! ; 2eceived fro )andesh )adhale< Q: Why do we need wrapper classes? A: It is so eti es easier to deal with pri itives as ob,ects! %oreover ost of the collection classes store ob,ects and not pri itive data types! And also the wrapper classes provide any utility ethods also! 1ecause of these resons we need wrapper classes! And since we create instances of these classes we can

store the in any of the collection classes and pass the around as a collection! Also we can pass the around as ethod para eters where a ethod e#pects an ob,ect! ; 2eceived fro )andesh )adhale< Q: What are chec*ed e#ceptions? A: 7hec*ed e#ception are those which the Java co piler forces you to catch! e!g! I9"#ception are chec*ed "#ceptions! ; 2eceived fro )andesh )adhale< Q: What are runti e e#ceptions? A: 2unti e e#ceptions are those e#ceptions that are thrown at runti e because of either wrong input data or because of wrong business logic etc! &hese are not chec*ed by the co piler at co pile ti e! ; 2eceived fro )andesh )adhale< Q: What is the difference between error and an e#ception? A: An error is an irrecoverable condition occurring at runti e! )uch as 9ut9f%e ory error! &hese J'% errors and you can not repair the at runti e! While e#ceptions are conditions that occur because of bad input etc! e!g! @ile=ot@ound"#ception will be thrown if the specified file does not e#ist! 9r a =ull0ointer"#ception will ta*e place if you try using a null reference! In ost of the cases it is possible to recover fro an e#ception -probably by giving user a feedbac* for entering proper values etc!.! ; 2eceived fro )andesh )adhale< Q: $ow to create custo e#ceptions? A: 6our class should e#tend class "#ception, or so e ; 2eceived fro )andesh )adhale<

ore specific type thereof!

Q: If I want an ob,ect of y class to be thrown as an e#ception ob,ect, what should I do? A: &he class should e#tend fro "#ception class! 9r you can e#tend your class fro so e ore precise e#ception type also! ; 2eceived fro )andesh )adhale< Q: If y class already e#tends fro so e other class what should I do if I want an instance of y class to be thrown as an e#ception ob,ect? A: 9ne can not do anytihng in this scenarion! 1ecause Java does not allow ultiple inheritance and does not provide any e#ception interface as well! ; 2eceived fro )andesh )adhale<

Q: $ow does an e#ception per eate through the code? A: An unhandled e#ception oves up the ethod stac* in search of a atching When an e#ception is thrown fro a code which is wrapped in a try bloc* followed by one or ore catch bloc*s, a search is ade for atching catch bloc*! If a atching type is found then that bloc* will be invo*ed! If a atching type is not found then the e#ception oves up the ethod stac* and reaches the caller ethod! )a e procedure is repeated if the caller ethod is included in a try catch bloc*! &his process continues until a catch bloc* handling the appropriate type of e#ception is found! If it does not find such a bloc* then finally the progra ter inates! ; 2eceived fro )andesh )adhale< Q: What are the different ways to handle e#ceptions? A: &here are two ways to handle e#ceptions, D! 1y wrapping the desired code in a try bloc* followed by a catch bloc* to catch the e#ceptions! and E! (ist the desired e#ceptions in the throws clause of the ethod and let the caller of the ethod hadle those e#ceptions! ; 2eceived fro )andesh )adhale< Q: What is the basic difference between the 5 approaches to e#ception handling! 67 try catch bloc* and 57 specifying the candidate e#ceptions in the throws clause? When should you use which approach? A: In the first approach as a progra er of the ethod, you urself are dealing with the e#ception! &his is fine if you are in a best position to decide should be done in case of an e#ception! Whereas if it is not the responsibility of the ethod to deal with it/s own e#ceptions, then do not use this approach! In this case use the second approach! In the second approach we are forcing the caller of the ethod to catch the e#ceptions, that the ethod is li*ely to throw! &his is often the approach library creators use! &hey list the e#ception in the throws clause and we ust catch the ! 6ou will find the sa e approach throughout the ,ava libraries we use! ; 2eceived fro )andesh )adhale< Q: Is it necessary that each try bloc* ust be followed by a catch bloc*? A: It is not necessary that each try bloc* ust be followed by a catch bloc*! It should be followed by either a catch bloc* 92 a finally bloc*! And whatever e#ceptions are li*ely to be thrown should be declared in the throws clause of the ethod! ; 2eceived fro )andesh )adhale< Q: If I write return at the end of the try bloc*, will the finally bloc* still e#ecute?

A: 6es even if you write return as the last state ent in the try bloc* and no e#ception occurs, the finally bloc* will e#ecute! &he finally bloc* will e#ecute and then the control return! ; 2eceived fro )andesh )adhale< Q: If I write )yste !e#it 89:; at the end of the try bloc*, will the finally bloc* still e#ecute? A: =o in this case the finally bloc* will not e#ecute because when you say )yste !e#it -?.4 the control i ediately goes out of the progra , and thus finally never e#ecutes! ; 2eceived fro )andesh )adhale<

Java Interview Questions


Question:$ow are 9bserver and 9bservable used? Question:What is synchronization and why is it i portant? Question:$ow does Java handle integer overflows and underflows? Question:Does garbage collection guarantee that a progra will not run out of e ory? Question:What is the difference between pree ptive scheduling and ti e slicing? Question:When a thread is created and started, what is its initial state? Question:What is the purpose of finalization? Question:What is the (ocale class? Question:What is the difference between a while state ent and a do state ent? Question:What is the difference between static and non3static variables? Question:$ow are this-. and super-. used with constructors? Question:What are synchronized ethods and synchronized state ents? Question:What is dae on thread and which ethod is used to create the dae on thread? Question:7an applets co unicate with each other? Question:What are the steps in the JD17 connection? Question:$ow does a try state ent deter ine which catch clause should be used to handle an e#ception?

Q: $ow are 0bserver and 0bservable used? A: 9b,ects that subclass the 9bservable class aintain a list of observers! When an 9bservable ob,ect is updated it invo*es the update-. ethod of each of its observers to notify the observers that it has changed state! &he 9bserver interface is i ple ented by ob,ects that observe 9bservable ob,ects! ;2eceived fro 'en*ateswara %ana < Q: What is synchronization and why is it i portant? A: With respect to ultithreading, synchronization is the capability to control the access of ultiple threads to shared resources! Without synchronization, it is

possible for one thread to odify a shared ob,ect while another thread is in the process of using or updating that ob,ect/s value! &his often leads to significant errors! ; 2eceived fro 'en*ateswara %ana < Q: $ow does Java handle integer overflows and underflows? A: It uses those low order bytes of the result that can fit into the size of the type allowed by the operation! ; 2eceived fro 'en*ateswara %ana < Q: Does garbage collection guarantee that a progra will not run out of e ory? A: Garbage collection does not guarantee that a progra will not run out of e ory! It is possible for progra s to use up e ory resources faster than they are garbage collected! It is also possible for progra s to create ob,ects that are not sub,ect to garbage collection ! ; 2eceived fro 'en*ateswara %ana < Q: What is the difference between pree ptive scheduling and ti e slicing? A: 8nder pree ptive scheduling, the highest priority tas* e#ecutes until it enters the waiting or dead states or a higher priority tas* co es into e#istence! 8nder ti e slicing, a tas* e#ecutes for a predefined slice of ti e and then reenters the pool of ready tas*s! &he scheduler then deter ines which tas* should e#ecute ne#t, based on priority and other factors! ; 2eceived fro 'en*ateswara %ana < Q: When a thread is created and started, what is its initial state? A: A thread is in the ready state after it has been created and started! ; 2eceived fro 'en*ateswara %ana < Q: What is the purpose of finalization? A: &he purpose of finalization is to give an unreachable ob,ect the opportunity to perfor any cleanup processing before the ob,ect is garbage collected! ; 2eceived fro 'en*ateswara %ana < Q: What is the (ocale class? A: &he (ocale class is used to tailor progra geographic, political, or cultural region! ; 2eceived fro 'en*ateswara %ana < Q: What is the difference between a while state ent and a do state ent? output to the conventions of a particular

A: A while state ent chec*s at the beginning of a loop to see whether the ne#t loop iteration should occur! A do state ent chec*s at the end of a loop to see whether the ne#t iteration of a loop should occur! &he do state ent will always e#ecute the body of a loop at least once! ; 2eceived fro 'en*ateswara %ana < Q: What is the difference between static and non<static variables? A: A static variable is associated with the class as a whole rather than with specific instances of a class! =on3static variables ta*e on uni+ue values with each ob,ect instance! ; 2eceived fro 'en*ateswara %ana < Q: $ow are this8: and super8: used with constructors? A: &his-. is used to invo*e a constructor of the sa e class! super-. is used to invo*e a superclass constructor! ; 2eceived fro 'en*ateswara %ana < Q: What are synchronized ethods and synchronized state ents? A: )ynchronized ethods are ethods that are used to control access to an ob,ect! A thread only e#ecutes a synchronized ethod after it has ac+uired the loc* for the ethod/s ob,ect or class! )ynchronized state ents are si ilar to synchronized ethods! A synchronized state ent can only be e#ecuted after a thread has ac+uired the loc* for the ob,ect or class referenced in the synchronized state ent! ; 2eceived fro 'en*ateswara %ana < Q: What is dae on thread and which ethod is used to create the dae on thread? A: Dae on thread is a low priority thread which runs inter ittently in the bac* ground doing the garbage collection operation for the ,ava runti e syste ! setDae on ethod is used to create a dae on thread! ; 2eceived fro )hipra Ia ra< Q: -an applets co unicate with each other? A: At this point in ti e applets ay co unicate with other applets running in the sa e virtual achine! If the applets are of the sa e class, they can co unicate via shared static variables! If the applets are of different classes, then each will need a reference to the sa e class with static variables! In any case the basic idea is to pass the infor ation bac* and forth through a static variable! An applet can also get references to all other applets on the sa e page using the getApplets-. ethod of ,ava!applet!Applet7onte#t! 9nce you get the reference to an applet, you can co unicate with it by using its public e bers! It is conceivable to have applets in different virtual achines that tal* to a server so ewhere on the Internet and store any data that needs to be serialized there!

&hen, when another applet needs this data, it could connect to this sa e server! I ple enting this is non3trivial! ; 2eceived fro Irishna Iu ar < Q: What are the steps in the JD1- connection? A: While a*ing a JD17 connection we go through the following steps : )tep D : 2egister the database driver by using : 7lass!for=a e-J: driver classs for that specific databaseJ: .4 )tep E : =ow create a database connection using : 7onnection con C Driver%anager!get7onnection-url,userna e,password.4 )tep F: =ow 7reate a +uery using : )tate ent st t C 7onnection!)tate ent-J:select B fro )tep K : "#ceute the +uery : st t!e#ceute8pdate-.4 ; 2eceived fro Q: $ow does a try state ent deter ine which catch clause should be used to handle an e#ception? A: When an e#ception is thrown within the body of a try state ent, the catch clauses of the try state ent are e#a ined in the order in which they appear! &he first catch clause that is capable of handling the e#ceptionis e#ecuted! &he re aining catch clauses are ignored! ; 2eceived fro 0 2a,esh< )hri 0ra*ash Iunwar< &A1(" =A%"J:.4

Java Interview Questions


Question:7an an unreachable ob,ect beco e reachable again? Question:What ethod ust be i ple ented by all threads? Question:What are synchronized ethods and synchronized state ents? Question:What is "#ternalizable? Question:What odifiers are allowed for ethods in an Interface? Question:What are so e alternatives to inheritance? Question:What does it ean that a ethod or field is :static:? ? Question:What is the difference between pree ptive scheduling and ti e slicing? Question:What is the catch or declare rule for ethod declarations?

Q: -an an unreachable ob,ect beco e reachable again? A: An unreachable ob,ect ay beco e reachable again! &his can happen when the ob,ect/s finalize-. ethod is invo*ed and the ob,ect perfor s an operation which causes it to beco e accessible to reachable ob,ects! ;2eceived fro 0 2a,esh< Q: What ethod ust be i ple ented by all threads? A: All tas*s ust i ple ent the run-. ethod, whether they are a subclass of &hread or i ple ent the 2unnable interface! ; 2eceived fro 0 2a,esh< Q: What are synchronized ethods and synchronized state ents? A: )ynchronized ethods are ethods that are used to control access to an ob,ect! A thread only e#ecutes a synchronized ethod after it has ac+uired the loc* for the ethod/s ob,ect or class! )ynchronized state ents are si ilar to synchronized ethods! A synchronized state ent can only be e#ecuted after a thread has ac+uired the loc* for the ob,ect or class referenced in the synchronized state ent! ; 2eceived fro 0 2a,esh< Q: What is "#ternalizable? A: "#ternalizable is an Interface that e#tends )erializable Interface! And sends data into )trea s in 7o pressed @or at! It has two ethods, write"#ternal-9b,ect9uput out. and read"#ternal-9b,ectInput in. ; 2eceived fro 'en*ateswara %ana < Q: What odifiers are allowed for ethods in an Interface? A: 9nly public and abstract odifiers are allowed for ethods in interfaces! ; 2eceived fro 0 2a,esh< Q: What are so e alternatives to inheritance? A: Delegation is an alternative to inheritance! Delegation eans that you include an instance of another class as an instance variable, and forward essages to the instance! It is often safer than inheritance because it forces you to thin* about each essage you forward, because the instance is of a *nown class, rather than a new class, and because it doesn/t force you to accept all the ethods of the super class: you can provide only the ethods that really a*e sense! 9n the other hand, it a*es you write ore code, and it is harder to re3use -because it is not a subclass.! ; 2eceived fro 0 2a,esh<

Q: What does it ean that a ethod or field is =static=? A: )tatic variables and ethods are instantiated only once per class! In other words they are class variables, not instance variables! If you change the value of a static variable in a particular ob,ect, the value of that variable changes for all instances of that class! )tatic ethods can be referenced with the na e of the class rather than the na e of a particular ob,ect of the class -though that wor*s too.! &hat/s how library ethods li*e )yste !out!println-. wor* out is a static field in the ,ava!lang!)yste class! ; 2eceived fro Q: What is the difference between pree ptive scheduling and ti e slicing? A: 8nder pree ptive scheduling, the highest priority tas* e#ecutes until it enters the waiting or dead states or a higher priority tas* co es into e#istence! 8nder ti e slicing, a tas* e#ecutes for a predefined slice of ti e and then reenters the pool of ready tas*s! &he scheduler then deter ines which tas* should e#ecute ne#t, based on priority and other factors! ; 2eceived fro 0 2a,esh< Q: What is the catch or declare rule for ethod declarations? A: If a chec*ed e#ception ay be thrown within the body of a ethod, the ust either catch the e#ception or declare it in its throws clause! ; 2eceived fro 0 2a,esh< ethod 0 2a,esh<

Java Interview Questions


Question:Is " pty !,ava file a valid source file? Question:7an a !,ava file contain ore than one ,ava classes? Question:Is )tring a pri itive data type in Java? Question:Is ain a *eyword in Java? Question:Is ne#t a *eyword in Java? Question:Is delete a *eyword in Java? Question:Is e#it a *eyword in Java? Question:What happens if you dont initialize an instance variable of any of the pri itive types in Java? Question:What will be the initial value of an ob,ect reference which is defined as an instance variable? Question:What are the different scopes for Java variables? Question:What is the default value of the local variables? Question:$ow any ob,ects are created in the following piece of code? %y7lass cD, cE, cF4 cD C new %y7lass -.4 cF C new %y7lass -.4 Question:7an a public class %y7lass be defined in a source file na ed 6our7lass!,ava?

Question:7an ain ethod be declared final? Question:What will be the output of the following state ent? )yste !out!println -:D: > F.4 Question:What will be the default values of all the ele ents of an array defined as an instance variable?

Q: Is " pty !,ava file a valid source file? A: 6es, an e pty !,ava file is a perfectly valid source file! ;2eceived fro )andesh )adhale< Q: -an a !,ava file contain ore than one ,ava classes? A: 6es, a !,ava file contain ore than one ,ava classes, provided at the the is a public class! ; 2eceived fro )andesh )adhale< Q: Is )tring a pri itive data type in Java? A: =o )tring is not a pri itive data type in Java, even though it is one of the ost e#tensively used ob,ect! )trings in Java are instances of )tring class defined in ,ava!lang pac*age! ; 2eceived fro )andesh )adhale< Q: Is ain a *eyword in Java? A: =o, ain is not a *eyword in Java! ; 2eceived fro )andesh )adhale< Q: Is ne#t a *eyword in Java? A: =o, ne#t is not a *eyword! ; 2eceived fro )andesh )adhale< Q: Is delete a *eyword in Java? A: =o, delete is not a *eyword in Java! Java does not the way 7>> does! ; 2eceived fro )andesh )adhale< Q: Is e#it a *eyword in Java? A: =o! &o e#it a progra e#plicitly you use e#it ; 2eceived fro )andesh )adhale< ethod in )yste ob,ect! a*e use of e#plicit destructors ost one of

Q: What happens if you dont initialize an instance variable of any of the pri itive types in Java? A: Java by default initializes it to the default value for that pri itive type! &hus an int will be initialized to ?, a boolean will be initialized to false! ; 2eceived fro )andesh )adhale< Q: What will be the initial value of an ob,ect reference which is defined as an instance variable? A: &he ob,ect references are all initialized to null in Java! $owever in order to do anything useful with these references, you ust set the to a valid ob,ect, else you will get =ull0ointer"#ceptions everywhere you try to use such default initialized references! ; 2eceived fro )andesh )adhale< Q: What are the different scopes for Java variables? A: &he scope of a Java variable is deter ined by the conte#t in which the variable is declared! &hus a ,ava variable can have one of the three scopes at any given point in ti e! D! Instance : 3 &hese are typical ob,ect level variables, they are initialized to default values at the ti e of creation of ob,ect, and re ain accessible as long as the ob,ect accessible! E! (ocal : 3 &hese are the variables that are defined within a ethod! &hey re ain accessbile only during the course of ethod e#cecution! When the ethod finishes e#ecution, these variables fall out of scope! F! )tatic: 3 &hese are the class level variables! &hey are initialized when the class is loaded in J'% for the first ti e and re ain there as long as the class re ains loaded! &hey are not tied to any particular ob,ect instance! ; 2eceived fro )andesh )adhale< Q: What is the default value of the local variables? A: &he local variables are not initialized to any default value, neither pri itives nor ob,ect references! If you try to use these variables without initializing the e#plicitly, the ,ava co piler will not co pile the code! It will co plain abt the local varaible not being initilized!! ; 2eceived fro )andesh )adhale< Q: $ow any ob,ects are created in the following piece of code? %y-lass c6, c5, c>; c6 ? new %y-lass 8:; c> ? new %y-lass 8:; A: 9nly E ob,ects are created, cD and cF! &he reference cE is only declared and not initialized! ; 2eceived fro )andesh )adhale<

Q: -an a public class %y-lass be defined in a source file na ed @our-lass!,ava? A: =o the source file na e, if it contains a public class, ust be the sa e as the public class na e itself with a !,ava e#tension! ; 2eceived fro )andesh )adhale< Q: -an ain ethod be declared final? A: 6es, the ain ethod can be declared final, in addition to being public static! ; 2eceived fro )andesh )adhale< Q: What will be the output of the following state )yste !out!println 8=6= A >:; A: It will print DF! ; 2eceived fro )andesh )adhale< Q: What will be the default values of all the ele ents of an array defined as an instance variable? A: If the array is an array of pri itive types, then all the ele ents of the array will be initialized to the default value corresponding to that pri itive type! e!g! All the ele ents of an array of int will be initialized to ?, while that of boolean type will be initialized to false! Whereas if the array is an array of references -of any type., all the ele ents will be initialized to null! ; 2eceived fro )andesh )adhale< ent?

Java -ollection Questions


Question: Question: Question: Question: Question: Question: Question: Question: Question: Question: Question: Question: Question: Question: What is the 7ollections A0I? What is the (ist interface? What is the 'ector class?

Interview

What is an Iterator interface? Which ,ava!util classes and interfaces support event handling? What is the Gregorian7alendar class? What is the (ocale class? What is the )i ple&i eLone class? What is the %ap interface? What is the highest3level event class of the event3delegation odel? What is the 7ollection interface? What is the )et interface? What is the typical use of $ashtable? I a trying to store an ob,ect using a *ey in a $ashtable! And so e other ob,ect already e#ists in that location, then what will happen? &he e#isting ob,ect will be overwritten? 9r the new ob,ect will be stored elsewhere? What is the difference between the size and capacity of a 'ector?

Question: Question: 7an a vector contain heterogenous ob,ects? Q: What is the -ollections A3I? A: &he 7ollections A0I is a set of classes and interfaces that support operations on collections of ob,ects! ; 2eceived fro 0rasanna Ina ana elluri< Q: What is the (ist interface? A: &he (ist interface provides support for ordered collections of ob,ects! ; 2eceived fro Q: What is the 'ector class? A: &he 'ector class provides the capability to i ple ent a growable array of ob,ects! ; 2eceived fro 0rasanna Ina ana elluri< Q: What is an Iterator interface? A: &he Iterator interface is used to step through the ele ents of a 7ollection ! ; 2eceived fro 0rasanna Ina ana elluri< )0rasanna Ina ana elluri<

Q: Which ,ava!util classes and interfaces support event handling? A: &he "vent9b,ect class and the "vent(istener interface support event processing! ; 2eceived fro 0rasanna Ina ana elluri< Q: What is the 4regorian-alendar class? A: &he Gregorian7alendar provides support for traditional Western calendars ; 2eceived fro 0rasanna Ina ana elluri< Q: What is the (ocale class? A: &he (ocale class is used to tailor progra output to the conventions of a particular geographic, political, or cultural region ! ; 2eceived fro 0rasanna Ina ana elluri< Q: What is the )i ple&i eBone class? A: &he )i ple&i eLone class provides support for a Gregorian calendar ! ; 2eceived fro Q: What is the %ap interface? A: &he %ap interface replaces the JDI D!D Dictionary class and is used associate *eys with values! ; 2eceived fro 0rasanna Ina ana elluri< Q: What is the highest<level event class of the event<delegation odel? A: &he ,ava!util!"vent9b,ect class is the highest3level class in the event3delegation class hierarchy! ; 2eceived fro 0rasanna Ina ana elluri< Q: What is the -ollection interface? A: &he 7ollection interface provides support for the i ple entation of a athe atical bag 3 an unordered collection of ob,ects that ay contain duplicates! ; 2eceived fro 0rasanna Ina ana elluri< Q: What is the )et interface? A: &he )et interface provides ethods for accessing the ele ents of a finite athe atical set! )ets do not allow duplicate ele ents! ; 2eceived fro Q: What is the typical use of $ashtable? 0rasanna Ina ana elluri< 0rasanna Ina ana elluri<

A: Whenever a progra ; 2eceived fro Q:

wants to store a *ey value pair, one can use $ashtable!

)andesh )adhale<

I a trying to store an ob,ect using a *ey in a $ashtable! And so e other ob,ect already e#ists in that location, then what will happen? &he e#isting ob,ect will be overwritten? 0r the new ob,ect will be stored elsewhere? A: &he e#isting ob,ect will be overwritten and thus it will be lost! ; 2eceived fro Q: What is the difference between the size and capacity of a 'ector? A: &he size is the nu ber of ele ents actually stored in the vector, while capacity is the a#i u nu ber of ele ents it can store at a given instance of ti e! ; 2eceived fro )andesh )adhale< Q: -an a vector contain heterogenous ob,ects? A: 6es a 'ector can contain heterogenous ob,ects! 1ecause a 'ector stores everything in ter s of 9b,ect! ; 2eceived fro Q: -an a Array(ist contain heterogenous ob,ects? A: 6es a Array(ist can contain heterogenous ob,ects! 1ecause a Array(ist stores everything in ter s of 9b,ect! ; 2eceived fro Q: What is an enu eration? A: An enu eration is an interface containing ethods for accessing the underlying data structure fro which the enu eration is obtained! It is a construct which collection classes return when you re+uest a collection of all the ob,ects stored in the collection! It allows se+uential access to all the ele ents stored in the collection! ; 2eceived fro )andesh )adhale< Q: -onsidering the basic properties of 'ector and Array(ist, where will you use 'ector and where will you use Array(ist? A: &he basic difference between a 'ector and an Array(ist is that, vector is synchronized while Array(ist is not! &hus whenever there is a possibility of ultiple threads accessing the sa e instance, one should use 'ector! While if not ultiple threads are going to access the sa e instance then use Array(ist! =on synchronized data structure will give better perfor ance than the synchronized one! ; 2eceived fro )andesh )adhale< )andesh )adhale< )andesh )adhale< )andesh )adhale<

Q: -an a vector contain heterogenous ob,ects? A: 6es a 'ector can contain heterogenous ob,ects! 1ecause a 'ector stores everything in ter s of 9b,ect! ; 2eceived fro )andesh )adhale<

You might also like