Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
http://www.tutorialspoint.com/java/java_interview_questions.htm
Dear readers, t hese Java Interview Questio ns have been designed especially t o get you
acquaint ed wit h t he nat ure of quest ions you may encount er during your int erview for t he subject of
Java Pro gramming Language. As per my experience, good int erviewers hardly planned t o ask
any part icular quest ion during your int erview, normally quest ions st art wit h some basic concept of
t he subject and lat er t hey cont inue based on furt her discussion and what you answer:
What do you know about Java?
Java is a high-level programming language originally developed by Sun Microsyst ems and released in
1995. Java runs on a variet y of plat forms, such as Windows, Mac OS, and t he various versions of UNIX.
Q: What are t he support ed plat forms by Java Programming Language?
A: Java runs on a variet y of plat forms, such as Windows, Mac OS, and t he various versions of
UNIX/Linux like HP-Unix, Sun Solaris, Redhat Linux, Ubunt u, Cent OS, et c.
Q: What are the suppo rted platfo rms by Java Pro gramming Language?
A: Java runs on a variet y of plat forms, such as Windows, Mac OS, and t he various versions of
UNIX/Linux like HP-Unix, Sun Solaris, Redhat Linux, Ubunt u, Cent OS, et c.
Q: List any five features o f Java?
A: Some feat ures include Object Orient ed, Plat form Independent , Robust , Int erpret ed, Mult it hreaded
Q: Why is Java Architectural Neutral?
A: It s compiler generat es an archit ect ure-neut ral object file format , which makes t he compiled
code t o be execut able on many processors, wit h t he presence of Java runt ime syst em.
Q: Ho w Java enabled High Perfo rmance?
A: Java uses Just -In-Time compiler t o enable high performance. Just -In-Time compiler is a program
t hat t urns Java byt ecode, which is a program t hat cont ains inst ruct ions t hat must be int erpret ed
int o inst ruct ions t hat can be sent direct ly t o t he processor.
Q: Why Java is co nsidered dynamic?
A: It is designed t o adapt t o an evolving environment . Java programs can carry ext ensive amount of
run-t ime informat ion t hat can be used t o verify and resolve accesses t o object s on run-t ime.
Q: What is Java Virtual Machine and ho w it is co nsidered in co ntext o f Javas
platfo rm independent feature?
A: When Java is compiled, it is not compiled int o plat form specific machine, rat her int o plat form
independent byt e code. This byt e code is dist ribut ed over t he web and int erpret ed by virt ual
Machine (JVM) on whichever plat form it is being run.
Q: List two Java IDEs?
A: Net beans, Eclipse, et c.
Q: List so me Java keywo rds(unlike C, C++ keywo rds)?
A: Some Java keywords are import , super, finally, et c.
Q: What do yo u mean by Object?
A: Object is a runt ime ent it y and it s st at e is st ored in fields and behavior is shown via met hods.
Met hods operat e on an object 's int ernal st at e and serve as t he primary mechanism for object -t oobject communicat ion.
Q: Define class?
A: A class is a blue print from which individual object s are creat ed. A class can cont ain fields and
met hods t o describe t he behavior of an object .
Q: What kind o f variables a class can co nsist o f?
A: A class consist of Local variable, inst ance variables and class variables.
Q: What is a Lo cal Variable
A: Variables defined inside met hods, const ruct ors or blocks are called local variables. The variable
will be declared and init ialized wit hin t he met hod and it will be dest royed when t he met hod has
complet ed.
Q: What is a Instance Variable
A: Inst ance variables are variables wit hin a class but out side any met hod. These variables are
inst ant iat ed when t he class is loaded.
Q: What is a Class Variable
A: These are variables declared wit h in a class, out side any met hod, wit h t he st at ic keyword.
Q: What is Singleto n class?
A: Singlet on class cont rol object creat ion, limit ing t he number t o one but allowing t he flexibilit y t o
creat e more object s if t he sit uat ion changes.
Q: What do yo u mean by Co nstructo r?
A: Const ruct or get s invoked when a new object is creat ed. Every class has a const ruct or. If we do
not explicit ly writ e a const ruct or for a class t he java compiler builds a default const ruct or for t hat
class.
Q: List the three steps fo r creating an Object fo r a class?
A: An Object is first declared, t hen inst ant iat ed and t hen it is init ialized.
Q: What is the default value o f byte datatype in Java?
A: Default value of byt e dat at ype is 0.
Q: What is the default value o f flo at and do uble datatype in Java?
A: Default value of float and double dat at ype in different as compared t o C/C++. For float it s 0.0f
and for double it s 0.0d
Q: When a byte datatype is used?
A: This dat a t ype is used t o save space in large arrays, mainly in place of int egers, since a byt e is
four t imes smaller t han an int .
Q: What is a static variable?
A: Class variables also known as st at ic variables are declared wit h t he st at ic keyword in a class, but
out side a met hod, const ruct or or a block.
Q: What do yo u mean by Access Mo difier?
A: Java provides access modifiers t o set access levels for classes, variables, met hods and
const ruct ors. A member has package or default accessibilit y when no accessibilit y modifier is
specified.
Q: What is pro tected access mo difier?
A: Variables, met hods and const ruct ors which are declared prot ect ed in a superclass can be
accessed only by t he subclasses in ot her package or any class wit hin t he package of t he prot ect ed
members' class.
Q: What do yo u mean by synchro nized No n Access Mo difier?
A: Java provides t hese modifiers for providing funct ionalit ies ot her t han Access Modifiers,
synchronized used t o indicat e t hat a met hod can be accessed by only one t hread at a t ime.
Q: Acco rding to Java Operato r precedence, which o perato r is co nsidered to be with
highest precedence?
A: Post fix operat ors i.e () [] . is at t he highest precedence.
Q: Variables used in a switch statement can be used with which datatypes?
A: Variables used in a swit ch st at ement can only be a byt e, short , int , or char.
Q: When parseInt() metho d can be used?
A: This met hod is used t o get t he primit ive dat a t ype of a cert ain St ring.
Q: Why is String class co nsidered immutable?
A: The St ring class is immut able, so t hat once it is creat ed a St ring object cannot be changed. Since
St ring is immut able it can safely be shared bet ween many t hreads ,which is considered very
import ant for mult it hreaded programming.
Q: Why is StringBuffer called mutable?
A: The St ring class is considered as immut able, so t hat once it is creat ed a St ring object cannot be
changed. If t here is a necessit y t o make alot of modificat ions t o St rings of charact ers t hen
St ringBuffer should be used.
Q: What is the difference between StringBuffer and StringBuilder class?
A: Use St ringBuilder whenever possible because it is fast er t han St ringBuffer. But , if t hread safet y is
necessary t hen use St ringBuffer object s.
Q: Which package is used fo r pattern matching with regular expressio ns?
A: java.ut il.regex package is used for t his purpose.
Q: java.util.regex co nsists o f which classes?
A: java.ut il.regex consist s of t hree classes: Pat t ern class, Mat cher class and
Pat t ernSynt axExcept ion class.
Q: What is finalize() metho d?
A: It is possible t o define a met hod t hat will be called just before an object 's final dest ruct ion by t he
garbage collect or. This met hod is called finalize( ), and it can be used t o ensure t hat an object
t erminat es cleanly.
Q: What is an Exceptio n?
A: An except ion is a problem t hat arises during t he execut ion of a program. Except ions are caught
by handlers posit ioned along t he t hread's met hod invocat ion st ack.
Q: What do yo u mean by Checked Exceptio ns?
A: It is an except ion t hat is t ypically a user error or a problem t hat cannot be foreseen by t he
programmer. For example, if a file is t o be opened, but t he file cannot be found, an except ion occurs.
These except ions cannot simply be ignored at t he t ime of compilat ion.
Q: Explain Runtime Exceptio ns?
A: It is an except ion t hat occurs t hat probably could have been avoided by t he programmer. As
opposed t o checked except ions, runt ime except ions are ignored at t he t ime of compliat ion.
A: It is used t o init ialize t he st at ic dat a member, It is excut ed before main met hod at t he t ime of
classloading.
Q: Define co mpo sitio n?
A: Holding t he reference of t he ot her class wit hin some ot her class is known as composit ion.
Q: What is functio n o verlo ading?
A: If a class has mult iple funct ions by same name but different paramet ers, it is known as Met hod
Overloading.
Q: What is functio n o verriding?
A: If a subclass provides a specific implement at ion of a met hod t hat is already provided by it s
parent class, it is known as Met hod Overriding.
Q: Difference between Overlo ading and Overriding?
A: Met hod overloading increases t he readabilit y of t he program. Met hod overriding provides t he
specific implement at ion of t he met hod t hat is already provided by it s super class paramet er must
be different in case of overloading, paramet er must be same in case of overriding.
Q: What is final class?
A: Final classes are creat ed so t he met hods implement ed by t hat class cannot be overridden. It
cant be inherit ed.
Q: What is NullPo interExceptio n?
A: A NullPoint erExcept ion is t hrown when calling t he inst ance met hod of a null object , accessing or
modifying t he field of a null object et c.
Q: What are the ways in which a thread can enter the waiting state?
A: A t hread can ent er t he wait ing st at e by invoking it s sleep() met hod, by blocking on IO, by
unsuccessfully at t empt ing t o acquire an object 's lock, or by invoking an object 's wait () met hod. It
can also ent er t he wait ing st at e by invoking it s (deprecat ed) suspend() met hod.
Q: Ho w do es multi-threading take place o n a co mputer with a single CPU?
A: The operat ing syst em's t ask scheduler allocat es execut ion t ime t o mult iple t asks. By quickly
swit ching bet ween execut ing t asks, it creat es t he impression t hat t asks execut e sequent ially.
Q: What invo kes a thread's run() metho d?
A: Aft er a t hread is st art ed, via it s st art () met hod of t he Thread class, t he JVM invokes t he t hread's
run() met hod when t he t hread is init ially execut ed.
Q: Do es it matter in what o rder catch statements fo r FileNo tFo undExceptio n and
IOExceptio n are written?
A: Yes, it does. The FileNoFoundExcept ion is inherit ed from t he IOExcept ion. Except ion's subclasses
have t o be caught first .
Q: What is the difference between yielding and sleeping?
A: When a t ask invokes it s yield() met hod, it ret urns t o t he ready st at e. When a t ask invokes it s
sleep() met hod, it ret urns t o t he wait ing st at e.
Q: Why Vecto r class is used?
A: The Vect or class provides t he capabilit y t o implement a growable array of object s. Vect or proves
t o be very useful if you don't know t he size of t he array in advance, or you just need one t hat can
change sizes over t he lifet ime of a program.
Q: Ho w many bits are used to represent Unico de, ASCII, UT F-16, and UT F-8
characters?
A: Unicode requires 16 bit s and ASCII require 7 bit s. Alt hough t he ASCII charact er set uses only 7
bit s, it is usually represent ed as 8 bit s. UTF-8 represent s charact ers using 8, 16, and 18 bit pat t erns.
UTF-16 uses 16-bit and larger bit pat t erns.
Q: What are Wrapper classes?
A: These are classes t hat allow primit ive t ypes t o be accessed as object s. Example: Int eger,
Charact er, Double, Boolean et c.
Q: What is the difference between a Windo w and a Frame?
A: The Frame class ext ends Window t o define a main applicat ion window t hat can have a menu bar.
Q: Which package has light weight co mpo nents?
A: javax.Swing package. All component s in Swing, except JApplet , JDialog, JFrame and JWindow are
light weight component s.
Q: What is the difference between the paint() and repaint() metho ds?
A: The paint () met hod support s paint ing via a Graphics object . The repaint () met hod is used t o
cause paint () t o be invoked by t he AWT paint ing t hread.
Q: What is the purpo se o f File class?
A: It is used t o creat e object s t hat provide access t o t he files and direct ories of a local file syst em.
Q: What is the difference between the Reader/Writer class hierarchy and the
InputStream/OutputStream class hierarchy?
A: The Reader/Writ er class hierarchy is charact er-orient ed, and t he Input St ream/Out put St ream class
hierarchy is byt e-orient ed.
Q: Which class sho uld yo u use to o btain design info rmatio n abo ut an o bject?
A: The Class class is used t o obt ain informat ion about an object 's design and java.lang.Class class
inst ance represent classes, int erfaces in a running Java applicat ion.
Q: What is the difference between static and no n-static variables?
A: A st at ic variable is associat ed wit h t he class as a whole rat her t han wit h specific inst ances of a
class. Non-st at ic variables t ake on unique values wit h each object inst ance.
Q: What is Serializatio n and deserializatio n?
A: Serializat ion is t he process of writ ing t he st at e of an object t o a byt e st ream. Deserializat ion is
t he process of rest oring t hese object s.
Q: What are use cases?
A: It is part of t he analysis of a program and describes a sit uat ion t hat a program might encount er
and what behavior t he program should exhibit in t hat circumst ance.
Q: Explain the use o f sublass in a Java pro gram?
A: Sub class inherit s all t he public and prot ect ed met hods and t he implement at ion. It also inherit s all
t he default modifier met hods and t heir implement at ion.
Q: Ho w to add menusho rtcut to menu item?
A: If t here is a but t on inst ance called b1, you may add menu short cut by calling b1.set Mnemonic('F'),
so t he user may be able t o use Alt +F t o click t he but t on.
Q: Can yo u write a Java class that co uld be used bo th as an applet as well as an
applicatio n?
A: Yes, just add a main() met hod t o t he applet .
A: Socket s are flexible and sufficient . Efficient socket based programming can be easily
implement ed for general communicat ions. It cause low net work t raffic.
Q: Disadvantages o f Java So ckets?
A: Socket based communicat ions allows only t o send packet s of raw dat a bet ween applicat ions.
Bot h t he client -side and server-side have t o provide mechanisms t o make t he dat a useful in any
way.
Q: Which class is used by server applicatio ns to o btain a po rt and listen fo r client
requests?
A: java.net .ServerSocket class is used by server applicat ions t o obt ain a port and list en for client
request s
Q: Which class represents the so cket that bo th the client and server use to
co mmunicate with each o ther?
A: java.net .Socket class represent s t he socket t hat bot h t he client and server use t o communicat e
wit h each ot her.
Q: Why Generics are used in Java?
A: Generics provide compile-t ime t ype safet y t hat allows programmers t o cat ch invalid t ypes at
compile t ime. Java Generic met hods and generic classes enable programmers t o specify, wit h a
single met hod declarat ion, a set of relat ed met hods or, wit h a single class declarat ion, a set of
relat ed t ypes.
Q: What enviro nment variables do I need to set o n my machine in o rder to be able
to run Java pro grams?
A: CLASSPATH and PATH are t he t wo variables.
Q: Is there any need to impo rt java.lang package?
A: No, t here is no need t o import t his package. It is by default loaded int ernally by t he JVM.
Q: What is Nested to p-level class?
A: If a class is declared wit hin a class and specify t he st at ic modifier, t he compiler t reat s t he class
just like any ot her t op-level class. Nest ed t op-level class is an Inner class.
Q: What is Externalizable interface?
A: Ext ernalizable is an int erface which cont ains t wo met hods readExt ernal and writ eExt ernal. These
met hods give you a cont rol over t he serializat ion mechanism.
Q: If System.exit (0); is written at the end o f the try blo ck, will the finally blo ck
still execute?
A: No in t his case t he finally block will not execut e because when you say Syst em.exit (0); t he
cont rol immediat ely goes out of t he program, and t hus finally never execut es.
Q: What is daemo n thread?
A: Daemon t hread is a low priorit y t hread, which runs int ermit t ent ly in t he back ground doing t he
garbage collect ion operat ion for t he java runt ime syst em.
Q: Which metho d is used to create the daemo n thread?
A: set Daemon met hod is used t o creat e a daemon t hread.
Q: Which metho d must be implemented by all threads?
A: All t asks must implement t he run() met hod
Q: What is the Grego rianCalendar class?
A: The GregorianCalendar provides support for t radit ional West ern calendars
Q: What is the SimpleT imeZo ne class?
A: The SimpleTimeZone class provides support for a Gregorian calendar .
Q: What is the difference between the size and capacity o f a Vecto r?
A: The size is t he number of element s act ually st ored in t he vect or, while capacit y is t he maximum
number of element s it can st ore at a given inst ance of t ime.
Q: Can a vecto r co ntain hetero geno us o bjects?
A: Yes a Vect or can cont ain het erogenous object s. Because a Vect or st ores everyt hing in t erms of
Object .
Q: What is an enumeratio n?
A: An enumerat ion is an int erface cont aining met hods for accessing t he underlying dat a st ruct ure
from which t he enumerat ion is obt ained. It allows sequent ial access t o all t he element s st ored in t he
collect ion.
Q: What is difference between Path and Classpath?
A: Pat h and Classpat h are operat ing syst em level environment variales. Pat h is defines where t he
syst em can find t he execut ables(.exe) files and classpat h is used t o specify t he locat ion of .class
files.
Q: Can a class declared as private be accessed o utside it's package?
A: No, it 's not possible t o accessed out side it 's package.
Q: What are the restrictio n impo sed o n a static metho d o r a static blo ck o f co de?
A: A st at ic met hod should not refer t o inst ance variables wit hout creat ing an inst ance and cannot
use "t his" operat or t o refer t he inst ance.
Q: Can an Interface extend ano ther Interface?
A: Yes an Int erface can inherit anot her Int erface, for t hat mat t er an Int erface can ext end more t han
one Int erface.
Q: Which o bject o riented Co ncept is achieved by using o verlo ading and o verriding?
A: Polymorphism
Q: What is an o bject's lo ck and which o bject's have lo cks?
A: An object 's lock is a mechanism t hat is used by mult iple t hreads t o obt ain synchronized access
t o t he object . A t hread may execut e a synchronized met hod of an object only aft er it has acquired
t he object 's lock.
Q: What is Do wncasting?
A: It is t he cast ing from a general t o a more specific t ype, i.e. cast ing down t he hierarchy.
Q: What are o rder o f precedence and asso ciativity and ho w are they used?
A: Order of precedence det ermines t he order in which operat ors are evaluat ed in expressions.
Associat it y det ermines whet her an expression is evaluat ed left -t o-right or right -t o-left .
Q: If a metho d is declared as pro tected, where may the metho d be accessed?
A: A prot ect ed met hod may only be accessed by classes or int erfaces of t he same package or by
subclasses of t he class in which it is declared.
Q: What is the difference between inner class and nested class?
A: When a class is defined wit hin a scope of anot her class, t hen it becomes inner class. If t he access
modifier of t he inner class is st at ic, t hen it becomes nest ed class.
Q: What restrictio ns are placed o n metho d o verriding?
A: Overridden met hods must have t he same name, argument list , and ret urn t ype. The overriding
met hod may not limit t he access of t he met hod it overrides.
Q: What is co nstructo r chaining and ho w is it achieved in Java?
A: A child object const ruct or always first needs t o const ruct it s parent . In Java it is done via an
implicit call t o t he no-args const ruct or as t he first st at ement .
Q: Can a do uble value be cast to a byte?
A: Yes, a double value can be cast t o a byt e.
Q: Ho w do es a try statement determine which catch clause sho uld be used to
handle an exceptio n?
A: When an except ion is t hrown wit hin t he body of a t ry st at ement , t he cat ch clauses of t he t ry
st at ement are examined in t he order in which t hey appear. The first cat ch clause t hat is capable of
handling t he except ion is execut ed. The remaining cat ch clauses are ignored.
Q: What will be the default values o f all the elements o f an array defined as an
instance variable?
A: If t he array is an array of primit ive t ypes, t hen all t he element s of t he array will be init ialized t o t he
default value corresponding t o t hat primit ive t ype.
What is Next ?
Furt her, you can go t hrough your past assignment s you have done wit h t he subject and make sure
you are able t o speak confident ly on t hem. If you are fresher t hen int erviewer does not expect you
will answer very complex quest ions, rat her you have t o make your basics concept s very st rong.
Second it really doesn't mat t er much if you could not answer few quest ions but it mat t ers t hat
what ever you answered, you must have answered wit h confidence. So just feel confident during your
int erview. We at t ut orialspoint wish you best luck t o have a good int erviewer and all t he very best
for your fut ure endeavor. Cheers :-)