Sie sind auf Seite 1von 14

Java Interview Questions

Question: What is the difference between an Interface and an Abstract class?



Question:
What is the purpose of garbage collection in Java, and when is it used?
Question:
Describe synchronization in respect to ultithreading!
Question: "#plain different way of using thread?
Question:
What are pass by reference and passby value?
Question:
What is $ash%ap and %ap?
Question:
Difference between $ash%ap and $ash&able?
Question: Difference between 'ector and Array(ist?
Question:
Difference between )wing and Awt?
Question:
What is the difference between a constructor and a ethod?
Question:
What is an Iterators?
Question:
)tate the significance of public, private, protected, default odifiers both
singly and in cobination and state the effect of pac*age relationships on
declared ites +ualified by these odifiers!
Question: What is an abstract class?
Question: What is static in ,ava?
Question:
What is final?
Q:
What is the difference between an Interface and an Abstract class?
A:An Abstract class declares have at least one instance ethod that is declared
abstract which will be ipleented by the subclasses! An abstract class can have
instance ethods that ipleent a default behavior! An Interface can only declare
constants and instance ethods, but cannot ipleent default behavior!
TOP
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 reclaied and reused!
A Java ob,ect is sub,ect to garbage collection when it becoes unreachable to the
progra in which it is used!

TOP
Q:
escribe s!nchroni"ation 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 sae shared variable! &his usually leads to significant
errors!

TOP
Q:
%&plain different wa! of using thread?
A:&he thread could be ipleented by using runnable interface or by inheriting fro
the &hread class! &he forer is ore advantageous, -cause when you are going
for ultiple inheritance!!the only interface can help!

TOP
Q:
What are pass b! reference and passb! value?
A:.ass /y 0eference eans the passing the address itself rather than passing the
value! .assby 'alue eans passing a copy of the value to be passed!

TOP
Q:
What is 'ash(ap and (ap?
A:%ap is Interface and $ashap is class that ipleents that!

TOP
Q:
ifference between 'ash(ap and 'ashTable?
A:&he $ash%ap class is roughly e+uivalent to $ashtable, e#cept that it is
unsynchronized and perits nulls! 1$ash%ap allows null values as *ey and value
whereas $ashtable doesnt allow2! $ash%ap does not guarantee that the order of
the ap will reain constant over tie! $ash%ap is non synchronized and
$ashtable is synchronized!

TOP
Q:
ifference between )ector and Arra!*ist?
A:'ector is synchronized whereas arraylist is not!

TOP
Q:
ifference between +wing and Awt?
A:AW& are heavy3weight coponenets! )wings are light3weight coponents! $ence
swing wor*s faster than AW&!

TOP
Q:
What is the difference between a constructor and a #ethod?
A:A constructor is a eber function of a class that is used to create ob,ects of that
class! It has the sae nae as the class itself, has no return type, and is invo*ed
using the new operator!
A ethod is an ordinary eber function of a class! It has its own nae, a return
type 1which ay be void2, and is invo*ed using the dot operator!

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

TOP
Q:
+tate the significance of public, private, protected, default #odifiers both
singl! and in co#bination and state the effect of pac,age relationships on
declared ite#s -ualified b! these #odifiers$
A:public : .ublic class is visible in other pac*ages, field is visible everywhere 1class
ust be public too2
private : .rivate variables or ethods ay be used only by an instance of the
sae 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 sae 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 1ie, public
private or protected2!It eans that it is visible to all within a particular pac*age!

TOP
Q:
What is an abstract class?
A:Abstract class ust be e#tended5subclassed 1to be useful2! It serves as a
teplate! A class that is abstract ay not be instantiated 1ie, you ay not call its
constructor2, abstract class ay contain static data! Any class with an abstract
ethod is autoatically 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!

TOP
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 iplicitly 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!

TOP
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 1is a constant2!
Question: What if the ain ethod is declared as private?
Question: What if the static odifier is reoved fro the signature of the ain
ethod?
Question: What if I write static public void instead of public static void?
Q:Question:What if I do not provide the )tring array as the arguent to the
ethod?
Question: What is the first arguent of the )tring array in ain ethod?
Question: If I do not provide any arguents on the coand line, then the )tring
array of %ain ethod will be epty of null?
Question: $ow can one prove that the array is not null but epty?
Question: What environent variables do I need to set on y achine in order to
be able to run Java progras?
Question: 7an an application have ultiple classes having ain ethod?
Question: 7an I have ultiple ain ethods in the sae class?
Question: Do I need to iport ,ava!lang pac*age any tie? Why ?
Question: 7an I iport sae pac*age5class twice? Will the J'% load the pac*age
twice at runtie?
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 copiles properly but at runtie it will give :%ain ethod not
public!: essage!

