Sei sulla pagina 1di 16

JAVA INTERVIEW QUESTIONS

http://www.tutorialspoint.com/java/java_interview_questions.htm

Copyright tutorials point.com

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.

Q: Which are the two subclasses under Exceptio n class?


A: The Except ion class has t wo main subclasses : IOExcept ion class and Runt imeExcept ion Class.
Q: When thro ws keywo rd is used?
A: If a met hod does not handle a checked except ion, t he met hod must declare it using t he
t hrowskeyword. The t hrows keyword appears at t he end of a met hod's signat ure.
Q: When thro w keywo rd is used?
A: An except ion can be t hrown, eit her a newly inst ant iat ed one or an except ion t hat you just caught ,
by using t hrow keyword.
Q: Ho w finally used under Exceptio n Handling?
A: The finally keyword is used t o creat e a block of code t hat follows a t ry block. A finally block of
code always execut es, whet her or not an except ion has occurred.
Q: What things sho uld be kept in mind while creating yo ur o wn exceptio ns in Java?
A: While creat ing your own except ion:
All except ions must be a child of Throwable.
If you want t o writ e a checked except ion t hat is aut omat ically enforced by t he Handle or
Declare Rule, you need t o ext end t he Except ion class.
You want t o writ e a runt ime except ion, you need t o ext end t he Runt imeExcept ion class.
Q: Define Inheritance?
A: It is t he process where one object acquires t he propert ies of anot her. Wit h t he use of inherit ance
t he informat ion is made manageable in a hierarchical order.
Q: When super keywo rd is used?
A: If t he met hod overrides one of it s superclass's met hods, overridden met hod can be invoked
t hrough t he use of t he keyword super. It can be also used t o refer t o a hidden field
Q: What is Po lymo rphism?
A: Polymorphism is t he abilit y of an object t o t ake on many forms. The most common use of
polymorphism in OOP occurs when a parent class reference is used t o refer t o a child class object .
Q: What is Abstractio n?
A: It refers t o t he abilit y t o make a class abst ract in OOP. It helps t o reduce t he complexit y and also
improves t he maint ainabilit y of t he syst em.
Q: What is Abstract class
A: These classes cannot be inst ant iat ed and are eit her part ially implement ed or not at all
implement ed. This class cont ains one or more abst ract met hods which are simply met hod
declarat ions wit hout a body.
Q: When Abstract metho ds are used?
A: If you want a class t o cont ain a part icular met hod but you want t he act ual implement at ion of t hat
met hod t o be det ermined by child classes, you can declare t he met hod in t he parent class as
abst ract .
Q: What is Encapsulatio n?
A: It is t he t echnique of making t he fields in a class privat e and providing access t o t he fields via
public met hods. If a field is declared privat e, it cannot be accessed by anyone out side t he class,
t hereby hiding t he fields wit hin t he class. Therefore encapsulat ion is also referred t o as dat a hiding.

Q: What is the primary benefit o f Encapsulatio n?