TOP
Q:
What if the static #odifier is re#oved fro# the signature of the #ain
#ethod?
A:.rogra copiles! /ut at runtie throws an error :;o)uch%ethod"rror:!

TOP
Q:
What if I write static public void instead of public static void?
A:.rogra copiles and runs properly!

TOP
Q:
What if I do not provide the +tring arra! as the argu#ent to the #ethod?
A:.rogra copiles but throws a runtie error :;o)uch%ethod"rror:!

TOP
Q:
What is the first argu#ent of the +tring arra! in #ain #ethod?
A:&he )tring array is epty! It does not have any eleent! &his is unli*e 757<<
where the first eleent by default is the progra nae!

TOP
Q:
If I do not provide an! argu#ents on the co##and line, then the +tring
arra! of (ain #ethod will be e#pt! of null?
A:It is epty! /ut not null!

TOP
Q:
'ow can one prove that the arra! is not null but e#pt!?
A:.rint args!length! It will print =! &hat eans it is epty! /ut if it would have been
null then it would have thrown a ;ull.ointer"#ception on attepting to print
args!length!

TOP
Q:
What environ#ent variables do I need to set on #! #achine in order to
be able to run Java progra#s?
A:7(A)).A&$ and .A&$ are the two variables!

TOP
Q:
/an an application have #ultiple classes having #ain #ethod?
A:6es it is possible! While starting the application we ention the class nae to be
run! &he J'% will loo* for the %ain ethod only in the class whose nae you have
entioned! $ence there is not conflict aongst the ultiple classes having ain
ethod!

TOP
Q:
/an I have #ultiple #ain #ethods in the sa#e class?
A:;o the progra fails to copile! &he copiler says that the ain ethod is
already defined in the class!

TOP
Q:
o I need to i#port .ava$lang pac,age an! ti#e? Wh! ?
A:;o! It is by default loaded internally by the J'%!

TOP
Q:
/an I i#port sa#e pac,age0class twice? Will the J)( load the pac,age
twice at runti#e?
A:9ne can iport the sae pac*age or sae class ultiple ties! ;either copiler
nor J'% coplains abt it! And the J'% will internally load the class only once no
atter how any ties you iport the sae class!

TOP
Q:
What are /hec,ed and 1n/hec,ed %&ception?
A:A chec*ed e#ception is soe subclass of "#ception 1or "#ception itself2, e#cluding
class 0untie"#ception and its subclasses!
%a*ing an e#ception chec*ed forces client prograers to deal with the possibility
that the e#ception will be thrown! eg, I9"#ception thrown by
,ava!io!>ileInput)trea-s read12 ethod?
8nchec*ed e#ceptions are 0untie"#ception and any of its subclasses! 7lass "rror
and its subclasses also are unchec*ed! With an unchec*ed e#ception, however, the
copiler doesn-t force client prograers either to catch the
e#ception or declare it in a throws clause! In fact, client prograers ay not
even *now that the e#ception could be thrown! eg,
)tringInde#9ut9f/ounds"#ception thrown by )tring-s charAt12 ethod? 7hec*ed
e#ceptions ust be caught at copile tie! 0untie e#ceptions do not need to
be! "rrors often cannot be!

TOP
Q:
What is Overriding?
A:When a class defines a ethod using the sae nae, return type, and arguents
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!

TOP
Q:
What are different t!pes of inner classes?
A:Nested top-level classes, Member classes, Local classes, Anonymous classes
Nested top-level classes3 If you declare a class within a class and specify the
static odifier, the copiler treats the class ,ust li*e any other top3level class!
Any class outside the declaring class accesses the nested class with the declaring
class nae acting siilarly to a pac*age! eg, outer!inner! &op3level inner classes
iplicitly have access only to static variables!&here can also be inner interfaces!
All of these are of the nested top3level variety!
Member classes 3 %eber inner classes are ,ust li*e other eber ethods and
eber variables and access to the eber class is restricted, ,ust li*e ethods
and variables! &his eans a public eber class acts siilarly to a nested top3
level class! &he priary difference between eber classes and nested top3level
classes is that eber 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 ipleent a
ore publicly available interface!/ecause local classes are not ebers, the
odifiers public, protected, private, and static are not usable!
Anonymous classes 3 Anonyous inner classes e#tend local inner classes one
level further! As anonyous classes have no nae, you cannot provide a
constructor!
Question:Are the iports chec*ed for validity at copile tie? e!g! will the code
containing an iport such as ,ava!lang!A/7D copile?
Question:Does iporting a pac*age iports the subpac*ages as well? e!g! Does
iporting co!%y&est!@ also iport co!%y&est!8nit&ests!@?
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 top level class be private or protected?
Question:What type of paraeter passing does Java support?
Question:.riitive 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 ipleent?
Question:$ow can I custoize the seralization process? i!e! how can one have a
control over the serialization process?
Question:What is the coon usage of serialization?
Question:What is "#ternalizable interface?
Question:What happens to the ob,ect references included in the ob,ect serialized?
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? Are these
fields serialized as a part of each serialized ob,ect?