A: The main benefit of encapsulat ion is t he abilit y t o modify our implement ed code wit hout breaking
t he code of ot hers who use our code. Wit h t his Encapsulat ion gives maint ainabilit y, flexibilit y and
ext ensibilit y t o our code.
Q: What is an Interface?
A: An int erface is a collect ion of abst ract met hods. A class implement s an int erface, t hereby
inherit ing t he abst ract met hods of t he int erface.
Q: Give so me features o f Interface?
A: It includes:
Int erface cannot be inst ant iat ed
An int erface does not cont ain any const ruct ors.
All of t he met hods in an int erface are abst ract .
Q: Define Packages in Java?
A: A Package can be defined as a grouping of relat ed t ypes(classes, int erfaces, enumerat ions and
annot at ions ) providing access prot ect ion and name space management .
Q: Why Packages are used?
A: Packages are used in Java in-order t o prevent naming conflict s, t o cont rol access, t o make
searching/locat ing and usage of classes, int erfaces, enumerat ions and annot at ions, et c., easier.
Q: What do yo u mean by Multithreaded pro gram?
A: A mult it hreaded program cont ains t wo or more part s t hat can run concurrent ly. Each part of such
a program is called a t hread, and each t hread defines a separat e pat h of execut ion.
Q: What are the two ways in which T hread can be created?
A: Thread can be creat ed by: implement ing Runnable int erface, ext ending t he Thread class.
Q: What is an applet?
A: An applet is a Java program t hat runs in a Web browser. An applet can be a fully funct ional Java
applicat ion because it has t he ent ire Java API at it s disposal.
Q: An applet extend which class?
A: An applet ext ends java.applet .Applet class.
Q: Explain garbage co llectio n in Java?
A: It uses garbage collect ion t o free t he memory. By cleaning t hose object s t hat is no longer
reference by any of t he program.
Q: Define immutable o bject?
A: An immut able object cant be changed once it is creat ed.
Q: Explain the usage o f this() with co nstructo rs?
A: It is used wit h variables or met hods and used t o call const ruct er of same class.
Q: Explain Set Interface?
A: It is a collect ion of element which cannot cont ain duplicat e element s. The Set int erface cont ains
only met hods inherit ed from Collect ion and adds t he rest rict ion t hat duplicat e element s are
prohibit ed.
Q: Explain T reeSet?

A: It is a Set implement ed when we want element s in a sort ed order.


Q: What is Co mparable Interface?
A: It is used t o sort collect ions and arrays of object s using t he collect ions.sort () and java.ut ils. The
object s of t he class implement ing t he Comparable int erface can be ordered.
Q: Difference between thro w and thro ws?
A: It includes:
Throw is used t o t rigger an except ion where as t hrows is used in declarat ion of except ion.
Wit hout t hrows, Checked except ion cannot be handled where as checked except ion can be
propagat ed wit h t hrows.
Q: Explain the fo llo wing line used under Java Pro gram:
public st at ic void main (St ring args[ ])
A: The following shows t he explanat ion individually:
public: it is t he access specifier.
st at ic: it allows main() t o be called wit hout inst ant iat ing a part icular inst ance of a class.
void: it affirns t he compiler t hat no value is ret urned by main().
main(): t his met hod is called at t he beginning of a Java program.
St ring args[ ]: args paramet er is an inst ance array of class St ring
Q: Define JRE i.e. Java Runtime Enviro nment?
A: Java Runt ime Environment is an implement at ion of t he Java Virt ual Machine which execut es Java
programs. It provides t he minimum requirement s for execut ing a Java applicat ion;
Q: What is JAR file?
A: JAR files is Java Archive fles and it aggregat es many files int o one. It holds Java classes in a library.
JAR files are built on ZIP file format and have .jar file ext ension.
Q: What is a WAR file?
A: This is Web Archive File and used t o st ore XML, java classes, and JavaServer pages. which is used
t o dist ribut e a collect ion of JavaServer Pages, Java Servlet s, Java classes, XML files, st at ic Web pages
et c.
Q: Define JIT co mpiler?
A: It improves t he runt ime performance of comput er programs based on byt ecode.
Q: What is the difference between o bject o riented pro gramming language and
o bject based pro gramming language?
A: Object based programming languages follow all t he feat ures of OOPs except Inherit ance.
JavaScript is an example of object based programming languages
Q: What is the purpo se o f default co nstructo r?
A: The java compiler creat es a default const ruct or only if t here is no const ruct or in t he class.
Q: Can a co nstructo r be made final?
A: No, t his is not possible.
Q: What is static blo ck?

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 .

Q: What is the difference between Swing and AWT co mpo nents?


A: AWT component s are heavy-weight , whereas Swing component s are light weight . Heavy weight
component s depend on t he local windowing t oolkit . For example, java.awt .But t on is a heavy weight
component , when it is running on t he Java plat form for Unix plat form, it maps t o a real Mot if but t on.
Q: What's the difference between co nstructo rs and o ther metho ds?
A: Const ruct ors must have t he same name as t he class and can not ret urn a value. They are only
called once while regular met hods could be called many t imes.
Q: Is there any limitatio n o f using Inheritance?
A: Yes, since inherit ance inherit s everyt hing from t he super class and int erface, it may make t he
subclass t oo clust ering and somet imes error-prone when dynamic overriding or dynamic overloading
in some sit uat ion.
Q: When is the ArraySto reExceptio n thro wn?
A: When copying element s bet ween different arrays, if t he source or dest inat ion argument s are not
arrays or t heir t ypes are not compat ible, an ArraySt oreExcept ion will be t hrown.
Q: Can yo u call o ne co nstructo r fro m ano ther if a class has multiple co nstructo rs?
A: Yes, use t his() synt ax.
Q: What's the difference between the metho ds sleep() and wait()?
A: The code sleep(2000); put s t hread aside for exact ly t wo seconds. The code wait (2000), causes a
wait of up t o t wo second. A t hread could st op wait ing earlier if it receives t he not ify() or not ifyAll()
call. The met hod wait () is defined in t he class Object and t he met hod sleep() is defined in t he class
Thread.
Q: When ArithmeticExceptio n is thro wn?
A: The Arit hmet icExcept ion is t hrown when int eger is divided by zero or t aking t he remainder of a
number by zero. It is never t hrown in float ing-point operat ions.
Q: What is a transient variable?
A: A t ransient variable is a variable t hat may not be serialized during Serializat ion and which is
init ialized by it s default value during de-serializat ion,
Q: What is synchro nizatio n?
A: Synchronizat ion is t he capabilit y t o cont rol t he access of mult iple t hreads t o shared resources.
synchronized keyword in java provides locking which ensures mut ual exclusive access of shared
resource and prevent dat a race.
Q: What is the Co llectio ns API?
A: The Collect ions API is a set of classes and int erfaces t hat support operat ions on collect ions of
object s.
Q: Do es garbage co llectio n guarantee that a pro gram will no t run o ut o f memo ry?
A: Garbage collect ion does not guarant ee t hat a program will not run out of memory. It is possible
for programs t o use up memory resources fast er t han t hey are garbage collect ed. It is also possible
for programs t o creat e object s t hat are not subject t o garbage collect ion.
Q: T he immediate superclass o f the Applet class?
A: Panel is t he immediat e superclass. A panel provides space in which an applicat ion can at t ach any
ot her component , including ot her panels.
Q: Which Java o perato r is right asso ciative?
A: The = operat or is right associat ive.

Q: What is the difference between a break statement and a co ntinue statement?


A: A break st at ement result s in t he t erminat ion of t he st at ement t o which it applies (swit ch, for,
do, or while). A cont inue st at ement is used t o end t he current loop it erat ion and ret urn cont rol t o
t he loop st at ement .
Q: If a variable is declared as private, where may the variable be accessed?
A: A privat e variable may only be accessed wit hin t he class in which it is declared.
Q: What is the purpo se o f the System class?
A: The purpose of t he Syst em class is t o provide access t o syst em resources.
Q: List primitive Java types?
A: The eight primit ive t ypes are byt e, char, short , int , long, float , double, and boolean.
Q: What is the relatio nship between clipping and repainting under AWT ?
A: When a window is repaint ed by t he AWT paint ing t hread, it set s t he clipping regions t o t he area
of t he window t hat requires repaint ing.
Q: Which class is the immediate superclass o f the Co ntainer class?
A: Component class is t he immediat e super class.
Q: What class o f exceptio ns are generated by the Java run-time system?
A: The Java runt ime syst em generat es Runt imeExcept ion and Error except ions.
Q: Under what co nditio ns is an o bject's finalize() metho d invo ked by the garbage
co llecto r?
A: The garbage collect or invokes an object 's finalize() met hod when it det ect s t hat t he object has
become unreachable.
Q: Ho w can a dead thread be restarted?
A: A dead t hread cannot be rest art ed.
Q: Which arithmetic o peratio ns can result in the thro wing o f an
ArithmeticExceptio n?
A: Int eger / and % can result in t he t hrowing of an Arit hmet icExcept ion.
Q: Variable o f the bo o lean type is auto matically initialized as?
A: The default value of t he boolean t ype is false.
Q: Can try statements be nested?
A: Yes
Q: What are ClassLo aders?
A: A class loader is an object t hat is responsible for loading classes. The class ClassLoader is an
abst ract class.
Q: What is the difference between an Interface and an Abstract class?
A: An abst ract class can have inst ance met hods t hat implement a default behavior. An Int erface can
only declare const ant s and inst ance met hods, but cannot implement default behavior and all
met hods are implicit ly abst ract . An int erface has all public members and no implement at ion.
Q: What will happen if static mo difier is remo ved fro m the signature o f the main
metho d?