Q:
Are the i#ports chec,ed for validit! at co#pile ti#e? e$g$ will the code
containing an i#port such as .ava$lang$A2/ co#pile?
A:6es the iports are chec*ed for the seantic validity at copile tie! &he code
containing above line of iport will not copile! It will throw an error saying,can
not resolve sybol
sybol : class A/7D
location: pac*age io
iport ,ava!io!A/7D4

TOP
Q:
oes i#porting a pac,age i#ports the subpac,ages as well? e$g$ oes
i#porting co#$(!Test$3 also i#port co#$(!Test$1nitTests$3?
A:;o you will have to iport the subpac*ages e#plicitly! Iporting co!%y&est!@ will
iport classes in the pac*age %y&est only! It will not iport any class in any of
it-s subpac*age!

TOP
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 nae! We do not
initialize it! /ut defining eans declaration < initialization!
e!g )tring s4 is ,ust a declaration while )tring s A new )tring 1:abcd:24 9r )tring s
A :abcd:4 are both definitions!

TOP
Q:
What is the default value of an ob.ect reference declared as an instance
variable?
A:null unless we define it e#plicitly!

TOP
Q:
/an a top level class be private or protected?
A:;o! A top 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 top level class is declared as private the copiler will coplain that the
:odifier private is not allowed here:! &his eans that a top level class can not be
private! )ae is the case with protected!

TOP
Q:
What t!pe of para#eter passing does Java support?
A:In Java the arguents are always passed by value !

TOP
Q:
Pri#itive data t!pes are passed b! reference or pass b! value?
A:.riitive data types are passed by value!

TOP
Q:
Ob.ects are passed b! value or b! 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 paraeter copy both refer
to the sae ob,ect !

TOP
Q:
What is seriali"ation?
A:)erialization is a echanis by which you can save the state of an ob,ect by
converting it to a byte strea!

TOP
Q:
'ow do I seriali"e an ob.ect to a file?
A:&he class whose instances are to be serialized should ipleent 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!

TOP
Q:
Which #ethods of +eriali"able interface should I i#ple#ent?
A:&he serializable interface is an epty interface, it does not contain any ethods!
)o we do not ipleent any ethods!

TOP
Q:
'ow can I custo#i"e the serali"ation process? i$e$ how can one have a
control over the seriali"ation process?
A:6es it is possible to have control over serialization process! &he class should
ipleent "#ternalizable interface! &his interface contains two ethods naely
read"#ternal and write"#ternal! 6ou should ipleent these ethods and write
the logic for custoizing the serialization process!

TOP
Q:
What is the co##on usage of seriali"ation?
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!

TOP
Q:
What is %&ternali"able 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 ipleents this interface, you can custoize the serialization
process by ipleenting these ethods!

TOP
Q:
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
deterines 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!

TOP
Q:
What one should ta,e care of while seriali"ing 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!

TOP
Q:
What happens to the static fields of a class during seriali"ation? Are these
fields seriali"ed as a part of each seriali"ed ob.ect?
A:6es the static fields do get serialized! If the static field is an ob,ect then it ust
have ipleented )erializable interface! &he static fields are serialized as a part
of every ob,ect! /ut the coonness of the static fields across all the instances is
aintained even after serialization!

Question:$ow are 9bserver and 9bservable used?
Question:
What is synchronization and why is it iportant?
Question:$ow does Java handle integer overflows and underflows?
Question:Does garbage collection guarantee that a progra will not run out of
eory?
Question:What is the difference between preeptive scheduling and tie 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 stateent and a do stateent?
Question:What is the difference between static and non3static variables?
Question:$ow are this12 and super12 used with constructors?
Question:What are synchronized ethods and synchronized stateents?
Q:
'ow are Observer and Observable 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 update12 ethod of each of its
observers to notify the observers that it has changed state! &he 9bserver interface
is ipleented by ob,ects that observe 9bservable ob,ects!

TOP
Q:
What is s!nchroni"ation and wh! 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!

TOP
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!

TOP
Q:
oes garbage collection guarantee that a progra# will not run out of
#e#or!?
A:Barbage collection does not guarantee that a progra will not run out of
eory! It is possible for progras to use up eory resources faster than they
are garbage collected! It is also possible for progras to create ob,ects that are
not sub,ect to garbage collection
!

TOP
Q:
What is the difference between pree#ptive scheduling and ti#e slicing?
A:8nder preeptive scheduling, the highest priority tas* e#ecutes until it enters
the waiting or dead states or a higher priority tas* coes into e#istence! 8nder
tie slicing, a tas* e#ecutes for a predefined slice of tie and then reenters the
pool of ready tas*s! &he scheduler then deterines which tas* should e#ecute
ne#t, based on priority and other factors!

TOP
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!

TOP
Q:
What is the purpose of finali"ation?
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!

TOP
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!

TOP
Q:
What is the difference between a while state#ent and a do state#ent?
A:A while stateent chec*s at the beginning of a loop to see whether the ne#t
loop iteration should occur! A do stateent chec*s at the end of a loop to see
whether the ne#t iteration of a loop should occur! &he do stateent will
always e#ecute the body of a loop at least once!

TOP
Q:
What is the difference between static and non4static 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!

TOP
Q:
'ow are this56 and super56 used with constructors?
A:9this12 is used to invo*e a constructor of the sae class! super12 is used to
invo*e a superclass constructor!

TOP
Q:
What are s!nchroni"ed #ethods and s!nchroni"ed 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 stateents are siilar to synchronized
ethods! A synchronized stateent can only be e#ecuted after a thread has
ac+uired the loc* for the ob,ect or class referenced in the synchronized stateent!

C TOP
Question:$ow are 9bserver and 9bservable used?
Question:
What is synchronization and why is it iportant?
Question:$ow does Java handle integer overflows and underflows?
Question:Does garbage collection guarantee that a progra will not run out of
eory?
Question:What is the difference between preeptive scheduling and tie 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 stateent and a do stateent?
Question:What is the difference between static and non3static variables?
Question:$ow are this12 and super12 used with constructors?
Question:What are synchronized ethods and synchronized stateents?
Q:
'ow are Observer and Observable 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 update12 ethod of each of its
observers to notify the observers that it has changed state! &he 9bserver interface
is ipleented by ob,ects that observe 9bservable ob,ects!

TOP
Q:
What is s!nchroni"ation and wh! 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!

TOP
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!

TOP
Q:
oes garbage collection guarantee that a progra# will not run out of
#e#or!?
A:Barbage collection does not guarantee that a progra will not run out of
eory! It is possible for progras to use up eory resources faster than they
are garbage collected! It is also possible for progras to create ob,ects that are
not sub,ect to garbage collection
!

TOP
Q:
What is the difference between pree#ptive scheduling and ti#e slicing?
A:8nder preeptive scheduling, the highest priority tas* e#ecutes until it enters
the waiting or dead states or a higher priority tas* coes into e#istence! 8nder
tie slicing, a tas* e#ecutes for a predefined slice of tie and then reenters the
pool of ready tas*s! &he scheduler then deterines which tas* should e#ecute
ne#t, based on priority and other factors!

TOP
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!

TOP
Q:
What is the purpose of finali"ation?
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!

TOP
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!

TOP
Q:
What is the difference between a while state#ent and a do state#ent?
A:A while stateent chec*s at the beginning of a loop to see whether the ne#t
loop iteration should occur! A do stateent chec*s at the end of a loop to see
whether the ne#t iteration of a loop should occur! &he do stateent will
always e#ecute the body of a loop at least once!

TOP
Q:
What is the difference between static and non4static 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!

TOP
Q:
'ow are this56 and super56 used with constructors?
A:9this12 is used to invo*e a constructor of the sae class! super12 is used to
invo*e a superclass constructor!

TOP
Q:
What are s!nchroni"ed #ethods and s!nchroni"ed 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 stateents are siilar to synchronized
ethods! A synchronized stateent can only be e#ecuted after a thread has
ac+uired the loc* for the ob,ect or class referenced in the synchronized stateent!

Das könnte Ihnen auch gefallen