A: Program t hrows "NoSuchMet hodError" error at runt ime .


Q: What is the default value o f an o bject reference declared as an instance
variable?
A: Null, unless it is defined explicit ly.
Q: Can a to p level class be private o r pro tected?
A: No, a t op level class can not be privat e or prot ect ed. It can have eit her "public" or no modifier.
Q: Why do we need wrapper classes?
A: We can pass t hem around as met hod paramet ers where a met hod expect s an object . It also
provides ut ilit y met hods.
Q: What is the difference between erro r and an exceptio n?
A: An error is an irrecoverable condit ion occurring at runt ime. Such as Out OfMemory error.
Except ions are condit ions t hat occur because of bad input et c. e.g. FileNot FoundExcept ion will be
t hrown if t he specified file does not exist .
Q: Is it necessary that each try blo ck must be fo llo wed by a catch blo ck?
A: It is not necessary t hat each t ry block must be followed by a cat ch block. It should be followed by
eit her a cat ch block or a finally block.
Q: When a thread is created and started, what is its initial state?
A: A t hread is in t he ready st at e as init ial st at e aft er it has been creat ed and st art ed.
Q: What is the Lo cale class?
A: The Locale class is used t o t ailor program out put t o t he convent ions of a part icular geographic,
polit ical, or cult ural region.
Q: What are synchro nized metho ds and synchro nized statements?
A: Synchronized met hods are met hods t hat are used t o cont rol access t o an object . A synchronized
st at ement can only be execut ed aft er a t hread has acquired t he lock for t he object or class
referenced in t he synchronized st at ement .
Q: What is runtime po lymo rphism o r dynamic metho d dispatch?
A: Runt ime polymorphism or dynamic met hod dispat ch is a process in which a call t o an overridden
met hod is resolved at runt ime rat her t han at compile-t ime. In t his process, an overridden met hod is
called t hrough t he reference variable of a superclass.
Q: What is Dynamic Binding(late binding)?
A: Binding refers t o t he linking of a procedure call t o t he code t o be execut ed in response t o t he
call. Dynamic binding means t hat t he code associat ed wit h a given procedure call is not known unt il
t he t ime of t he call at run-t ime.
Q: Can co nstructo r be inherited?
A: No, const ruct or cannot be inherit ed.
Q: What are the advantages o f ArrayList o ver arrays?
A: ArrayList can grow dynamically and provides more powerful insert ion and search mechanisms t han
arrays.
Q: Why deletio n in LinkedList is fast than ArrayList?
A: Delet ion in linked list is fast because it involves only updat ing t he next point er in t he node before
t he delet ed node and updat ing t he previous point er in t he node aft er t he delet ed node.

Q: Ho w do yo u decide when to use ArrayList and LinkedList?


A: If you need t o frequent ly add and remove element s from t he middle of t he list and only access
t he list element s sequent ially, t hen LinkedList should be used. If you need t o support random
access, wit hout insert ing or removing element s from any place ot her t han t he end, t hen ArrayList
should be used.
Q: What is a Values Co llectio n View ?
A: It is a collect ion ret urned by t he values() met hod of t he Map Int erface, It cont ains all t he object s
present as values in t he map.
Q: What is do t o perato r?
A: The dot operat or(.) is used t o access t he inst ance variables and met hods of class object s.It is
also used t o access classes and sub-packages from a package.
Q: Where and ho w can yo u use a private co nstructo r?
A: Privat e const ruct or is used if you do not want ot her classes t o inst ant iat e t he object and t o
prevent subclassing.T
Q: What is type casting?
A: Type cast ing means t reat ing a variable of one t ype as t hough it is anot her t ype.
Q: Describe life cycle o f thread?
A: A t hread is a execut ion in a program. The life cycle of a t hread include:
Newborn st at e
Runnable st at e
Running st at e
Blocked st at e
Dead st at e
Q: What is the difference between the >> and >>> o perato rs?
A: The >> operat or carries t he sign bit when shift ing right . The >>> zero-fills bit s t hat have been
shift ed out .
Q: Which metho d o f the Co mpo nent class is used to set the po sitio n and size o f a
co mpo nent?
A: set Bounds() met hod is used for t his purpose.
Q: What is the range o f the sho rt type?
A: The range of t he short t ype is -(2^15) t o 2^15 - 1.
Q: What is the immediate superclass o f Menu?
A: MenuIt em class
Q: Do es Java allo w Default Arguments?
A: No, Java does not allow Default Argument s.
Q: Which number is deno ted by leading zero in java?
A: Oct al Numbers are denot ed by leading zero in java, example: 06
Q: Which number is deno ted by leading 0x o r 0X in java?
A: Hexadecimal Numbers are denot ed by leading 0x or 0X in java, example: 0XF

Q: Break statement can be used as labels in Java?


A: Yes, an example can be break one;
Q: Where impo rt statement is used in a Java pro gram?
A: Import st at ement is allowed at t he beginning of t he program file aft er package st at ement .
Q: Explain suspend() metho d under T hread class>
A: It is used t o pause or t emporarily st op t he execut ion of t he t hread.
Q: Explain isAlive() metho d under T hread class?
A: It is used t o find out whet her a t hread is st ill running or not .
Q: What is currentT hread()?
A: It is a public st at ic met hod used t o obt ain a reference t o t he current t hread.
Q: Explain main thread under T hread class executio n?
A: The main t hread is creat ed aut omat ically and it begins t o execut e immediat ely when a program
st art s. It ia t hread from which all ot her child t hreads originat e.
Q: Life cycle o f an applet includes which steps?
A: Life cycle involves t he following st eps:
Init ializat ion
St art ing
St opping
Dest roying
Paint ing
Q: Why is the ro le o f init() metho d under applets?
A: It init ializes t he applet and is t he first met hod t o be called.
Q: Which metho d is called by Applet class to lo ad an image?
A: get Image(URL object , filename) is used for t his purpose.
Q: Define co de as an attribute o f Applet?
A: It is used t o specify t he name of t he applet class.
Q: Define canvas?
A: It is a simple drawing surface which are used for paint ing images or t o perform ot her graphical
operat ions.
Q: Define Netwo rk Pro gramming?
A: It refers t o writ ing programs t hat execut e across mult iple devices (comput ers), in which t he
devices are all connect ed t o each ot her using a net work.
Q: What is a So cket?
A: Socket s provide t he communicat ion mechanism bet ween t wo comput ers using TCP. A client
program creat es a socket on it s end of t he communicat ion and at t empt s t o connect t hat socket t o
a server.
Q: Advantages o f Java So ckets?

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 :-)

Potrebbero piacerti anche