Sunteți pe pagina 1din 55

What is Java?

Java is an object-oriented programming language developed initially by James Gosling and colleagues at Sun Microsystems.
The language, initially called a! "named a#ter the oa! trees outside Gosling$s o##ice%, &as intended to replace '((,
although the #eature set better resembles that o# bjective '. Java should not be con#used &ith JavaScript, &hich shares
only the name and a similar '-li!e synta). Sun Microsystems currently maintains and updates Java regularly.
What is Collection API ?
The 'ollection *+, is a set o# classes and inter#aces that support operation on collections o# objects. These classes and
inter#aces are more #le)ible, more po&er#ul, and more regular than the vectors, arrays, and hashtables i# e##ectively replaces.
-)ample o# classes. /ashSet, /ashMap, *rray0ist, 0in!ed0ist, TreeSet and TreeMap.
-)ample o# inter#aces. 'ollection, Set, 0ist and Map.
,s ,terator a 'lass or ,nter#ace? What is its use?
*ns&er. ,terator is an inter#ace &hich is used to step through the elements o# a 'ollection.
What is similarities/difference between an Abstract class and Interface?
1i##erences are as #ollo&s.
,nter#aces provide a #orm o# multiple inheritance. * class can e)tend only one other class. ,nter#aces are limited to public
methods and constants &ith no implementation. *bstract classes can have a partial implementation, protected parts, static
methods, etc.
* 'lass may implement several inter#aces. 2ut in case o# abstract class, a class may e)tend only one abstract class. ,nter#aces
are slo& as it re3uires e)tra indirection to to #ind corresponding method in in the actual class. *bstract classes are #ast.
Similarities.
4either *bstract classes or ,nter#ace can be instantiated.
How to define an Abstract class?
* class containing abstract method is called *bstract class. *n *bstract class can$t be instantiated.
-)ample o# *bstract class.
abstract class test*bstract'lass 5
protected String myString6
public String getMyString"% 5
return myString6
7
public abstract string any*bstract8unction"%6
7
/o& to de#ine an ,nter#ace?
,n Java ,nter#ace de#ines the methods but does not implement them. ,nter#ace can include constants. * class that implements
the inter#aces is bound to implement all the methods de#ined in ,nter#ace.
-maple o# ,nter#ace.
public inter#ace sample,nter#ace 5
public void #unctionne"%6
public long '4ST*4T94- : ;<<<6
7
,# a class is located in a pac!age, &hat do you need to change in the S environment to be able to use it?
=ou need to add a directory or a jar #ile that contains the pac!age directories to the '0*SS+*T/ environment variable. 0et$s
say a class -mployee belongs to a pac!age com.)y>.hr6 and is located in the #ile c.?dev?com?)y>?hr?-mployee.java. ,n this
case, you$d need to add c.?dev to the variable '0*SS+*T/. ,# this class contains the method main"%, you could test it #rom a
command prompt &indo& as #ollo&s.
c.?@java com.)y>.hr.-mployee
/o& many methods in the Seriali>able inter#ace?
There is no method in the Seriali>able inter#ace. The Seriali>able inter#ace acts as a mar!er, telling the object seriali>ation
tools that your class is seriali>able.
/o& many methods in the -)ternali>able inter#ace?
There are t&o methods in the -)ternali>able inter#ace. =ou have to implement these t&o methods in order to ma!e your class
e)ternali>able. These t&o methods are read-)ternal"% and &rite-)ternal"%.
What is the difference between Serializalble and Externalizable interface?
When you use Seriali>able inter#ace, your class is seriali>ed automatically by de#ault. 2ut you can override &ritebject"% and
readbject"% t&o methods to control more comple) object seraili>ation process. When you use -)ternali>able inter#ace, you
have a complete control over your class$s seriali>ation process.
What is a transient variable?
* transient variable is a variable that may not be seriali>ed. ,# you don$t &ant some #ield to be seriali>ed, you can mar! that
#ield transient or static.
Which containers use a border laout as their default laout?
The Windo&, 8rame and 1ialog classes use a border layout as their de#ault layout.
How are !bserver and !bservable used?
bjects that subclass the bservable class maintain a list o# observers. When an bservable object is updated, it invo!es the
update"% method o# each o# its observers to noti#y the observers that it has changed state. The bserver inter#ace is
implemented by objects that observe bservable objects.
What does a &ell-&ritten program loo! li!e?
* &ell-&ritten program e)hibits recurring structures that promote abstraction, #le)ibility, modularity and elegance.
'an you have virtual #unctions in Java?
=es, all #unctions in Java are virtual by de#ault. This is actually a pseudo tric! 3uestion because the &ord AvirtualA is not part
o# the naming convention in Java "as it is in '((, '-sharp and B2.4-T%, so this &ould be a #oreign concept #or someone &ho
has only coded in Java. Birtual #unctions or virtual methods are #unctions or methods that &ill be rede#ined in derived classes.
Jac! developed a program by using a Map container to hold !eyCvalue pairs. /e &anted to ma!e a change to the map. /e
decided to ma!e a clone o# the map in order to save the original data on side. What do you thin! o# it? ?
,# Jac! made a clone o# the map, any changes to the clone or the original map &ould be seen on both maps, because the clone
o# Map is a shallo& copy. So Jac! made a &rong decision.
What is more advisable to create a thread, by implementing a Dunnable inter#ace or by e)tending Thread class?
Strategically spea!ing, threads created by implementing Dunnable inter#ace are more advisable. ,# you create a thread by
e)tending a thread class, you cannot e)tend any other class. ,# you create a thread by implementing Dunnable inter#ace, you
save a space #or your class to e)tend another class no& or in #uture.
What is 4ull+ointer-)ception and ho& to handle it?
When an object is not initiali>ed, the de#ault value is null. When the #ollo&ing things happen, the 4ull+ointer-)ception is
thro&n.
--'alling the instance method o# a null object.
--*ccessing or modi#ying the #ield o# a null object.
--Ta!ing the length o# a null as i# it &ere an array.
--*ccessing or modi#ying the slots o# null as i# it &ere an array.
--Thro&ing null as i# it &ere a Thro&able value.
The 4ull+ointer-)ception is a runtime e)ception. The best practice is to catch such e)ception even i# it is not re3uired by
language design.
An a""lication needs to load a librar before it starts to run# how to code?
ne option is to use a static bloc! to load a library be#ore anything is called. 8or e)ample,
class Test 5
static 5
System.load0ibrary"Apath-to-library-#ileA%6
7
....
7
When you call ne& Test"%, the static bloc! &ill be called #irst be#ore any initiali>ation happens. 4ote that the static bloc!
position may matter.
/o& could Java classes direct program messages to the system console, but error messages, say to a #ile?
The class System has a variable out that represents the standard output, and the variable err that represents the standard error
device. 2y de#ault, they both point at the system console. This ho& the standard output could be re-directed.
Stream st : ne& Stream"ne& 8ileutputStream"Aoutput.t)tA%%6 System.set-rr"st%6 System.setut"st%6
What do you understand by Synchroni>ation?
Synchroni>ation is a process o# controlling the access o# shared resources by the multiple threads in such a manner that only
one thread can access one resource at a time. ,n non synchroni>ed multithreaded application, it is possible #or one thread to
modi#y a shared object &hile another thread is in the process o# using or updating the object$s value.
Synchroni>ation prevents such type o# data corruption.
-.g. Synchroni>ing a #unction.
public synchroni>ed void Method; "% 5
CC *ppropriate method-related code.
7
-.g. Synchroni>ing a bloc! o# code inside a #unction.
public my8unction "%5
synchroni>ed "this% 5
CC Synchroni>ed code here.
7
7
What is snchronization and wh is it im"ortant?
With respect to multithreading, synchroni>ation is the capability to control the access o# multiple threads to shared resources.
Without synchroni>ation, it is possible #or one thread to modi#y a shared object &hile another thread is in the process o# using
or updating that object$s value. This o#ten causes dirty data and leads to signi#icant errors.
What are snchronized methods and snchronized statements?
Synchroni>ed methods are methods that are used to control access to a method or an object. * thread only e)ecutes a
synchroni>ed method a#ter it has ac3uired the loc! #or the method$s object or class. Synchroni>ed statements are similar to
synchroni>ed methods. * synchroni>ed statement can only be e)ecuted a#ter a thread has ac3uired the loc! #or the object or
class re#erenced in the synchroni>ed statement.
What are three &ays in &hich a thread can enter the &aiting state?
* thread can enter the &aiting state by invo!ing its sleep"% method, by bloc!ing on ,, by unsuccess#ully attempting to ac3uire
an object$s loc!, or by invo!ing an object$s &ait"% method. ,t can also enter the &aiting state by invo!ing its "deprecated%
suspend"% method.
Can a loc$ be ac%uired on a class?
=es, a loc! can be ac3uired on a class. This loc! is ac3uired on the class$s 'lass object.
What$s ne& &ith the stop"%, suspend"% and resume"% methods in J1E ;.F?
The stop"%, suspend"% and resume"% methods have been deprecated in J1E ;.F.
What is the pre#erred si>e o# a component?
The pre#erred si>e o# a component is the minimum component si>e that &ill allo& the component to display normally.
What$s the di##erence bet&een JFS1E ;.G and JFS1E G.<?
There$s no di##erence, Sun Microsystems just re-branded this version.
What &ould you use to compare t&o String variables - the operator :: or the method e3uals"%?
,$d use the method e3uals"% to compare the values o# the Strings and the :: to chec! i# t&o variables point at the same instance
o# a String object.
What is thread?
* thread is an independent path o# e)ecution in a system.
What is multi-threading?
Multi-threading means various threads that run in a system.
How does multi&threadin' ta$e "lace on a com"uter with a sin'le CP(?
The operating system$s tas! scheduler allocates e)ecution time to multiple tas!s. 2y 3uic!ly s&itching bet&een e)ecuting
tas!s, it creates the impression that tas!s e)ecute se3uentially.
/o& to create a thread in a program?
=ou have t&o &ays to do so. 8irst, ma!ing your class Ae)tendsA Thread class. Second, ma!ing your class AimplementsA
Dunnable inter#ace. +ut jobs in a run"% method and call start"% method to start the thread.
'an Java object be loc!ed do&n #or e)clusive use by a given thread?
=es. =ou can loc! an object by putting it in a Asynchroni>edA bloc!. The loc!ed object is inaccessible to any thread other than
the one that e)plicitly claimed it.
Can each )ava ob*ect $ee" trac$ of all the threads that want to exclusivel access to it?
=es. Hse Thread.currentThread"% method to trac! the accessing thread.
+oes it matter in what order catch statements for ,ile-ot,oundExce"tion and I!Exce"ti"on are written?
=es, it does. The 8ile4o8ound-)ception is inherited #rom the ,-)ception. -)ception$s subclasses have to be caught #irst.
What invo!es a thread$s run"% method?
*#ter a thread is started, via its start"% method o# the Thread class, the JBM invo!es the thread$s run"% method &hen the thread
is initially e)ecuted.
What is the "ur"ose of the wait./# notif./# and notifAll./ methods?
The &ait"%,noti#y"%, and noti#y*ll"% methods are used to provide an e##icient &ay #or threads to communicate each other.
What are the high-level thread states?
The high-level thread states are ready, running, &aiting, and dead.
What is the di##erence bet&een yielding and sleeping?
When a tas! invo!es its yield"% method, it returns to the ready state. When a tas! invo!es its sleep"% method, it returns to the
&aiting state.
What happens &hen a thread cannot ac3uire a loc! on an object?
,# a thread attempts to e)ecute a synchroni>ed method or synchroni>ed statement and is unable to ac3uire an object$s loc!, it
enters the &aiting state until the loc! becomes available.
What is the difference between Process and 0hread?
* process can contain multiple threads. ,n most multithreading operating systems, a process gets its o&n memory address
space6 a thread doesn$t. Threads typically share the heap belonging to their parent process. 8or instance, a JBM runs in a single
process in the host CS. Threads in the JBM share the heap belonging to that process6 that$s &hy several threads may access
the same object. Typically, even though they share a common heap, threads have their o&n stac! space. This is ho& one
thread$s invocation o# a method is !ept separate #rom another$s. This is all a gross oversimpli#ication, but it$s accurate enough
at a high level. 0ots o# details di##er bet&een operating systems. +rocess vs. Thread * program vs. similar to a se3uential
program an run on its o&n vs. 'annot run on its o&n Hnit o# allocation vs. Hnit o# e)ecution /ave its o&n memory space vs.
Share &ith others -ach process has one or more threads vs. -ach thread belongs to one process -)pensive, need to conte)t
s&itch vs. 'heap, can use process memory and may not need to conte)t s&itch More secure. ne process cannot corrupt
another process vs. 0ess secure. * thread can &rite the memory used by another thread
Can an inner class declared inside of a method access local variables of this method?
,t$s possible i# these variables are #inal.
What can 'o wron' if ou re"lace 1em"21em"2 with 1em"2 in the followin' code3 Strin' a4null2 if .a54null 11
a6len'th./789/ :666;
* single ampersand here &ould lead to a 4ull+ointer-)ception.
What is the Bector class?
The Bector class provides the capability to implement a gro&able array o# objects
What modifiers ma be used with an inner class that is a member of an outer class?
* "non-local% inner class may be declared as public, protected, private, static, #inal, or abstract.
,# a method is declared as protected, &here may the method be accessed?
* protected method may only be accessed by classes or inter#aces o# the same pac!age or by subclasses o# the class in &hich it
is declared.
What is an ,terator inter#ace?
The ,terator inter#ace is used to step through the elements o# a 'ollection.
/o& many bits are used to represent Hnicode, *S',,, HT8-;I, and HT8-J characters?
Hnicode re3uires ;I bits and *S',, re3uire K bits. *lthough the *S',, character set uses only K bits, it is usually represented
as J bits. HT8-J represents characters using J, ;I, and ;J bit patterns. HT8-;I uses ;I-bit and larger bit patterns.
What$s the main di##erence bet&een a Bector and an *rray0ist?
Java Bector class is internally synchroni>ed and *rray0ist is not.
What are &rapped classes?
Wrapped classes are classes that allo& primitive types to be accessed as objects.
1oes garbage collection guarantee that a program &ill not run out o# memory?
4o, it doesn$t. ,t is possible #or programs to use up memory resources #aster than they are garbage collected. ,t is also possible
#or programs to create objects that are not subject to garbage collection.
What is the di##erence bet&een preemptive scheduling and time slicing?
Hnder preemptive scheduling, the highest priority tas! e)ecutes until it enters the &aiting or dead states or a higher priority
tas! comes into e)istence. Hnder time slicing, a tas! e)ecutes #or a prede#ined slice o# time and then reenters the pool o# ready
tas!s. The scheduler then determines &hich tas! should e)ecute ne)t, based on priority and other #actors.
-ame Com"onent subclasses that su""ort "aintin' ?
The 'anvas, 8rame, +anel, and *pplet classes support painting.
What is a native method?
* native method is a method that is implemented in a language other than Java.
/o& can you &rite a loop inde#initely?
#or"66%--#or loop6 &hile"true%--al&ays true, etc.
'an an anonymous class be declared as implementing an inter#ace and e)tending a class?
*n anonymous class may implement an inter#ace or e)tend a superclass, but may not be declared to do both.
What is the purpose o# finalization?
The purpose o# #inali>ation is to give an unreachable object the opportunity to per#orm any cleanup processing be#ore the
object is garbage collected.
When should the method invo!e0ater"%be used?
This method is used to ensure that S&ing components are updated through the event-dispatching thread.
/o& many methods in bject class?
This 3uestion is not as!ed to test your memory. ,t tests you ho& &ell you !no& Java. Ten in total.
clone"%
e3uals"% L hashcode"%
get'lass"%
#inali>e"%
&ait"% L noti#y"%
toString"%
/o& does Java handle integer over#lo&s and under#lo&s?
,t uses lo& order bytes o# the result that can #it into the si>e o# the type allo&ed by the operation.
What is the numeric promotion?
4umeric promotion is used &ith both unary and binary bit&ise operators. This means that byte, char, and short values are
converted to int values be#ore a bit&ise operator is applied.
,# a binary bit&ise operator has one long operand, the other operand is converted to a long value.
The type o# the result o# a bit&ise operation is the type to &hich the operands have been promoted. 8or e)ample.
short a : G6
byte b : ;<6
long c : ;G6
The type o# the result o# "a(b% is int, not short or byte. The type o# the result o# "a(c% or "b(c% is long.
,s the numeric promotion available in other plat#orm?
=es. 2ecause Java is implemented using a plat#orm-independent virtual machine, bit&ise operations al&ays yield the same
result, even &hen run on machines that use radically di##erent '+Hs.
What is the di##erence bet&een the 2oolean L operator and the LL operator?
,# an e)pression involving the 2oolean L operator is evaluated, both operands are evaluated. Then the L operator is applied to
the operand. When an e)pression involving the LL operator is evaluated, the #irst operand is evaluated. ,# the #irst operand
returns a value o# true then the second operand is evaluated. The LL operator is then applied to the #irst and second operands.
,# the #irst operand evaluates to #alse, the evaluation o# the second operand is s!ipped.
perator L has no chance to s!ip both sides evaluation and LL operator does. ,# as!ed &hy, give details as above.
When is the *rithmetic-)ception thro&Muestion. What is the
Gregorian'alendar class?
The Gregorian'alendar provides support #or traditional Western calendars.
What is the SimpleTimeNone class?
The SimpleTimeNone class provides support #or a Gregorian calendar.
How can a subclass call a method or a constructor defined in a su"erclass?
Hse the #ollo&ing synta). super.myMethod"%6 To call a constructor o# the superclass, just &rite super"%6 in the #irst line o# the
subclass$s constructor.
What is the +roperties class?
The properties class is a subclass o# /ashtable that can be read #rom or &ritten to a stream. ,t also provides the capability to
speci#y a set o# de#ault values to be used.
What is the purpose o# the Duntime class?
The purpose o# the Duntime class is to provide access to the Java runtime system.
What is the purpose o# the System class?
The purpose o# the System class is to provide access to system resources.
What is the purpose o# the #inally clause o# a try-catch-#inally statement?
The #inally clause is used to provide the capability to e)ecute code no matter &hether or not an e)ception is thro&n or caught.
What is the 0ocale class?
The 0ocale class is used to tailor program output to the conventions o# a particular geographic, political, or cultural region.
What is an abstract method?
*n abstract method is a method &hose implementation is de#erred to a subclass. r, a method that has no implementation.
What is the difference between interface and abstract class?
inter#ace contains methods that must be abstract6 abstract class may contain concrete methods. inter#ace contains variables that
must be static and #inal6 abstract class may contain non-#inal and #inal variables. members in an inter#ace are public by de#ault,
abstract class may contain non-public members. inter#ace is used to AimplementsA6 &hereas abstract class is used to Ae)tendsA.
inter#ace can be used to achieve multiple inheritance6 abstract class can be used as a single inheritance. inter#ace can Ae)tendsA
another inter#ace, abstract class can Ae)tendsA another class and AimplementsA multiple inter#aces. inter#ace is absolutely
abstract6 abstract class can be invo!ed i# a main"% e)ists. inter#ace is more #le)ible than abstract class because one class can
only Ae)tendsA one super class, but AimplementsA multiple inter#aces. ,# given a choice, use inter#ace instead o# abstract class.
What is a static method?
* static method is a method that belongs to the class rather than any object o# the class and doesn$t apply to an object or even
re3uire that any objects o# the class have been instantiated.
What is a protected method?
* protected method is a method that can be accessed by any method in its pac!age and inherited by any subclass o# its class.
What is the di##erence bet&een a static and a non-static inner class?
* non-static inner class may have object instances that are associated &ith instances o# the class$s outer class. * static inner
class does not have any object instances.
What is an object$s loc! and &hich object$s have loc!s?
*n object$s loc! is a mechanism that is used by multiple threads to obtain synchroni>ed access to the object. * thread may
e)ecute a synchroni>ed method o# an object only a#ter it has ac3uired the object$s loc!. *ll objects and classes have loc!s. *
class$s loc! is ac3uired on the class$s 'lass object.
When can an object re#erence be cast to an inter#ace re#erence?
*n object re#erence can be cast to an inter#ace re#erence &hen the object implements the re#erenced inter#ace.
What is the di##erence bet&een a Windo& and a 8rame?
The 8rame class e)tends Windo& to de#ine a main application &indo& that can have a menu bar.
What is the di##erence bet&een a Windo& and a 8rame?
/eavy &eight components li!e *bstract Windo& Tool!it "*WT%, depend on the local &indo&ing tool!it. 8or e)ample,
java.a&t.2utton is a heavy &eight component, &hen it is running on the Java plat#orm #or Hni) plat#orm, it maps to a real
Moti# button. ,n this relationship, the Moti# button is called the peer to the java.a&t.2utton. ,# you create t&o 2uttons, t&o
peers and hence t&o Moti# 2uttons are also created. The Java plat#orm communicates &ith the Moti# 2uttons using the Java
4ative ,nter#ace. 8or each and every component added to the application, there is an additional overhead tied to the local
&indo&ing system, &hich is &hy these components are called heavy &eight.
Which pac!age has light &eight components?
java).S&ing pac!age. *ll components in S&ing, e)cept J*pplet, J1ialog, J8rame and JWindo& are light&eight components.
What are peerless components?
The peerless components are called light &eight components.
What is the di##erence bet&een the 8ont and 8ontMetrics classes?
The 8ontMetrics class is used to de#ine implementation-speci#ic properties, such as ascent and descent, o# a 8ont object
What restrictions are "laced on method overridin'?
verridden methods must have the same name, argument list, and return type. The overriding method may not limit the access
o# the method it overrides. The overriding method may not thro& any e)ceptions that may not be thro&n by the overridden
method.
What is casting?
There are t&o types o# casting, casting bet&een primitive numeric types and casting bet&een object re#erences. 'asting
bet&een numeric types is used to convert larger values, such as double values, to smaller values, such as byte values. 'asting
bet&een object re#erences is used to re#er to an object by a compatible class, inter#ace, or array type re#erence.
-)plain the usage o# the !ey&ord transient?
This !ey&ord indicates that the value o# this member variable does not have to be seriali>ed &ith the object. When the class
&ill be de-seriali>ed, this variable &ill be initiali>ed &ith a de#ault value o# its data type "i.e. >ero #or integers%.
What class allo&s you to read objects directly #rom a stream?
The bject,nputStream class supports the reading o# objects #rom input streams.
/o& are this"% and super"% used &ith constructors?
this"% is used to invo!e a constructor o# the same class. super"% is used to invo!e a superclass constructor.
/o& is it possible #or t&o String objects &ith identical values not to be e3ual under the :: operator?
The :: operator compares t&o objects to determine i# they are the same objects in memory. ,t is possible #or t&o String
objects to have the same value, but located in di##erent areas o# memory.
What is an , #ilter?
*n , #ilter is an object that reads #rom one stream and &rites to another, usually altering the data in some &ay as it is passed
#rom one stream to another.
What is the Set inter#ace?
The Set inter#ace provides methods #or accessing the elements o# a #inite mathematical set. Sets do not allo& duplicate
elements.
/o& can you #orce garbage collection?
=ou can$t #orce G', but could re3uest it by calling System.gc"%. JBM does not guarantee that G' &ill be started immediately.
What is the purpose o# the enable-vents"% method?
The enable-vents"% method is used to enable an event #or a particular object. 4ormally, an event is enabled &hen a listener is
added to an object #or a particular event. The enable-vents"% method is used by objects that handle events by overriding their
event-dispatch methods.
What is the di##erence bet&een the 8ile and Dandom*ccess8ile classes?
The 8ile class encapsulates the #iles and directories o# the local #ile system. The Dandom*ccess8ile class provides the
methods needed to directly access data contained in any part o# a #ile.
What inter#ace must an object implement be#ore it can be &ritten to a stream as an object?
*n object must implement the Seriali>able or -)ternali>able inter#ace be#ore it can be &ritten to a stream as an object.
What is the Desource2undle class?
The Desource2undle class is used to store locale-speci#ic resources that can be loaded by a program to tailor the program$s
appearance to the particular locale in &hich it is being run.
/o& do you !no& i# an e)plicit object casting is needed?
,# you assign a superclass object to a variable o# a subclass$s data type, you need to do e)plicit casting. 8or e)ample.
bject a6 'ustomer b6 b : "'ustomer% a6
When you assign a subclass to a variable having a supeclass type, the casting is per#ormed automatically.
What is a Java pac!age and ho& is it used?
* Java pac!age is a naming conte)t #or classes and inter#aces. * pac!age is used to create a separate name space #or groups o#
classes and inter#aces. +ac!ages are also used to organi>e related classes and inter#aces into a single *+, unit and to control
accessibility to these classes and inter#aces.
/o& do you restrict a user to cut and paste #rom the html page?
Hsing Servlet or client side scripts to loc! !eyboard !eys. ,t is one o# solutions.
What are the !b*ect and Class classes used for?
The bject class is the highest-level class in the Java class hierarchy. The 'lass class is used to represent the classes and
inter#aces that are loaded by a Java program.
What is Seriali>ation and deseriali>ation ?
Serialization is the "rocess of writin' the state of an ob*ect to a bte stream6 +eserialization is the "rocess of restorin'
these ob*ects6
-)plain the usage o# Java pac!ages.
This is a &ay to organi>e #iles &hen a project consists o# multiple modules. ,t also helps resolve naming con#licts &hen
di##erent pac!ages have classes &ith the same names. +ac!ages access level also allo&s you to protect data #rom being used by
the non-authori>ed classes.
1oes the code in #inally bloc! get e)ecuted i# there is an e)ception and a return statement in a catch bloc!?
,# an e)ception occurs and there is a return statement in catch bloc!, the #inally bloc! is still e)ecuted. The #inally bloc! &ill
not be e)ecuted &hen the System.e)it";% statement is e)ecuted earlier or the system shut do&n earlier or the memory is used
up earlier be#ore the thread goes to #inally bloc!.
,s Java a super set o# JavaScript?
4o. They are completely di##erent. Some synta) may be similar.
What is a 'ontainer in a GH,?
* 'ontainer contains and arranges other components "including other containers% through the use o# layout managers, &hich
use speci#ic layout policies to determine &here components should go as a #unction o# the si>e o# the container.
/o& the object oriented approach helps us !eep comple)ity o# so#t&are development under control?
We can discuss such issue #rom the #ollo&ing aspects.
bjects allo& procedures to be encapsulated &ith their data to reduce potential inter#erence.
,nheritance allo&s &ell-tested procedures to be reused and enables changes to ma!e once and have e##ect in all relevant places.
The &ell-de#ined separations o# inter#ace and implementation allo& constraints to be imposed on inheriting classes &hile still
allo&ing the #le)ibility o# overriding and overloading.
What is polymorphism?
+olymorphism means Ahaving many #ormsA. ,t allo&s methods "may be variables% to be &ritten that needn$t be concerned
about the speci#ics o# the objects they &ill be applied to. That is, the method can be speci#ied at a higher level o# abstraction
and can be counted on to &or! even on objects o# un-conceived classes.
What is desi'n b contract?
The design by contract speci#ies the obligations o# a method to any other methods that may use its services and also theirs to it.
8or e)ample, the preconditions speci#y &hat the method re3uired to be true &hen the method is called. /ence ma!ing sure that
preconditions are. Similarly, postconditions speci#y &hat must be true &hen the method is #inished, thus the called method has
the responsibility o# satis#ying the post conditions.
,n Java, the e)ception handling #acilities support the use o# design by contract, especially in the case o# chec!ed e)ceptions.
The assert !ey&ord can be used to ma!e such contracts.
What are use cases?
* use case describes a situation that a program might encounter and &hat behavior the program should e)hibit in that
circumstance. ,t is part o# the analysis o# a program. The collection o# use cases should, ideally, anticipate all the standard
circumstances and many o# the e)traordinary circumstances possible so that the program &ill be robust.
What are use cases?
* use case describes a situation that a program might encounter and &hat behavior the program should e)hibit in that
circumstance. ,t is part o# the analysis o# a program. The collection o# use cases should, ideally, anticipate all the standard
circumstances and many o# the e)traordinary circumstances possible so that the program &ill be robust.
What is scalability and per#ormance?
+er#ormance is a measure o# Aho& #ast can you per#orm this tas!.A and scalability describes ho& an application behaves as its
&or!load and available computing resources increase.
What is the bene#it o# subclass?
Generally. The sub class inherits all the public methods and the implementation.
The sub class inherits all the protected methods and their implementation.
The sub class inherits all the de#ault"non-access modi#ier% methods and their implementation.
The sub class also inherits all the public, protected and de#ault member variables #rom the super class.
The constructors are not part o# this inheritance model.
/o& to add menushortcut to menu item?
,# you have a button instance called about2utton, you may add menu short cut by calling about2utton.setMnemonic"$*$%, so
the user may be able to use *lt(* to clic! the button.
What are use cases?
* use case describes a situation that a program might encounter and &hat behavior the program should e)hibit in that
circumstance. ,t is part o# the analysis o# a program. The collection o# use cases should, ideally, anticipate all the standard
circumstances and many o# the e)traordinary circumstances possible so that the program &ill be robust.
In Sstem6out6"rintln./#what is Sstem#out and "rintln#"ls ex"lain?
System is a prede#ined #inal class,out is a +rintStream object acting as a #ield member and println is a built-in overloaded
method in the out object.
'an you &rite a Java class that could be used both as an applet as &ell as an application?
*. =es. *dd a main"% method to the applet.
'an you ma!e an instance o# an abstract class? 8or e)ample - java.util.'alender is an abstract class &ith a method
get,nstance"% &hich returns an instance o# the 'alender class.
4oO =ou cannot ma!e an instance o# an abstract class. *n abstract class has to be sub-classed. ,# you have an abstract class and
you &ant to use a method &hich has been implemented, you may need to subclass that abstract class, instantiate your subclass
and then call that method.
What is the output o# ) @ y? a.b : pP3 &hen ):;,y:F,p:Q,3:R?
When this !ind o# 3uestion has been as!ed, #ind the problems you thin! is necessary to as! bac! be#ore you give an ans&er.
*s! i# variables a and b have been declared or initiali>ed. ,# the ans&er is yes. =ou can say that the synta) is &rong. ,# the
statement is re&ritten as. )
What is the di##erence bet&een S&ing and *WT components?
*WT components are heavy-&eight, &hereas S&ing components are light&eight. /eavy &eight components depend on the
local &indo&ing tool!it. 8or e)ample, java.a&t.2utton is a heavy &eight component, &hen it is running on the Java plat#orm
#or Hni) plat#orm, it maps to a real Moti# button.
Why Java does not support pointers?
2ecause pointers are unsa#e. Java uses re#erence types to hide pointers and programmers #eel easier to deal &ith re#erence
types &ithout pointers. This is &hy Java and '-sharp shine.
+arsers? 1M vs S*S parser
+arsers are #undamental )ml components, a bridge bet&een SM0 documents and applications that process that SM0. The
parser is responsible #or handling )ml synta), chec!ing the contents o# the document against constraints established in a 1T1
or Schema.
1M
;. Tree o# nodes
F. Memory. ccupies more memory, pre##ered #or small SM0 documents
Q. Slo&er at runtime
R. Stored as objects
G. +rogrammatically easy
I. -ase o# navigation
S*S
;. Se3uence o# events
F. 1oesn$t use any memory pre#erred #or large documents
Q. 8aster at runtime
R. bjects are to be created
G. 4eed to &rite code #or creating objects
I. 2ac!&ard navigation is not possible as it se3uentially processes the document
'an you declare a class as private?
=es, &e can declare a private class as an inner class. 8or e)ample,
class My+rivate 5
private static class MyEey 5
String !ey : A;FQRGA6
7
public static void main"StringTU args% 5
System.out.println"ne& MyEey"%.!ey%6CCprints ;FQRG
7
7
What is the di##erence bet&een shallo& copy and deep copy?
Shallo& copy shares the same re#erence &ith the original object li!e cloning, &hereas the deep copy get a duplicate instance o#
the original object. ,# the shallo& copy has been changed, the original object &ill be re#lected and vice versa.
'an one create a method &hich gets a String and modi#ies it?
4o. ,n Java, Strings are constant or immutable6 their values cannot be changed a#ter they are created, but they can be shared.
nce you change a string, you actually create a ne& object. 8or e)ample.
String s : AabcA6 CCcreate a ne& String object representing AabcA
s : s.toHpper'ase"%6 CCcreate another object representing A*2'A
Why is multiple inheritance not possible in Java?
,t depends on ho& you understand AinheritanceA. Java can only Ae)tendsA one super class, but can AimplementsA many
inter#aces6 that doesn$t mean the multiple inheritance is not possible. =ou may use inter#aces to ma!e inheritance &or! #or you.
r you may need to &or! around. 8or e)ample, i# you cannot get a #eature #rom a class because your class has a super class
already, you may get that class$s #eature by declaring it as a member #ield or getting an instance o# that class. So the ans&er is
that multiple inheritance in Java is possible.
What$s the di##erence bet&een constructors and other methods?
'onstructors must have the same name as the class and can not return a value. They are only called once &hile regular
methods could be called many times.

What is the relationshi" between snchronized and volatile $eword?
The JBM is guaranteed to treat reads and &rites o# data o# QF bits or less as atomic."Some JBM might treat reads and &rites o#
data o# IR bits or less as atomic in #uture% 8or long or double variable, programmers should ta!e care in multi-threading
environment. -ither put these variables in a synchroni>ed method or bloc!, or declare them volatile.

0his class .IncrementIm"l/ will be used b various threads concurrentl2 can ou see the inherent flaw.s/? How would
ou im"rove it?
public class ,ncrement,mpl 5
private static int counter : <6
public synchroni>ed void increment"% 5
counter((6
7
public int get'ounter"% 5
return counter6
7
7
The counter is static variable &hich is shared by multiple instances o# this class. The increment"% method is synchroni>ed, but
the get'ounter"% should be synchroni>ed too. ther&ise the Java run-time system &ill not guarantee the data integrity and the
race conditions &ill occur. The #amous producerCconsumer e)ample listed at Sun$s thread tutorial site &ill tell more.
one o# solutions
public class ,ncrement,mpl 5
private static int counter : <6
public synchroni>ed void increment"% 5
counter((6
7
public synchroni>ed int get'ounter"% 5
return counter6
7
7

What are the drawbac$s of inheritance?
Since inheritance inherits everything #rom the super class and inter#ace, it may ma!e the subclass too clustering and sometimes
error-prone &hen dynamic overriding or dynamic overloading in some situation. ,n addition, the inheritance may ma!e peers
hardly understand your code i# they don$t !no& ho& your super-class acts and add learning curve to the process o#
development.
Hsually, &hen you &ant to use a #unctionality o# a class, you may use subclass to inherit such #unction or use an instance o#
this class in your class. Which is better, depends on your speci#ication.

Is there an other wa that ou can achieve inheritance in )ava?
There are a couple o# &ays. *s you !no&, the straight &ay is to Ae)tendsA andCor AimplementsA. The other &ay is to get an
instance o# the class to achieve the inheritance. That means to ma!e the supposed-super-class be a #ield member. When you
use an instance o# the class, actually you get every #unction available #rom this class, but you may lose the dynamic #eatures o#
+

0wo methods have $e words static snchronized and snchronized se"aratel6 What is the difference between them?
2oth are synchroni>ed methods. ne is instance method, the other is class method. Method &ith static modi#ier is a class
method. That means the method belongs to class itsel# and can be accessed directly &ith class name and is also called
Singleton design. The method &ithout static modi#ier is an instance method. That means the instance method belongs to its
object. -very instance o# the class gets its o&n copy o# its instance method.
When synchroni>ed is used &ith a static method, a loc! #or the entire class is obtained. When synchroni>ed is used &ith a non-
static method, a loc! #or the particular object "that means instance% o# the class is obtained.
Since both methods are synchroni>ed methods, you are not as!ed to e)plain &hat is a synchroni>ed method. =ou are as!ed to
tell the di##erence bet&een instance and class method. # course, your e)planation to ho& synchroni>ed !ey&ord &or!s
doesn$t hurt. *nd you may use this opportunity to sho& your !no&ledge scope.

How do ou create a read&onl collection?
The 'ollections class has si) methods to help out here.
;. unmodi#iable'ollection"'ollection c%
F. unmodi#iable0ist"0ist list%
Q. unmodi#iableMap"Map m%
R. unmodi#iableSet"Set s%
G. unmodi#iableSortedMap"SortedMap m%
I. unmodi#iableSortedSet"SortedSet s%
,# you get an ,terator #rom one o# these unmodi#iable collections, &hen you call remove"%, it &ill thro& an
Hnsupportedperation-)ception.

Can a "rivate method of a su"erclass be declared within a subclass?
Sure. * private #ield or method or inner class belongs to its declared class and hides #rom its subclasses. There is no &ay #or
private stu## to have a runtime overloading or overriding "polymorphism% #eatures.

Wh )ava does not su""ort multi"le inheritance ?
This is a classic 3uestion. =es or 4o depends on ho& you loo! at Java. ,# you #ocus on the synta) o# Ae)tendsA and compare
&ith '((, you may ans&er $4o$ and give e)planation to support you. r you may ans&er $=es$. Decommend you to say $=es$.
Java 1-S support multiple inheritance via inter#ace implementation. Some people may not thin! in this &ay. Give
e)planation to support your point.
What is the difference between final# finall and finalize?
Short ans&er.
#inal - declares constant
#inally - relates &ith e)ception handling
#inali>e - helps in garbage collection
,# as!ed to give details, e)plain.
#inal #ield, #inal method, #inal class
tryC#inally, tryCcatchC#inally
protected void #inali>e"% in bject class

What $ind of securit tools are available in )<SE =69?
There are three tools that can be used to protect application &or!ing &ithin the scope o# security policies set at remote sites.
!eytool -- used to manage !eystores and certi#icates.
jarsigner -- used to generate and veri#y J*D signatures.
policytool -- used #or managing policy #iles.
There are three tools that help obtain, list and manage Eerberos tic!ets.
!init -- used to obtain Eerberos BG tic!ets.
t!list -- used to list entries in credential cache and !ey tab.
!tab -- used to help manage entries in the !ey table.

How to ma$e an arra co" from Sstem?
There is a method called arraycopy in the System class. =ou can do it.
System.arraycopy"source*rray, src##set, destination*rray, dest##set, num#-lementsF'opy%6
When you use this method, the destination*rray &ill be #illed &ith the elements o# source*rray at the length speci#ied.

Can we use Sstem6arraco"./ method to co" the same arra?
=es, you can. The source and destination arrays can be the same i# you &ant to copy a subset o# the array to another area
&ithin that array.

What is shallo& copy or shallo& clone in array cloning?
Clonin' an arra invloves creatin' a new arra of the same size and t"e and co"in' all the old elements into the new
arra6 >ut such co" is called shallow co" or shallow clone because an chan'es to the ob*ect would be reflected in
both arras6

When is the ArraStoreExce"tion thrown?
When copying elements bet&een di##erent arrays, i# the source or destination arguments are not arrays or their types are not
compatible, an *rrayStore-)ception &ill be thro&n.

How to chec$ two arras to see if contents have the same t"es and contain the same elements?
ne o# options is to use the e3uals"% method o# *rrays class.
*rrays.e3uals"a, b%6
,# the array types are di##erent, a compile-time error &ill happen.

Can ou call one constructor from another if a class has multi"le constructors?
=es. Hse this"% synta).

What are the different t"es of inner classes?
There are #our di##erent types o# inner classes in Java. They are. a%Static member classes , a static member class has access to
all static methods o# the parent, or top-level, class b% Member classes, the member class is instance speci#ic and has access to
any and all methods and members, even the parent$s this re#erence c% 0ocal classes, are declared &ithin a bloc! o# code and are
visible only &ithin that bloc!, just as any other method variable. d% *nonymous classes, is a local class that has no name

In which case would ou choose a static inner class?
,nteresting one, static inner classes can access the outer class$s protected and private #ields. This is both a positive and a
negitive point #or us since &e can, in essence, violate the encapsulation o# the outer class by muc!ing up the outer class$s
protected and private #ields. The only proper use o# that capability is to &rite &hite-bo) tests o# the class -- since &e can
induce cases that might be very hard to induce via normal blac!-bo) tests "&hich don$t have access to the internal state o# the
object%. Second advantage,i# , can say, is that, &e can this static concept to impose restriction on the inner class. *gain as
discussed in earlier point, an ,nner class has access to all the public, private and protected members o# the parent class.
Suppose you &ant to restrict the access even to inner class, ho& &ould you go ahead? Ma!ing the inner class static en#orces it
to access only the public static members o# the outer class" Since, protected and private members are not supposed to be static
and that static members can access only other static members%. ,# it has to access any non-static member, it has to create an
instance o# the outer class &hich leads to accessing only public members.

What is wea$ reference in )ava
* &ea! re#erence is one that does not prevent the re#erenced object #rom being garbage collected. =ou might use them to
manage a /ashMap to loo! up a cache o# objects. * &ea! re#erence is a re#erence that does not !eep the object it re#ers to
alive. * &ea! re#erence is not counted as a re#erence in garbage collection. ,# the object is not re#erred to else&here as &ell, it
&ill be garbage collected.
What is the difference between final# finall and finalize?
#inal is used #or ma!ing a class no-subclassable, and ma!ing a member variable as a constant &hich cannot be modi#ied.
#inally is usually used to release all the resources utili>ed inside the try bloc!. *ll the resources present in the #inali>e method
&ill be garbage collected &henever G' is called. Though #inally and #inali>e seem to be #or a similar tas! there is an
interesting t&ea! here, usually , pre#er #inally than #inali>e unless it is unavoidable. This is because the code in #inally bloc! is
guaranteed o# e)ecution irrespective o# occurrence o# e)ception, &hile e)ecution o# #inali>e is not guarenteed.#inali>e method
is called by the garbage collector on an object &hen the garbage collector determines that there are no more re#erences to the
object. +resumably the garbage collector &ill, li!e its civil servant namesa!e, visit the heap on a regular basis to clean up
resources that are no longer in use. Garbage collection e)ists to prevent programmers #rom calling delete. This is a &onder#ul
#eature. 8or e)ample, i# you can$t call delete, then you can$t accidentally call delete t&ice on the same object. /o&ever,
removing delete #rom the language is not the same thing as automatically cleaning up. To add to it, Garbage collection might
not ever run. ,# garbage collection runs at all, and an object is no longer re#erenced, then that object$s #inali>e &ill run. *lso,
across multiple objects, #inali>e order is not predictable. The correct approach to resource cleanup in Java language programs
does not rely on #inali>e. ,nstead, you simply &rite e)plicit close methods #or objects that &rap native resources. ,# you ta!e
this approach, you must document that the close method e)ists and &hen it should be called. 'allers o# the object must then
remember to call close &hen they are #inished &ith a resource.

What?s the difference between the methods slee"./ and wait./
The code sleep";<<<%6 puts thread aside #or e)actly one second. The code &ait";<<<%, causes a &ait o# up to one second. *
thread could stop &aiting earlier i# it receives the noti#y"% or noti#y*ll"% call. The method &ait"% is de#ined in the class bject
and the method sleep"% is de#ined in the class Thread.

0he followin' statement "rints true or false# wh?
byteTU a : 5 ;, F, Q 76,
byteTU b : "byteTU% a.clone"%6
System.out.println"a :: b%6
The #alse &ill be printed out. 2ecause the t&o arrays have distinctive memory addresses. Starting in Java ;.F, &e can use
java.util.*rrays.e3uals"a, b% to compare &hether t&o arrays have the same contents.

Wh do we need to use 'etSstem@esource./ and 'etSstem@esources./ method to load resources?
2ecause &e &ant to loo! #or resources strictly #rom the system classpath, These methods use the system 'lass0oader to locate
resources, &hich gives you stricter control o# the resources used by the application.

ArithmeticExce"tion?
The *rithmetic-)ception is thro&n &hen integer is divided by >ero or ta!ing the remainder o# a number by >ero. ,t is never
thro&n in #loating-point operations.

What is a transient variable?
* transient variable is a variable that may not be seriali>ed.

Which containers use a border Aaout as their default laout?
The &indo&, 8rame and 1ialog classes use a border layout as their de#ault layout.

Wh do threads bloc$ on I/!?
Threads bloc! on ,C "that is enters the &aiting state% so that other threads may e)ecute &hile the ,C peration is per#ormed.

What is the out"ut from Sstem6out6"rintln.BHelloBCnull/2?
/ellonull

What is snchronization and wh is it im"ortant?
With respect to multithreading, synchroni>ation is the capability to control the access o# multiple threads to shared resources.
Without synchroni>ation, it is possible #or one thread to modi#y a shared object &hile another thread is in the process o# using
or updating that object$s value. This o#ten leads to signi#icant errors.

Can a loc$ be ac%uired on a class?
=es, a loc! can be ac3uired on a class. This loc! is ac3uired on the class$s 'lass object.

What?s new with the sto"./# sus"end./ and resume./ methods in )+D 86<?
The stop"%, suspend"% and resume"% methods have been deprecated in J1E ;.F.

Is null a $eword?
The null value is not a !ey&ord.

What is the "referred size of a com"onent?
The pre#erred si>e o# a component is the minimum component si>e that &ill allo& the component to display normally.

What method is used to s"ecif a container?s laout?
The set0ayout"% method is used to speci#y a container$s layout.

Which containers use a ,lowAaout as their default laout?
The +anel and *pplet classes use the 8lo&0ayout as their de#ault layout.

What state does a thread enter when it terminates its "rocessin'?
When a thread terminates its processing, it enters the dead state.

What is the Collections API?
The 'ollections *+, is a set o# classes and inter#aces that support operations on collections o# objects.

Which characters ma be used as the second character of an identifier# but not as the first character of an identifier?
The digits < through V may not be used as the #irst character o# an identi#ier but they may be used a#ter the #irst character o# an
identi#ier.

What is the Aist interface?
The 0ist inter#ace provides support #or ordered collections o# objects.

How does )ava handle inte'er overflows and underflows?
,t uses those lo& order bytes o# the result that can #it into the si>e o# the type allo&ed by the operation.

What is the Eector class?
The Bector class provides the capability to implement a gro&able array o# objects

What modifiers ma be used with an inner class that is a member of an outer class?
* "non-local% inner class may be declared as public, protected, private, static, #inal, or abstract.

What is an Iterator interface?
The ,terator inter#ace is used to step through the elements o# a 'ollection.

What is the difference between the 77 and 777 o"erators?
The @@ operator carries the sign bit &hen shi#ting right. The @@@ >ero-#ills bits that have been shi#ted out.

Which method of the Com"onent class is used to set the "osition and size of a com"onent?
set2ounds"%

How man bits are used to re"resent (nicode# ASCII# (0,&8F# and (0,&G characters?
Hnicode re3uires ;I bits and *S',, re3uire K bits. *lthough the *S',, character set uses only K bits, it is usually represented
as J bits. HT8-J represents characters using J, ;I, and ;J bit patterns. HT8-;I uses ;I-bit and larger bit patterns.

What is the difference between ieldin' and slee"in'?
When a tas! invo!es its yield"% method, it returns to the ready state. When a tas! invo!es its sleep"% method, it returns to the
&aiting state.

Which *ava6util classes and interfaces su""ort event handlin'?
The -ventbject class and the -vent0istener inter#ace support event processing.

Is sizeof a $eword?
The si>eo# operator is not a !ey&ord.

What are wra""er classes?
Wrapper classes are classes that allo& primitive types to be accessed as objects.

+oes 'arba'e collection 'uarantee that a "ro'ram will not run out of memor?
Garbage collection does not guarantee that a program &ill not run out o# memory. ,t is possible #or programs to use up
memory resources #aster than they are garbage collected. ,t is also possible #or programs to create objects that are not subject
to garbage collection.

What restrictions are "laced on the location of a "ac$a'e statement within a source code file?
* pac!age statement must appear as the #irst line in a source code #ile "e)cluding blan! lines and comments%.
Can an ob*ect?s finalize./ method be invo$ed while it is reachable?
*n object$s #inali>e"% method cannot be invo!ed by the garbage collector &hile the object is still reachable. /o&ever, an
object$s #inali>e"% method may be invo!ed by other objects.

What is the immediate su"erclass of the A""let class?
+anel

What is the difference between "reem"tive schedulin' and time slicin'?
Hnder preemptive scheduling, the highest priority tas! e)ecutes until it enters the &aiting or dead states or a higher priority
tas! comes into e)istence. Hnder time slicing, a tas! e)ecutes #or a prede#ined slice o# time and then reenters the pool o# ready
tas!s. The scheduler then determines &hich tas! should e)ecute ne)t, based on priority and other #actors.

-ame three Com"onent subclasses that su""ort "aintin'6
The 'anvas, 8rame, +anel, and *pplet classes support painting.

What value does readAine./ return when it has reached the end of a file?
The read0ine"% method returns null &hen it has reached the end o# a #ile.

What is the immediate su"erclass of the +ialo' class?
Windo&.

What is cli""in'?
'lipping is the process o# con#ining paint operations to a limited area or shape.

What is a native method?
* native method is a method that is implemented in a language other than Java.

Can a for statement loo" indefinitel?
=es, a #or statement can loop inde#initely. 8or e)ample, consider the #ollo&ing. #or"66% 6

What are order of "recedence and associativit# and how are the used?
rder o# precedence determines the order in &hich operators are evaluated in e)pressions. *ssociatity determines &hether an
e)pression is evaluated le#t-to-right or right-to-le#t

When a thread bloc$s on I/!# what state does it enter?
* thread enters the &aiting state &hen it bloc!s on ,C.

0o what value is a variable of the Strin' t"e automaticall initialized?
The de#ault value o# a String type is null.

What is the catch or declare rule for method declarations?
,# a chec!ed e)ception may be thro&n &ithin the body o# a method, the method must either catch the e)ception or declare it in
its thro&s clause.

What is the difference between a HenuItem and a Chec$boxHenuItem?
The 'hec!bo)Menu,tem class e)tends the Menu,tem class to support a menu item that may be chec!ed or unchec!ed.

What is a tas$?s "riorit and how is it used in schedulin'?
* tas!$s priority is an integer value that identi#ies the relative order in &hich it should be e)ecuted &ith respect to other tas!s.
The scheduler attempts to schedule higher priority tas!s be#ore lo&er priority tas!s.

What class is the to" of the AW0 event hierarch?
The java.a&t.*WT-vent class is the highest-level class in the *WT event-class hierarchy.

When a thread is created and started# what is its initial state?
* thread is in the ready state a#ter it has been created and started.

Can an anonmous class be declared as im"lementin' an interface and extendin' a class?
*n anonymous class may implement an inter#ace or e)tend a superclass, but may not be declared to do both.

What is the ran'e of the short t"e?
he range o# the short type is -"FW;G% to FW;G - ;.

What is the ran'e of the char t"e?
The range o# the char type is < to FW;I - ;.

In which "ac$a'e are most of the AW0 events that su""ort the event&dele'ation model defined?
Most o# the *WT-related events o# the event-delegation model are de#ined in the java.a&t.event pac!age. The *WT-vent
class is de#ined in the java.a&t pac!age.

What is the immediate su"er class of Henu?
What is the immediate super class o# Menu? Menu,tem

What is the "ur"ose of finalization?
The purpose o# #inali>ation is to give an unreachable object the opportunity to per#orm any cleanup processing be#ore the
object is garbage collected.

Which class is the immediate su"er class of the HenuCom"onent class6
bject

What invo$es a thread?s run./ method?
*#ter a thread is started, via its start"% method or that o# the Thread class, the JBM invo!es the thread$s run"% method &hen the
thread is initially e)ecuted.

What is the difference between the >oolean 1 o"erator and the 11 o"erator?
,# an e)pression involving the 2oolean L operator is evaluated, both operands are evaluated. Then the L operator is applied to
the operand. When an e)pression involving the LL operator is evaluated, the #irst operand is evaluated. ,# the #irst operand
returns a value o# true then the second operand is evaluated. The LL operator is then applied to the #irst and second operands.
,# the #irst operand evaluates to #alse, the evaluation o# the second operand is s!ipped.

-ame three subclasses of the Com"onent class6
2o).8iller, 2utton, 'anvas, 'hec!bo), 'hoice, 'ontainer, 0abel, 0ist, Scrollbar, or Te)t'omponent

What is the Ire'orianCalendar class?
The Gregorian'alendar provides support #or traditional Western calendars.

Which Container method is used to cause a container to be laid out and redis"laed?
validate"%

What is the "ur"ose of the @untime class?
The purpose o# the Duntime class is to provide access to the Java runtime system.
How man times ma an ob*ect?s finalize./ method be invo$ed b the 'arba'e collector?
*n object$s #inali>e"% method may only be invo!ed once by the garbage collector.

What is the "ur"ose of the finall clause of a tr&catch&finall statement? 'arba'e collector?
The #inally clause is used to provide the capability to e)ecute code no matter &hether or not an e)ception is thro&n or caught.

What is the ar'ument t"e of a "ro'ram?s main./ method?
* program$s main"% method ta!es an argument o# the StringTU type.

Which )ava o"erator is ri'ht associative?
The : operator is right associative.

What is the Aocale class?
The 0ocale class is used to tailor program output to the conventions o# a particular geographic, political, or cultural region.

Can a double value be cast to a bte?
=es, a double value can be cast to a byte.
What is the difference between a brea$ statement and a continue statement?
* brea! statement results in the termination o# the statement to &hich it applies "s&itch, #or, do, or &hile%. * continue
statement is used to end the current loop iteration and return control to the loop statement.

What must a class do to im"lement an interface?
,t must provide all o# the methods in the inter#ace and identi#y the inter#ace in its implements clause.

What method is invo$ed to cause an ob*ect to be'in executin' as a se"arate thread?
The start"% method o# the Thread class is invo!ed to cause an object to begin e)ecuting as a separate thread.

-ame two subclasses of the 0extCom"onent class6
Te)t8ield and Te)t*rea

What is the advanta'e of the event&dele'ation model over the earlier event&inheritance model?
The event-delegation model has t&o advantages over the event-inheritance model. 8irst, it enables event handling to be
handled by objects other than the ones that generate the events "or their containers%. This allo&s a clean separation bet&een a
component$s design and its use. The other advantage o# the event-delegation model is that it per#orms much better in
applications &here many events are generated. This per#ormance improvement is due to the #act that the event-delegation
model does not have to repeatedly process unhandled events, as is the case o# the event-inheritance model.

Which containers ma have a Henu>ar?
8rame

How are commas used in the initialization and iteration "arts of a for statement?
'ommas are used to separate multiple statements &ithin the initiali>ation and iteration parts o# a #or statement.

What is the "ur"ose of the wait./# notif./# and notifAll./ methods?
The &ait"%,noti#y"%, and noti#y*ll"% methods are used to provide an e##icient &ay #or threads to &ait #or a shared resource.
When a thread e)ecutes an object$s &ait"% method, it enters the &aiting state. ,t only enters the ready state a#ter another thread
invo!es the object$s noti#y"% or noti#y*ll"% methods.

What is an abstract method?
*n abstract method is a method &hose implementation is de#erred to a subclass.
How are )ava source code files named?
* Java source code #ile ta!es the name o# a public class or inter#ace that is de#ined &ithin the #ile. * source code #ile may
contain at most one public class or inter#ace. ,# a public class or inter#ace is de#ined &ithin a source code #ile, then the source
code #ile must ta!e the name o# the public class or inter#ace. ,# no public class or inter#ace is de#ined &ithin a source code #ile,
then the #ile must ta!e on a name that is di##erent than its classes and inter#aces. Source code #iles use the .java e)tension.
What is the relationshi" between the Canvas class and the Ira"hics class?
* 'anvas object provides access to a Graphics object via its paint"% method.
What are the hi'h&level thread states?
The high-level thread states are ready, running, &aiting, and dead.
What value does read./ return when it has reached the end of a file?
The read"% method returns -; &hen it has reached the end o# a #ile.
Can a >te ob*ect be cast to a double value?
4o, an object cannot be cast to a primitive value.
What is the difference between a static and a non&static inner class?
* non-static inner class may have object instances that are associated &ith instances o# the class$s outer class. * static inner
class does not have any object instances.
What is the difference between the Strin' and Strin'>uffer classes?
String objects are constants. String2u##er objects are not.
If a variable is declared as "rivate# where ma the variable be accessed?
* private variable may only be accessed &ithin the class in &hich it is declared.

What is an object$s loc! and &hich objects have loc!s?
*n object$s loc! is a mechanism that is used by multiple threads to obtain synchroni>ed access to the object. * thread may
e)ecute a synchroni>ed method o# an object only a#ter it has ac3uired the object$s loc!. *ll objects and classes have loc!s. *
class$s loc! is ac3uired on the class$s 'lass object.
What is the +ictionar class?
The 1ictionary class provides the capability to store !ey-value pairs.
How are the elements of a >orderAaout or'anized?
The elements o# a 2order0ayout are organi>ed at the borders "4orth, South, -ast, and West% and the center o# a container.
What is the J o"erator?
,t is re#erred to as the modulo or remainder operator. ,t returns the remainder o# dividing the #irst operand by the second
operand.
When can an ob*ect reference be cast to an interface reference?
*n object re#erence be cast to an inter#ace re#erence &hen the object implements the re#erenced inter#ace.
What is the difference between a Window and a ,rame?
The 8rame class e)tends Windo& to de#ine a main application &indo& that can have a menu bar.
Which class is extended b all other classes?
The bject class is e)tended by all other classes.
Can an ob*ect be 'arba'e collected while it is still reachable?
* reachable object cannot be garbage collected. nly unreachable objects may be garbage collected..
Is the ternar o"erator written x 3 ? z or x ? 3 z ?
,t is &ritten ) ? y . >.
What is the difference between the ,ont and ,ontHetrics classes?
The 8ontMetrics class is used to de#ine implementation-speci#ic properties, such as ascent and descent, o# a 8ont object.
How is roundin' "erformed under inte'er division?
The #ractional part o# the result is truncated. This is !no&n as rounding to&ard >ero.
What ha""ens when a thread cannot ac%uire a loc$ on an ob*ect?
,# a thread attempts to e)ecute a synchroni>ed method or synchroni>ed statement and is unable to ac3uire an object$s loc!, it
enters the &aiting state until the loc! becomes available.
What is the difference between the @eader/Writer class hierarch and the In"utStream/!ut"utStream class hierarch?
The DeaderCWriter class hierarchy is character-oriented, and the ,nputStreamCutputStream class hierarchy is byte-oriented.
What classes of exce"tions ma be cau'ht b a catch clause?
* catch clause can catch any e)ception that may be assigned to the Thro&able type. This includes the -rror and -)ception
types.
If a class is declared without an access modifiers# where ma the class be accessed?
* class that is declared &ithout any access modi#iers is said to have pac!age access. This means that the class can only be
accessed by other classes and inter#aces that are de#ined &ithin the same pac!age.
What is the Sim"le0imeKone class?
The SimpleTimeNone class provides support #or a Gregorian calendar.
What is the Ha" interface?
The Map inter#ace replaces the J1E ;.; 1ictionary class and is used associate !eys &ith values.
+oes a class inherit the constructors of its su"erclass?
* class does not inherit constructors #rom any o# its super classes.
,or which statements does it ma$e sense to use a label?
The only statements #or &hich it ma!es sense to use a label are those statements that can enclose a brea! or continue
statement.
What is the "ur"ose of the Sstem class?
The purpose o# the System class is to provide access to system resources.
Which 0extCom"onent method is used to set a 0extCom"onent to the read&onl state?
set-ditable"%
How are the elements of a CardAaout or'anized?
The elements o# a 'ard0ayout are stac!ed, one on top o# the other, li!e a dec! o# cards.
Is 114 a valid )ava o"erator?
4o, it is not.
-ame the ei'ht "rimitive )ava t"es6
The eight primitive types are byte, char, short, int, long, #loat, double, and boolean.
Which class should ou use to obtain desi'n information about an ob*ect?
The 'lass class is used to obtain in#ormation about an object$s design.

What is the relationshi" between cli""in' and re"aintin'?
When a &indo& is repainted by the *WT painting thread, it sets the clipping regions to the area o# the &indo& that re3uires
repainting.

Is BabcB a "rimitive value?
The String literal AabcA is not a primitive value. ,t is a String object.

What is the relationshi" between an event&listener interface and an event&ada"ter class?
*n event-listener inter#ace de#ines the methods that must be implemented by an event handler #or a particular !ind o# event.
*n event adapter provides a de#ault implementation o# an event-listener inter#ace.

What restrictions are "laced on the values of each case of a switch statement?
1uring compilation, the values o# each case o# a s&itch statement must evaluate to a value that can be promoted to an int
value.

What modifiers ma be used with an interface declaration?
*n inter#ace may be declared as public or abstract.

Is a class a subclass of itself?
* class is a subclass o# itsel#.

What is the hi'hest&level event class of the event&dele'ation model?
The java.util.-ventbject class is the highest-level class in the event-delegation class hierarchy.

What event results from the clic$in' of a button?
The *ction-vent event is generated as the result o# the clic!ing o# a button.

How can a I(I com"onent handle its own events?
* component can handle its o&n events by implementing the re3uired event-listener inter#ace and adding itsel# as its o&n
event listener.

What is the difference between a while statement and a do statement?
* &hile statement chec!s at the beginning o# a loop to see &hether the ne)t loop iteration should occur. * do statement chec!s
at the end o# a loop to see &hether the ne)t iteration o# a loop should occur. The do statement &ill al&ays e)ecute the body o#
a loop at least once.

How are the elements of a Irid>a'Aaout or'anized?
The elements o# a Grid2ag0ayout are organi>ed according to a grid. /o&ever, the elements are o# di##erent si>es and may
occupy more than one ro& or column o# the grid. ,n addition, the ro&s and columns may have di##erent si>es.

What advanta'e do )ava?s laout mana'ers "rovide over traditional windowin' sstems?
Java uses layout managers to lay out components in a consistent manner across all &indo&ing plat#orms. Since Java$s layout
managers aren$t tied to absolute si>ing and positioning, they are able to accommodate plat#orm-speci#ic di##erences among
&indo&ing systems.

What is the Collection interface?
The 'ollection inter#ace provides support #or the implementation o# a mathematical bag - an unordered collection o# objects
that may contain duplicates.

What modifiers can be used with a local inner class?
* local inner class may be #inal or abstract.

What is the difference between static and non&static variables?
* static variable is associated &ith the class as a &hole rather than &ith speci#ic instances o# a class. 4on-static variables ta!e
on uni3ue values &ith each object instance.

What is the difference between the "aint./ and re"aint./ methods?
The paint"% method supports painting via a Graphics object. The repaint"% method is used to cause paint"% to be invo!ed by the
*WT painting thread.

What is the "ur"ose of the ,ile class?
The 8ile class is used to create objects that provide access to the #iles and directories o# a local #ile system.

Can an exce"tion be rethrown?
=es, an e)ception can be rethro&n.

Which Hath method is used to calculate the absolute value of a number?
The abs"% method is used to calculate absolute values.

How does multithreadin' ta$e "lace on a com"uter with a sin'le CP(?
The operating system$s tas! scheduler allocates e)ecution time to multiple tas!s. 2y 3uic!ly s&itching bet&een e)ecuting
tas!s, it creates the impression that tas!s e)ecute se3uentially.

When does the com"iler su""l a default constructor for a class?
The compiler supplies a de#ault constructor #or a class i# no other constructors are provided.

When is the finall clause of a tr&catch&finall statement executed?
The #inally clause o# the try-catch-#inally statement is al&ays e)ecuted unless the thread o# e)ecution terminates or an
e)ception occurs &ithin the e)ecution o# the #inally clause.

Which class is the immediate su"erclass of the Container class?
'omponent>

If a method is declared as "rotected# where ma the method be accessed?
* protected method may only be accessed by classes or inter#aces o# the same pac!age or by subclasses o# the class in &hich it
is declared

How can the Chec$box class be used to create a radio button?
2y associating 'hec!bo) objects &ith a 'hec!bo)Group.

Which non&(nicode letter characters ma be used as the first character of an identifier?
The non-Hnicode letter characters X and 9 may appear as the #irst character o# an identi#ier

What restrictions are "laced on method overloadin'?
T&o methods may not have the same name and argument list but di##erent return types.

What ha""ens when ou invo$e a thread?s interru"t method while it is slee"in' or waitin'?
When a tas!$s interrupt"% method is e)ecuted, the tas! enters the ready state. The ne)t time the tas! enters the running state, an
,nterrupted-)ception is thro&n.

What is the return t"e of a "ro'ram?s main./ method?
* program$s main"% method has a void return type.

-ame four Container classes6
Windo&, 8rame, 1ialog, 8ile1ialog, +anel, *pplet, or Scroll+ane

What is the difference between a Choice and a Aist?
* 'hoice is displayed in a compact #orm that re3uires you to pull it do&n to see the list o# available choices. nly one item
may be selected #rom a 'hoice. * 0ist may be displayed in such a &ay that several 0ist items are visible. * 0ist supports the
selection o# one or more 0ist items.

What class of exce"tions are 'enerated b the )ava run&time sstem?
The Java runtime system generates Duntime-)ception and -rror e)ceptions.

What class allows ou to read ob*ects directl from a stream?
The bject,nputStream class supports the reading o# objects #rom input streams.

What is the difference between a field variable and a local variable?
* #ield variable is a variable that is declared as a member o# a class. * local variable is a variable that is declared local to a
method.

(nder what conditions is an ob*ect?s finalize./ method invo$ed b the 'arba'e collector?
The garbage collector invo!es an object$s #inali>e"% method &hen it detects that the object has become unreachable.

How are this ./ and su"er ./ used with constructors?
this"% is used to invo!e a constructor o# the same class. super"% is used to invo!e a superclass constructor.

What is the relationshi" between a method?s throws clause and the exce"tions that can be thrown durin' the method?s
execution?
* method$s thro&s clause must declare any chec!ed e)ceptions that are not caught &ithin the body o# the method.

What is the difference between the )+D 869< event model and the event&dele'ation model introduced with )+D 868?
The J1E ;.<F event model uses an event inheritance or bubbling approach. ,n this model, components are re3uired to handle
their o&n events. ,# they do not handle a particular event, the event is inherited by "or bubbled up to% the component$s
container. The container then either handles the event or it is bubbled up to its container and so on, until the highest-level
container has been tried. ,n the event-delegation model, speci#ic objects are designated as event handlers #or GH, components.
These objects implement event-listener inter#aces. The event-delegation model is more e##icient than the event-inheritance
model because it eliminates the processing re3uired to support the bubbling o# unhandled events.

How is it "ossible for two Strin' ob*ects with identical values not to be e%ual under the 44 o"erator?
The :: operator compares t&o objects to determine i# they are the same object in memory. ,t is possible #or t&o String objects
to have the same value, but located indi##erent areas o# memory.

Wh are the methods of the Hath class static?
So they can be invo!ed as i# they are a mathematical code library.

What Chec$box method allows ou to tell if a Chec$box is chec$ed?
getState"%

What state is a thread in when it is executin'?
*n e)ecuting thread is in the running state.

What are the le'al o"erands of the instanceof o"erator?
The le#t operand is an object re#erence or null value and the right operand is a class, inter#ace, or array type.

How are the elements of a IridAaout or'anized?
The elements o# a Grid2ad layout are o# e3ual si>e and are laid out using the s3uares o# a grid.

What an I/! filter?
*n ,C #ilter is an object that reads #rom one stream and &rites to another, usually altering the data in some &ay as it is passed
#rom one stream to another.

If an ob*ect is 'arba'e collected# can it become reachable a'ain?
nce an object is garbage collected, it ceases to e)ist. ,t can no longer become reachable again.
What are E and PI?
- is the base o# the natural logarithm and +, is mathematical value pi.

Are true and false $ewords?
The values true and #alse are not !ey&ords.

What is a void return t"e?
* void return type indicates that a method does not return a value.

What is the "ur"ose of the enableEvents./ method?
The enable-vents"% method is used to enable an event #or a particular object. 4ormally, an event is enabled &hen a listener is
added to an object #or a particular event. The enable-vents"% method is used by objects that handle events by overriding their
event-dispatch methods.

What is the difference between the ,ile and @andomAccess,ile classes?
The 8ile class encapsulates the #iles and directories o# the local #ile system. The Dandom*ccess8ile class provides the
methods needed to directly access data contained in any part o# a #ile.

What ha""ens when ou add a double value to a Strin'?
The result is a String object.

What is our "latform?s default character encodin'?
,# you are running Java on -nglish Windo&s plat#orms, it is probably 'p;FGF. ,# you are running Java on -nglish Solaris
plat#orms, it is most li!ely JJGV9;..

Which "ac$a'e is alwas im"orted b default?
The java.lang pac!age is al&ays imported by de#ault.

What interface must an ob*ect im"lement before it can be written to a stream as an ob*ect?
*n object must implement the Seriali>able or -)ternali>able inter#ace be#ore it can be &ritten to a stream as an object.

How are this and su"er used?
this is used to re#er to the current object instance. super is used to re#er to the variables and methods o# the superclass o# the
current object instance.

What is the "ur"ose of 'arba'e collection?
The purpose o# garbage collection is to identi#y and discard objects that are no longer needed by a program so that their
resources may be reclaimed and reused.

What is a com"ilation unit?
* compilation unit is a Java source code #ile.

What interface is extended b AW0 event listeners?
*ll *WT event listeners e)tend the java.util.-vent0istener inter#ace.

What restrictions are "laced on method overridin'?
verridden methods must have the same name, argument list, and return type. The overriding method may not limit the access
o# the method it overrides. The overriding method may not thro& any e)ceptions that may not be thro&n by the overridden
method.

How can a dead thread be restarted?
* dead thread cannot be restarted.

What ha""ens if an exce"tion is not cau'ht?
*n uncaught e)ception results in the uncaught-)ception"% method o# the thread$s ThreadGroup being invo!ed, &hich
eventually results in the termination o# the program in &hich it is thro&n.

What is a laout mana'er?
* layout manager is an object that is used to organi>e components in a container.

Which arithmetic o"erations can result in the throwin' of an ArithmeticExce"tion?
,nteger C and Y can result in the thro&ing o# an *rithmetic-)ception.

What are three was in which a thread can enter the waitin' state?
* thread can enter the &aiting state by invo!ing its sleep"% method, by bloc!ing on ,C, by unsuccess#ully attempting to
ac3uire an object$s loc!, or by invo!ing an object$s &ait"% method. ,t can also enter the &aiting state by invo!ing its
"deprecated% suspend"% method.

Can an abstract class be final?
*n abstract class may not be declared as #inal.

What is the @esource>undle class?
The Desource2undle class is used to store locale-speci#ic resources that can be loaded by a program to tailor the program$s
appearance to the particular locale in &hich it is being run.

What ha""ens if a tr&catch&finall statement does not have a catch clause to handle an exce"tion that is thrown within
the bod of the tr statement?
The e)ception propagates up to the ne)t higher level try-catch statement "i# any% or results in the program$s termination.

What is numeric "romotion?
4umeric promotion is the conversion o# a smaller numeric type to a larger numeric type, so that integer and #loating-point
operations may ta!e place. ,n numerical promotion, byte, char, and short values are converted to int values. The int values are
also converted to long values, i# necessary. The long and #loat values are converted to double values, as re3uired.

What is the difference between a Scrollbar and a ScrollPane?
* Scrollbar is a 'omponent, but not a 'ontainer. * Scroll+ane is a 'ontainer. * Scroll+ane handles its o&n events and
per#orms its o&n scrolling.

What is the difference between a "ublic and a non&"ublic class?
* public class may be accessed outside o# its pac!age. * non-public class may not be accessed outside o# its pac!age. .

0o what value is a variable of the boolean t"e automaticall initialized?
The de#ault value o# the boolean type is #alse.

Can tr statements be nested?
Try statements may be tested.

What is the difference between the "refix and "ostfix forms of the CC o"erator?
The pre#i) #orm per#orms the increment operation and returns the value o# the increment operation. The post#i) #orm returns
the current value all o# the e)pression and then per#orms the increment operation on that value.

What is the "ur"ose of a statement bloc$?
* statement bloc! is used to organi>e a se3uence o# statements as a single statement group.

What is a )ava "ac$a'e and how is it used?
* Java pac!age is a naming conte)t #or classes and inter#aces. * pac!age is used to create a separate name space #or groups o#
classes and inter#aces. +ac!ages are also used to organi>e related classes and inter#aces into a single *+, unit and to control
accessibility to these classes and inter#aces.

What modifiers ma be used with a to"&level class?
* top-level class may be public, abstract, or #inal.

What are the !b*ect and Class classes used for?
The bject class is the highest-level class in the Java class hierarchy. The 'lass class is used to represent the classes and
inter#aces that are loaded by a Java program.

How does a tr statement determine which catch clause should be used to handle an exce"tion?
When an e)ception is thro&n &ithin the body o# a try statement, the catch clauses o# the try statement are e)amined in the
order in &hich they appear. The #irst catch clause that is capable o# handling the e)ception is e)ecuted. The remaining catch
clauses are ignored.

Can an unreachable ob*ect become reachable a'ain?
*n unreachable object may become reachable again. This can happen &hen the object$s #inali>e"% method is invo!ed and the
object per#orms an operation &hich causes it to become accessible to reachable objects.

When is an ob*ect sub*ect to 'arba'e collection?
*n object is subject to garbage collection &hen it becomes unreachable to the program in &hich it is used.

What method must be im"lemented b all threads?
*ll tas!s must implement the run"% method, &hether they are a subclass o# Thread or implement the Dunnable inter#ace.

What methods are used to 'et and set the text label dis"laed b a >utton ob*ect?
get0abel"% and set0abel"%

Which Com"onent subclass is used for drawin' and "aintin'?
'anvas
What are the two basic was in which classes that can be run as threads ma be defined?
* thread class may be declared as a subclass o# Thread, or it may implement the Dunnable inter#ace.

What is the difference between an if statement and a switch statement?
The i# statement is used to select among t&o alternatives. ,t uses a 2oolean e)pression to decide &hich alternative should be
e)ecuted. The s&itch statement is used to select among multiple alternatives. ,t uses an int e)pression to determine &hich
alternative should be e)ecuted.

What are the different identifier states of a 0hread?
The di##erent identi#iers o# a Thread are.
D - Dunning or runnable thread
S - Suspended thread
'W - Thread &aiting on a condition variable
MW - Thread &aiting on a monitor loc!
MS - Thread suspended &aiting on a monitor loc!

What are some alternatives to inheritance?
1elegation is an alternative to inheritance. 1elegation means that you include an instance o# another class as an instance
variable, and #or&ard messages to the instance. ,t is o#ten sa#er than inheritance because it #orces you to thin! about each
message you #or&ard, because the instance is o# a !no&n class, rather than a ne& class, and because it doesn$t #orce you to
accept all the methods o# the super class. you can provide only the methods that really ma!e sense. n the other hand, it ma!es
you &rite more code, and it is harder to re-use "because it is not a subclass%.

Wh isn?t there o"erator overloadin'?
2ecause '(( has proven by e)ample that operator overloading ma!es code almost impossible to maintain. ,n #act there very
nearly &asn$t even method overloading in Java, but it &as thought that this &as too use#ul #or some very basic methods li!e
print"%. 4ote that some o# the classes li!e 1atautputStream have unoverloaded methods li!e &rite,nt"% and &rite2yte"%.

What does it mean that a method or field is BstaticB?
Static variables and methods are instantiated only once per class. ,n other &ords they are class variables, not instance
variables. ,# you change the value o# a static variable in a particular object, the value o# that variable changes #or all instances
o# that class.
Static methods can be re#erenced &ith the name o# the class rather than the name o# a particular object o# the class "though that
&or!s too%. That$s ho& library methods li!e System.out.println"% &or!. out is a static #ield in the java.lang.System class.

What is the difference between notif./ and notifAll./?
noti#y"% is used to unbloc! one &aiting thread6 noti#y*ll"% is used to unbloc! all o# them. Hsing noti#y"% is pre#erable "#or
e##iciency% &hen only one bloc!ed thread can bene#it #rom the change "#or e)ample, &hen #reeing a bu##er bac! into a pool%.
noti#y*ll"% is necessary "#or correctness% i# multiple threads should resume "#or e)ample, &hen releasing a A&riterA loc! on a
#ile might permit all AreadersA to resume%.

Wh can?t I sa *ust abs./ or sin./ instead of Hath6abs./ and Hath6sin./?
The import statement does not bring methods into your local name space. ,t lets you abbreviate class names, but not get rid o#
them altogether. That$s just the &ay it &or!s, you$ll get used to it. ,t$s really a lot sa#er this &ay.
/o&ever, there is actually a little tric! you can use in some cases that gets you &hat you &ant. ,# your top-level class doesn$t
need to inherit #rom anything else, ma!e it inherit #rom java.lang.Math. That PdoesP bring all the methods into your local
name space. 2ut you can$t use this tric! in an applet, because you have to inherit #rom java.a&t.*pplet. *nd actually, you can$t
use it on java.lang.Math at all, because Math is a A#inalA class &hich means it can$t be e)tended.

Wh are there no 'lobal variables in )ava?
Global variables are considered bad #orm #or a variety o# reasons. Z *dding state variables brea!s re#erential transparency "you
no longer can understand a statement or e)pression on its o&n. you need to understand it in the conte)t o# the settings o# the
global variables%.
Z State variables lessen the cohesion o# a program. you need to !no& more to understand ho& something &or!s. * major point
o# bject-riented programming is to brea! up global state into more easily understood collections o# local state.
Z When you add one variable, you limit the use o# your program to one instance. What you thought &as global, someone else
might thin! o# as local. they may &ant to run t&o copies o# your program at once.
8or these reasons, Java decided to ban global variables.

What does it mean that a class or member is final?
* #inal class can no longer be subclassed. Mostly this is done #or security reasons &ith basic classes li!e String and ,nteger. ,t
also allo&s the compiler to ma!e some optimi>ations, and ma!es thread sa#ety a little easier to achieve. Methods may be
declared #inal as &ell. This means they may not be overridden in a subclass.
8ields can be declared #inal, too. /o&ever, this has a completely di##erent meaning. * #inal #ield cannot be changed a#ter it$s
initiali>ed, and it must include an initiali>er statement &here it$s declared. 8or e)ample,
public #inal double c : F.VVJ6
,t$s also possible to ma!e a static #ield #inal to get the e##ect o# '(($s const statement or some uses o# '$s [de#ine, e.g. public
static #inal double c : F.VVJ6

What does it mean that a method or class is abstract?
*n abstract class cannot be instantiated. nly its subclasses can be instantiated. =ou indicate that a class is abstract &ith the
abstract !ey&ord li!e this.
public abstract class 'ontainer e)tends 'omponent 5
*bstract classes may contain abstract methods. * method declared abstract is not actually implemented in the current class. ,t
e)ists only to be overridden in subclasses. ,t has no body. 8or e)ample,
public abstract #loat price"%6
*bstract methods may only be included in abstract classes. /o&ever, an abstract class is not re3uired to have any abstract
methods, though most o# them do.
-ach subclass o# an abstract class must override the abstract methods o# its superclasses or itsel# be declared abstract.

What is the main difference between )ava "latform and other "latforms?
The Java plat#orm di##ers #rom most other plat#orms in that it$s a so#t&are-only plat#orm that runs on top o# other hard&are-
based plat#orms.
The Java plat#orm has three elements.
Java programming language
The Java Birtual Machine "Java BM%
The Java *pplication +rogramming ,nter#ace "Java *+,%

Ex"lain the new ,eatures of )+>C <69 Core API?
The J12' F.< *+, includes the complete J12' *+,, &hich includes both core and ptional +ac!age *+,, and provides
inductrial-strength database computing capabilities.
4e& 8eatures in J12' F.< 'ore *+,.
Scrollable result sets- using ne& methods in the DesultSet inter#ace allo&s programmatically move the to particular ro& or to a
position relative to its current position
J12' F.< 'ore *+, provides the 2atch Hpdates #unctionality to the java applications.
Java applications can no& use the DesultSet.updateSSS methods.
4e& data types - inter#aces mapping the SM0Q data types
'ustom mapping o# user-de#ined types "HT1s%
Miscellaneous #eatures, including per#ormance hints, the use o# character streams, #ull precision #or java.math.2ig1ecimal
values, additional security, and support #or time >ones in date, time, and timestamp values.

Ex"lain 'arba'e collection?
Garbage collection is one o# the most important #eature o# Java. Garbage collection is also called automatic memory
management as JBM automatically removes the unused variablesCobjects "value is null% #rom the memory. Hser program
cann$t directly #ree the object #rom memory, instead it is the job o# the garbage collector to automatically #ree the objects that
are no longer re#erenced by a program. -very class inherits #inali>e"% method #rom java.lang.bject, the #inali>e"% method is
called by garbage collector &hen it determines no more re#erences to the object e)ists. ,n Java, it is good idea to e)plicitly
assign null into a variable &hen no more in use. , Java on calling System.gc"% and Duntime.gc"%, JBM tries to recycle the
unused objects, but there is no guarantee &hen all the objects &ill garbage collected.

+escribe the "rinci"les of !!PS6
There are three main principals o# oops &hich are called +olymorphism, ,nheritance and -ncapsulation.

Ex"lain the Enca"sulation "rinci"le6
-ncapsulation is a process o# binding or &rapping the data and the codes that operates on the data into a single entity. This
!eeps the data sa#e #rom outside inter#ace and misuse. ne &ay to thin! about encapsulation is as a protective &rapper that
prevents code and data #rom being arbitrarily accessed by other code de#ined outside the &rapper.

Ex"lain the Polmor"hism "rinci"le6
The meaning o# +olymorphism is something li!e one name many #orms. +olymorphism enables one entity to be used as as
general category #or di##erent types o# actions. The speci#ic action is determined by the e)act nature o# the situation. The
concept o# polymorphism can be e)plained as Aone inter#ace, multiple methodsA.

Ex"lain the different forms of Polmor"hism6
8rom a practical programming vie&point, polymorphism e)ists in three distinct #orms in Java.
Method overloading
Method overriding through inheritance
Method overriding through the Java inter#ace

What are Access S"ecifiers available in )ava?
ccess speci#iers are !ey&ords that determines the type o# access to the member o# a class. These are.
+ublic
+rotected
+rivate
1e#aults

+escribe the wra""er classes in )ava6
Wrapper class is &rapper around a primitive data type. *n instance o# a &rapper class contains, or &raps, a primitive value o#
the corresponding type.
8ollo&ing table lists the primitive types and the corresponding &rapper classes.
+rimitive Wrapper
boolean java.lang.2oolean
byte java.lang.2yte
char java.lang.'haracter
double java.lang.1ouble
#loat java.lang.8loat
int java.lang.,nteger
long java.lang.0ong
short java.lang.Short
void java.lang.Boid

Wh are the interfaces more flexible than abstract classes?
*n inter#ace-de#ined type can be implemented by any class in a class hierarchy and can be e)tended by another inter#ace. ,n
contrast, an abstract-class-de#ined type can be implemented only by classes that subclass the abstract class.
--*n inter#ace-de#ined type can be used &ell in polymorphism. The so-called inter#ace type vs. implementation types.
--*bstract classes evolve more easily than inter#aces. ,# you add a ne& concrete method to an abstract class, the hierarchy
system is still &or!ing. ,# you add a method to an inter#ace, the classes that rely on the inter#ace &ill brea! &hen recompiled.
--Generally, use inter#aces #or #le)ibility6 use abstract classes #or ease o# evolution "li!e e)panding class #unctionality%.

What are new lan'ua'e features in )<SE =69?
Generally.
;. generics
F. static imports
Q. annotations
R. typesa#e enums
G. enhanced #or loop
I. autobo)ingCunbo)ing
K. varargs
J. covariant return types

What is covariant return t"e?
* covariant return type lets you override a superclass method &ith a return type that subtypes the superclass method$s return
type. So &e can use covariant return types to minimi>e upcasting and do&ncasting.
class +arent 5
+arent #oo "% 5
System.out.println "A+arent #oo"% calledA%6
return this6
7
7
class 'hild e)tends +arent 5
'hild #oo "% 5
System.out.println "A'hild #oo"% calledA%6
return this6
7
7
class 'ovariant 5
public static void main"StringTU args% 5
'hild c : ne& 'hild"%6
'hild cF : c.#oo"%6 CC cF is 'hild
+arent cQ : c.#oo"%6 CC cQ points to 'hild
7
7

What is a mar$er interface ?
*n inter#ace that contains no methods. -.g.. Seriali>able, 'loneable, SingleThreadModel etc. ,t is used to just mar! java
classes that support certain capability.

What are the restrictions "laced on static method ?
We cannot override static methods. We cannot access any object variables inside static method. *lso the this re#erence also
not available in static methods.

What is )EH?
JBM stands #or Java Birtual Machine. ,t is the run time #or java programs. *ll are java programs are running inside this JBM
only. ,t converts java byte code to S speci#ic commands. ,n addition to governing the e)ecution o# an application$s byte
codes, the virtual machine handles related tas!s such as managing the system$s memory, providing security against malicious
code, and managing multiple threads o# program e)ecution.

What is )I0?
J,T stands #or Just ,n Time compiler. ,t compiles java byte code to native code.

What are ClassAoaders?
* class loader is an object that is responsible #or loading classes. The class 'lass0oader is an abstract class. Given the name o#
a class, a class loader should attempt to locate or generate data that constitutes a de#inition #or the class. * typical strategy is to
trans#orm the name into a #ile name and then read a Aclass #ileA o# that name #rom a #ile system.
-very 'lass object contains a re#erence to the 'lass0oader that de#ined it.
'lass objects #or array classes are not created by class loaders, but are created automatically as re3uired by the Java runtime.
The class loader #or an array class, as returned by 'lass.get'lass0oader"% is the same as the class loader #or its element type6 i#
the element type is a primitive type, then the array class has no class loader.
*pplications implement subclasses o# 'lass0oader in order to e)tend the manner in &hich the Java virtual machine
dynamically loads classes.

What is Service Aocator "attern?
The Service 0ocator pattern locates JF-- "Java F +lat#orm, -nterprise -dition% services #or clients and thus abstracts the
comple)ity o# net&or! operation and JF-- service loo!up as -J2 "-nterprise Java2ean% ,ntervie& Muestions - /ome and JMS
"Java Message Service% component #actories. The Service 0ocator hides the loo!up process$s implementation details and
comple)ity #rom clients. To improve application per#ormance, Service 0ocator caches service objects to eliminate unnecessary
J41, "Java 4aming and 1irectory ,nter#ace% activity that occurs in a loo!up operation.

What is Session ,acade "attern?
Session #acade is one design pattern that is o#ten used &hile developing enterprise applications. ,t is implemented as a higher
level component "i.e.. Session -J2%, and it contains all the iteractions bet&een lo& level components "i.e.. -ntity -J2%. ,t then
provides a single inter#ace #or the #unctionality o# an application or part o# it, and it decouples lo&er level components
simpli#ying the design. Thin! o# a ban! situation, &here you have someone that &ould li!e to trans#er money #rom one
account to another. ,n this type o# scenario, the client has to chec! that the user is authori>ed, get the status o# the t&o
accounts, chec! that there are enough money on the #irst one, and then call the trans#er. The entire trans#er has to be done in a
single transaction other&ise is something goes south, the situation has to be restored.
*s you can see, multiple server-side objects need to be accessed and possibly modi#ied. Multiple #ine-grained invocations o#
-ntity "or even Session% 2eans add the overhead o# net&or! calls, even multiple transaction. ,n other &ords, the ris! is to have
a solution that has a high net&or! overhead, high coupling, poor reusability and mantainability.
The best solution is then to &rap all the calls inside a Session 2ean, so the clients &ill have a single point to access "that is the
session bean% that &ill ta!e care o# handling all the rest.
What is +ata Access !b*ect "attern?
The 1ata *ccess bject "or 1*% pattern. separates a data resource$s client inter#ace #rom its data access mechanisms adapts
a speci#ic data resource$s access *+, to a generic client inter#ace
The 1* pattern allo&s data access mechanisms to change independently o# the code that uses the data.
The 1* implements the access mechanism re3uired to &or! &ith the data source. The data source could be a persistent store
li!e an D12MS, an e)ternal service li!e a 2F2 e)change, a repository li!e an 01*+ database, or a business service accessed
via 'D2* ,nternet ,nter-D2 +rotocol ",,+% or lo&-level soc!ets. The business component that relies on the 1* uses the
simpler inter#ace e)posed by the 1* #or its clients. The 1* completely hides the data source implementation details #rom
its clients. 2ecause the inter#ace e)posed by the 1* to clients does not change &hen the underlying data source
implementation changes, this pattern allo&s the 1* to adapt to di##erent storage schemes &ithout a##ecting its clients or
business components. -ssentially, the 1* acts as an adapter bet&een the component and the data source.

Can we ma$e an E)> sin'leton?
This is a debatable 3uestion, and #or every ans&er &e propose there can be contradictions. , propose F solutions o# the same.
Demember that -J2$s are distributed components and can be deployed on di##erent JBM$s in a 1istributed environment
i% 8ollo& the steps as given belo&
Ma!e sure that your service0ocator is deployed on only one JBM.
,n the service0ocator create a /ashTableC/ashMap"=ou are the right judge to choose bet&een these t&o%
When ever a re3uest comes #or an -J2 to a service0ocator, it #irst chec!s in the /ashTable i# an entry already e)ists in the
table &ith !ey being the J41, name o# -J2. ,# !ey is present and value is not null, return the e)isting re#erence, else loo!up
the -J2 in J41, as &e do normally and add an entry into the /ashtable be#ore returning it to the client. This ma!es sure that
you maintain a singleton o# -J2.
ii% ,n distributed environment our componentsCJava bjects &ould be running on di##erent JBM$s. So the normal singleton
code &e &rite #or maintaining single instance &or!s #ine #or single JBM, but &hen the class could be loaded in multiple
JBM$s and ,nstantiated in multiple JBM$s normal singleton code does not &or!. This is because the 'lass0oaders being used
in the di##erent JBM$s are di##erent #rom each other and there is no de#ined mechanism to chec! and compare &hat is loaded in
another JBM. * solution could be"4ot tested yet. 4eed your #eedbac! on this% to &rite our o&n 'lass0oader and pass this
class0oader as argument, &henever &e are creating a ne& ,nstance and ma!e sure that only one instance is created #or the
proposed class. This can be done easily.

How can we ma$e a class Sin'leton ?
*% ,# the class is Seriali>able
class Singleton implements Seriali>able
5
private static Singleton instance6
private Singleton"% 5 7
public static synchroni>ed Singleton get,nstance"%
5
i# "instance :: null%
instance : ne& Singleton"%6
return instance6
7
CPP
,# the singleton implements Seriali>able, then this
P method must be supplied.
PC
protected bject readDesolve"% 5
return instance6
7
CPP
This method avoids the object #ro being cloned
PC
public bject clone"% 5
thro&s 'lone4otSupported-)ception 6
CCreturn instance6
7
7
2% ,# the class is 4T Seriali>able
class Singleton
5
private static Singleton instance6
private Singleton"% 5 7
public static synchroni>ed Singleton get,nstance"%
5
i# "instance :: null%
instance : ne& Singleton"%6
return instance6
7
CPP
This method avoids the object #rom being cloned
PPC
public bject clone"% 5
thro&s 'lone4otSupported-)ception 6
CCreturn instance6
7
7

How is static Snchronization different form non&static snchronization?
When Synchroni>ation is applied on a static Member or a static bloc!, the loc! is per#ormed on the 'lass and not on the
bject, &hile in the case o# a 4on-static bloc!Cmember, loc! is applied on the bject and not on class. TTrail F. There is a
class called 'lass in Java &hose object is associated &ith the object"s% o# your class. *ll the static members declared in your
class &ill have re#erence in this class"'lass%. *s long as your class e)ists in memory this object o# 'lass is also present. Thats
ho& even i# you create multiple objects o# your class only one 'lass object is present and all your objects are lin!ed to this
'lass object. -ven though one o# your object is G'ed a#ter some time, this object o# 'lass is not G'ed untill all the objects
associated &ith it are G'ed.
This means that &hen ever you call a Astatic synchroni>edA bloc!, JBM loc!s access to this 'lass object and not any o# your
objects. =our client can till access the non-static members o# your objects.

What are class members and Instance members?
*ny global members"Bariables, methods etc.% &hich are static are called as 'lass level members and those &hich are non-
static are called as ,nstance level members.

-ame few Iarba'e collection al'orithms?
/ere they go.
Mar! and S&eep
De#erence counting
Tracing collectors
'opying collectors
/eap compaction
Mar!-compact collectors

Can we force Iarba'e collection?
java #ollo&s a philosophy o# automatic garbage collection, you can suggest or encourage the JBM to per#orm garbage
collection but you can not #orce it. nce a variable is no longer re#erenced by anything it is available #or garbage collection.
=ou can suggest garbage collection &ith System.gc"%, but this does not guarantee &hen it &ill happen. 0ocal variables in
methods go out o# scope &hen the method e)its. *t this point the methods are eligible #or garbage collection. -ach time the
method comes into scope the local variables are re-created.

Wh 0hread is faster com"are to "rocess?
* thread is never #aster than a process. ,# you run a thread"say there$s a process &hich has spa&ned only one thread% in one
JBM and a process in another and that both o# them re3uire same resources then both o# them &ould ta!e same time to
e)ecute. 2ut, &hen a programC*pplication is thread based"remember here there &ill be multiple threads running #or a single
process% then de#inetly a thread based appliationCprogram is #aster than a process based application. This is because, &hen ever
a process re3uires or &aits #or a resource '+H ta!es it out o# the critical section and allocates the mute) to another process.
2e#ore deallocating the ealier one, it stores the conte)t"till &hat state did it e)ecute that process% in registers. 4o& i# this
deallocated process has to come bac! and e)ecute as it has got the resource #or &hich it &as &aiting, then it can$t go into
critical section directly. '+H as!s that process to #ollo& scheduling algorithm. So this process has to &ait again #or its turn.
While in the case o# thread based application, the application is still &ith '+H only that thread &hich re3uires some resource
goes out, but its co threads"o# same processCapllication% are still in the critical section. /ence it directly comes bac! to the '+H
and does not &ait outside. /ence an application &hich is thread based is #aster than an application &hich is process based.
2e sure that its not the competion bet&een thread and process, its bet&een an application &hich is thread based or process
based.

What are 'enerations in Iarba'e Collection terminolo'? What is its relevance?
Garbage 'ollectors ma!e assumptions about ho& our application runs. Most common assumption is that an object is most
li!ely to die shortly a#ter it &as created. called in#ant mortality. This assumes that an object that has been around #or a &hile,
&ill li!ely stay around #or a &hile. G' organi>es objects into generations "young, tenured, and perm%. This tells that i# an
object lives #or more than certain period o# time it is moved #rom one generation to another generations" say #rom young -@
tenured -@ permanent%. /ence G' &ill be run more #re3uently at the young generations and rarely at permanent generations.
This reduces the overhead on G' and gives #aster response time.

What is a 0hrou'h"ut Collector?
The throughput collector is a generational collector similar to the de#ault collector but &ith multiple threads used to do the
minor collection. The major collections are essentially the same as &ith the de#ault collector. 2y de#ault on a host &ith 4
'+Hs, the throughput collector uses 4 garbage collector threads in the collection. The number o# garbage collector threads can
be controlled &ith a command line option.

When to (se the 0hrou'h"ut Collector?
Hse the throughput collector &hen you &ant to improve the per#ormance o# your application &ith larger numbers o#
processors. ,n the de#ault collector garbage collection is done by one thread, and there#ore garbage collection adds to the serial
e)ecution time o# the application. The throughput collector uses multiple threads to e)ecute a minor collection and so reduces
the serial e)ecution time o# the application. * typical situation is one in &hich the application has a large number o# threads
allocating objects. ,n such an application it is o#ten the case that a large young generation is needed.

What is A''ressive Hea"?
The -SS.(*ggressive/eap option inspects the machine resources "si>e o# memory and number o# processors% and attempts to
set various parameters to be optimal #or long-running, memory allocation-intensive jobs. ,t &as originally intended #or
machines &ith large amounts o# memory and a large number o# '+Hs, but in the JFS- plat#orm, version ;.R.; and later it has
sho&n itsel# to be use#ul even on #our processor machines. With this option the throughput collector "-SS.(Hse+arallelG'% is
used along &ith adaptive si>ing "-SS.(Hse*daptiveSi>e+olicy%. The physical memory on the machines must be at least
FGIM2 be#ore *ggressive /eap can be used.

What is a Concurrent Aow Pause Collector?
The concurrent lo& pause collector is a generational collector similar to the de#ault collector. The tenured generation is
collected concurrently &ith this collector. This collector attempts to reduce the pause times needed to collect the tenured
generation. ,t uses a separate garbage collector thread to do parts o# the major collection concurrently &ith the applications
threads. The concurrent collector is enabled &ith the command line option -SS.(Hse'oncMar!S&eepG'. 8or each major
collection the concurrent collector &ill pause all the application threads #or a brie# period at the beginning o# the collection and
to&ard the middle o# the collection. The second pause tends to be the longer o# the t&o pauses and multiple threads are used to
do the collection &or! during that pause. The remainder o# the collection is done &ith a garbage collector thread that runs
concurrently &ith the application. The minor collections are done in a manner similar to the de#ault collector, and multiple
threads can optionally be used to do the minor collection.

When to (se the Concurrent Aow Pause Collector?
Hse the concurrent lo& pause collector i# your application &ould bene#it #rom shorter garbage collector pauses and can a##ord
to share processor resources &ith the garbage collector &hen the application is running. Typically applications &hich have a
relatively large set o# long-lived data "a large tenured generation%, and run on machines &ith t&o or more processors tend to
bene#it #rom the use o# this collector. /o&ever, this collector should be considered #or any application &ith a lo& pause time
re3uirement. ptimal results have been observed #or interactive applications &ith tenured generations o# a modest si>e on a
single processor.

What is Incremental Aow Pause Collector?
The incremental lo& pause collector is a generational collector similar to the de#ault collector. The minor collections are done
&ith the same young generation collector as the de#ault collector. 1o not use either -SS.(Hse+arallelG' or -SS.
(Hse+ar4e&G' &ith this collector. The major collections are done incrementally on the tenured generation. This collector
"also !no&n as the train collector% collects portions o# the tenured generation at each minor collection. The goal o# the
incremental collector is to avoid very long major collection pauses by doing portions o# the major collection &or! at each
minor collection. The incremental collector &ill sometimes #ind that a non-incremental major collection "as is done in the
de#ault collector% is re3uired in order to avoid running out o# memory.

When to (se the Incremental Aow Pause Collector?
Hse the incremental lo& pause collector &hen your application can a##ord to trade longer and more #re3uent young generation
garbage collection pauses #or shorter tenured generation pauses. * typical situation is one in &hich a larger tenured generation
is re3uired "lots o# long-lived objects%, a smaller young generation &ill su##ice "most objects are short-lived and don$t survive
the young generation collection%, and only a single processor is available.

How do ou enable the concurrent 'arba'e collector on Sun?s )EH?
-Sconcgc options allo&s us to use concurrent garbage collector ";.F.F9<K(%&e can also use -SS.(Hse'oncMar!S&eepG'
&hich is available beginning &ith JFS- ;.R.;.

What is a "latform?
* plat#orm is the hard&are or so#t&are environment in &hich a program runs. Most plat#orms can be described as a
combination o# the operating system and hard&are, li!e Windo&s F<<< and S+, 0inu), Solaris, and MacS.

What is transient variable?
Transient variable can$t be seriali>e. 8or e)ample i# a variable is declared as transient in a Seriali>able class and the class is
&ritten to an bjectStream, the value o# the variable can$t be &ritten to the stream instead &hen the class is retrieved #rom the
bjectStream the value o# the variable becomes null

How to ma$e a class or a bean serializable?
2y implementing either the java.io.Seriali>able inter#ace, or the java.io.-)ternali>able inter#ace. *s long as one class in a
class$s inheritance hierarchy implements Seriali>able or -)ternali>able, that class is seriali>able.

What is the difference between an Interface and an Abstract class?
*n abstract class can have instance methods that implement a de#ault behavior. *n ,nter#ace can only declare constants and
instance methods, but cannot implement de#ault behavior and all methods are implicitly abstract. *n inter#ace has all public
members and no implementation. *n abstract class is a class &hich may have the usual #lavors o# class members "private,
protected, etc.%, but has some abstract methods.

What is the "ur"ose of 'arba'e collection in )ava# and when is it used?
The purpose o# garbage collection is to identi#y and discard objects that are no longer needed by a program so that their
resources can be reclaimed and reused. * Java object is subject to garbage collection &hen it becomes unreachable to the
program in &hich it is used.

+escribe snchronization in res"ect to multithreadin'6
With respect to multithreading, synchroni>ation is the capability to control the access o# multiple threads to shared resources.
Without synchoni>ation, it is possible #or one thread to modi#y a shared variable &hile another thread is in the process o# using
or updating same shared variable. This usually leads to signi#icant errors.

Ex"lain different wa of usin' thread?
The thread could be implemented by using runnable inter#ace or by inheriting #rom the Thread class. The #ormer is more
advantageous, $cause &hen you are going #or multiple inheritance..the only inter#ace can help.

+ifference between HashHa" and Hash0able?
The /ashMap class is roughly e3uivalent to /ashtable, e)cept that it is unsynchroni>ed and permits nulls. "/ashMap allo&s
null values as !ey and value &hereas /ashtable doesnt allo&%. /ashMap does not guarantee that the order o# the map &ill
remain constant over time. /ashMap is unsynchroni>ed and /ashtable is synchroni>ed.

+ifference between Swin' and AW0?
*WT are heavy-&eight components. S&ings are light-&eight components. /ence s&ing &or!s #aster than *WT.

What is the difference between a constructor and a method?
* constructor is a member #unction o# a class that is used to create objects o# that class. ,t has the same name as the class itsel#,
has no return type, and is invo!ed using the ne& operator. * method is an ordinary member #unction o# a class. ,t has its o&n
name, a return type "&hich may be void%, and is invo!ed using the dot operator.

What is an Iterator?
Some o# the collection classes provide traversal o# their contents via a java.util.,terator inter#ace. This inter#ace allo&s you to
&al! through a collection o# objects, operating on each object in turn. Demember &hen using ,terators that they contain a
snapshot o# the collection at the time the ,terator &as obtained6 generally it is not advisable to modi#y the collection itsel#
&hile traversing an ,terator.

State the si'nificance of "ublic# "rivate# "rotected# default modifiers both sin'l and in combination and state the effect
of "ac$a'e relationshi"s on declared items %ualified b these modifiers6
public . +ublic class is visible in other pac!ages, #ield is visible every&here "class must be public too% private . +rivate
variables or methods may be used only by an instance o# the same class that declares the variable or method, * private #eature
may only be accessed by the class that o&ns the #eature. protected . ,s available to all classes in the same pac!age and also
available to all subclasses o# the class that o&ns the protected #eature. This access is provided even to subclasses that reside in
a di##erent pac!age #rom the class that o&ns the protected #eature. de#ault .What you get by de#ault ie, &ithout any access
modi#ier "ie, public private or protected%. ,t means that it is visible to all &ithin a particular pac!age.

What is an abstract class?
*bstract class must be e)tendedCsubclassed "to be use#ul%. ,t serves as a template. * class that is abstract may not be
instantiated "ie, you may not call its constructor%, abstract class may contain static data. *ny class &ith an abstract method is
automatically abstract itsel#, and must be declared as such.
* class may be declared abstract even i# it has no abstract methods. This prevents it #rom being instantiated.

What is static in *ava?
Static means one per class, not one #or each object no matter ho& many instance o# a class might e)ist. This means that you
can use them &ithout creating an instance o# a class. Static methods are implicitly #inal, because overriding is done based on
the type o# the object, and static methods are attached to a class, not an object. * static method in a super class can be
shado&ed by another static method in a subclass, as long as the original method &as not declared #inal. /o&ever, you can$t
override a static method &ith a no static method. ,n other &ords, you can$t change a static method into an instance method in a
subclass.

What is final?
* #inal class can$t be e)tended ie., #inal class may not be subclassed. * #inal method can$t be overridden &hen its class is
inherited. =ou can$t change value o# a #inal variable "is a constant%.

What if the main method is declared as "rivate?
The program compiles properly but at runtime it &ill give AMain method not public.A message.

How can one "rove that the arra is not null but em"t usin' one line of code?
+rint args.length. ,t &ill print <. That means it is empty. 2ut i# it &ould have been null then it &ould have thro&n a
4ull+ointer-)ception on attempting to print args.length.

Can an a""lication have multi"le classes havin' main method?
=es it is possible. While starting the application &e mention the class name to be run. The JBM &ill loo! #or the Main method
only in the class &hose name you have mentioned. /ence there is not con#lict amongst the multiple classes having main
method.

Can I have multi"le main methods in the same class?
4o the program #ails to compile. The compiler says that the main method is already de#ined in the class.

Can I im"ort same "ac$a'e/class twice? Will the )EH load the "ac$a'e twice at runtime?
ne can import the same pac!age or same class multiple times. 4either compiler nor JBM complains abt it. *nd the JBM &ill
internally load the class only once no matter ho& many times you import the same class.

What are Chec$ed and (nChec$ed Exce"tion?
* chec!ed e)ception is some subclass o# -)ception "or -)ception itsel#%, e)cluding class Duntime-)ception and its subclasses.
Ma!ing an e)ception chec!ed #orces client programmers to deal &ith the possibility that the e)ception &ill be thro&n. eg,
,-)ception thro&n by java.io.8ile,nputStream$s read"% methodZ Hnchec!ed e)ceptions are Duntime-)ception and any o# its
subclasses. 'lass -rror and its subclasses also are unchec!ed. With an unchec!ed e)ception, ho&ever, the compiler doesn$t
#orce client programmers either to catch the e)ception or declare it in a thro&s clause. ,n #act, client programmers may not
even !no& that the e)ception could be thro&n. eg, String,nde)ut#2ounds-)ception thro&n by String$s char*t"% methodZ
'hec!ed e)ceptions must be caught at compile time. Duntime e)ceptions do not need to be. -rrors o#ten cannot be.

What is !verridin'?
When a class de#ines a method using the same name, return type, and arguments as a method in its superclass, the method in
the class overrides the method in the superclass.
When the method is invo!ed #or an object o# the class, it is the ne& de#inition o# the method that is called, and not the method
de#inition #rom superclass. Methods may be overridden to be more public, not more private.

What are different t"es of inner classes?
4ested top-level classes, Member classes, 0ocal classes, *nonymous classes
4ested top-level classes- ,# you declare a class &ithin a class and speci#y the static modi#ier, the compiler treats the class just
li!e any other top-level class.
*ny class outside the declaring class accesses the nested class &ith the declaring class name acting similarly to a pac!age. eg,
outer.inner. Top-level inner classes implicitly have access only to static variables.There can also be inner inter#aces. *ll o#
these are o# the nested top-level variety.
Member classes - Member inner classes are just li!e other member methods and member variables and access to the member
class is restricted, just li!e methods and variables. This means a public member class acts similarly to a nested top-level class.
The primary di##erence bet&een member classes and nested top-level classes is that member classes have access to the speci#ic
instance o# the enclosing class.
0ocal classes - 0ocal classes are li!e local variables, speci#ic to a bloc! o# code. Their visibility is only &ithin the bloc! o#
their declaration. ,n order #or the class to be use#ul beyond the declaration bloc!, it &ould need to implement a more publicly
available inter#ace.2ecause local classes are not members, the modi#iers public, protected, private, and static are not usable.
*nonymous classes - *nonymous inner classes e)tend local inner classes one level #urther. *s anonymous classes have no
name, you cannot provide a constructor.

Are the im"orts chec$ed for validit at com"ile time? e6'6 will the code containin' an im"ort such as *ava6lan'6A>C+
com"ile?
=es the imports are chec!ed #or the semantic validity at compile time. The code containing above line o# import &ill not
compile. ,t &ill thro& an error saying,can not resolve symbol
symbol . class *2'1
location. pac!age io
import java.io.*2'16

+oes im"ortin' a "ac$a'e im"orts the sub"ac$a'es as well? e6'6 +oes im"ortin' com6H0est6L also im"ort
com6H0est6(nit0ests6L?
4o you &ill have to import the subpac!ages e)plicitly. ,mporting com.MyTest.P &ill import classes in the pac!age MyTest
only. ,t &ill not import any class in any o# it$s subpac!age.

What is the difference between declarin' a variable and definin' a variable?
,n declaration &e just mention the type o# the variable and it$s name. We do not initiali>e it. 2ut de#ining means declaration (
initiali>ation.
e.g String s6 is just a declaration &hile String s : ne& String "AabcdA%6 r String s : AabcdA6 are both de#initions.

What is the default value of an ob*ect reference declared as an instance variable?
4ull unless &e de#ine it e)plicitly.
Can a to" level class be "rivate or "rotected?
4o. * top level class can not be private or protected. ,t can have either ApublicA or no modi#ier. ,# it does not have a modi#ier it
is supposed to have a de#ault access.,# a top level class is declared as private the compiler &ill complain that the Amodi#ier
private is not allo&ed hereA. This means that a top level class can not be private. Same is the case &ith protected.

What is Externalizable interface?
-)ternali>able is an inter#ace &hich contains t&o methods read-)ternal and &rite-)ternal. These methods give you a control
over the seriali>ation mechanism. Thus i# your class implements this inter#ace, you can customi>e the seriali>ation process by
implementing these methods.

What are wra""er classes?
Java provides speciali>ed classes corresponding to each o# the primitive data types. These are called &rapper classes. They are
e.g. ,nteger, 'haracter, 1ouble etc.

Iive a sim"lest wa to find out the time a method ta$es for execution without usin' an "rofilin' tool?
Dead the system time just be#ore the method is invo!ed and immediately a#ter method returns. Ta!e the time di##erence, &hich
&ill give you the time ta!en by a method #or e)ecution.
To put it in code...
long start : System.currentTimeMillis "%6
method "%6
long end : System.currentTimeMillis "%6
System.out.println "ATime ta!en #or e)ecution is A ( "end - start%%6
Demember that i# the time ta!en #or e)ecution is too small, it might sho& that it is ta!ing >ero milliseconds #or e)ecution. Try
it on a method &hich is big enough, in the sense the one &hich is doing considerable amount o# processing.

Wh do we need wra""er classes?
,t is sometimes easier to deal &ith primitives as objects. Moreover most o# the collection classes store objects and not
primitive data types. *nd also the &rapper classes provide many utility methods also. 2ecause o# these reasons &e need
&rapper classes. *nd since &e create instances o# these classes &e can store them in any o# the collection classes and pass
them around as a collection. *lso &e can pass them around as method parameters &here a method e)pects an object.

What are runtime exce"tions?
Duntime e)ceptions are those e)ceptions that are thro&n at runtime because o# either &rong input data or because o# &rong
business logic etc. These are not chec!ed by the compiler at compile time.

What is the difference between error and an exce"tion?
*n error is an irrecoverable condition occurring at runtime. Such as ut#Memory error. These JBM errors and you can not
repair them at runtime. While e)ceptions are conditions that occur because o# bad input etc. e.g. 8ile4ot8ound-)ception &ill
be thro&n i# the speci#ied #ile does not e)ist. r a 4ull+ointer-)ception &ill ta!e place i# you try using a null re#erence. ,n
most o# the cases it is possible to recover #rom an e)ception "probably by giving user a #eedbac! #or entering proper values
etc.%.

How does an exce"tion "ermeate throu'h the code?
*n unhandled e)ception moves up the method stac! in search o# a matching When an e)ception is thro&n #rom a code &hich
is &rapped in a try bloc! #ollo&ed by one or more catch bloc!s, a search is made #or matching catch bloc!. ,# a matching type
is #ound then that bloc! &ill be invo!ed. ,# a matching type is not #ound then the e)ception moves up the method stac! and
reaches the caller method. Same procedure is repeated i# the caller method is included in a try catch bloc!. This process
continues until a catch bloc! handling the appropriate type o# e)ception is #ound. ,# it does not #ind such a bloc! then #inally
the program terminates.

What are the different was to handle exce"tions?
There are t&o &ays to handle e)ceptions,
;. 2y &rapping the desired code in a try bloc! #ollo&ed by a catch bloc! to catch the e)ceptions. and
F. 0ist the desired e)ceptions in the thro&s clause o# the method and let the caller o# the method handle those e)ceptions.

What is the basic difference between the < a""roaches to exce"tion handlin'6
86 tr catch bloc$ and
<6 s"ecifin' the candidate exce"tions in the throws clause?
When should ou use which a""roach?
,n the #irst approach as a programmer o# the method, you yoursel# are dealing &ith the e)ception. This is #ine i# you are in a
best position to decide should be done in case o# an e)ception. Whereas i# it is not the responsibility o# the method to deal &ith
it$s o&n e)ceptions, then do not use this approach. ,n this case use the second approach. ,n the second approach &e are #orcing
the caller o# the method to catch the e)ceptions, that the method is li!ely to thro&. This is o#ten the approach library creators
use. They list the e)ception in the thro&s clause and &e must catch them. =ou &ill #ind the same approach throughout the java
libraries &e use.

Is it necessar that each tr bloc$ must be followed b a catch bloc$?
,t is not necessary that each try bloc! must be #ollo&ed by a catch bloc!. ,t should be #ollo&ed by either a catch bloc! D a
#inally bloc!. *nd &hatever e)ceptions are li!ely to be thro&n should be declared in the thro&s clause o# the method.

If I write return at the end of the tr bloc$# will the finall bloc$ still execute?
=es even i# you &rite return as the last statement in the try bloc! and no e)ception occurs, the #inally bloc! &ill e)ecute. The
#inally bloc! &ill e)ecute and then the control return.

If I write Sstem6exit .9/2 at the end of the tr bloc$# will the finall bloc$ still execute?
4o in this case the #inally bloc! &ill not e)ecute because &hen you say System.e)it "<%6 the control immediately goes out o#
the program, and thus #inally never e)ecutes.

How are !bserver and !bservable used?
bjects that subclass the bservable class maintain a list o# observers. When an bservable object is updated it invo!es the
update"% method o# each o# its observers to noti#y the observers that it has changed state. The bserver inter#ace is
implemented by objects that observe bservable objects.

What is snchronization and wh is it im"ortant?
With respect to multithreading, synchroni>ation is the capability to control the access o# multiple threads to shared resources.
Without synchroni>ation, it is possible #or one thread to modi#y a shared object &hile another thread is in the process o# using
or updating that object$s value. This o#ten leads to signi#icant errors.

+oes 'arba'e collection 'uarantee that a "ro'ram will not run out of memor?
Garbage collection does not guarantee that a program &ill not run out o# memory. ,t is possible #or programs to use up
memory resources #aster than they are garbage collected. ,t is also possible #or programs to create objects that are not subject
to garbage collection .

What is the difference between "reem"tive schedulin' and time slicin'?
Hnder preemptive scheduling, the highest priority tas! e)ecutes until it enters the &aiting or dead states or a higher priority
tas! comes into e)istence. Hnder time slicing, a tas! e)ecutes #or a prede#ined slice o# time and then reenters the pool o# ready
tas!s. The scheduler then determines &hich tas! should e)ecute ne)t, based on priority and other #actors.

What is the difference between a while statement and a do statement?
* &hile statement chec!s at the beginning o# a loop to see &hether the ne)t loop iteration should occur. * do statement chec!s
at the end o# a loop to see &hether the ne)t iteration o# a loop should occur. The do statement &ill al&ays e)ecute the body o#
a loop at least once.
What are snchronized methods and snchronized statements?
Synchroni>ed methods are methods that are used to control access to an object. * thread only e)ecutes a synchroni>ed method
a#ter it has ac3uired the loc! #or the method$s object or class. Synchroni>ed statements are similar to synchroni>ed methods. *
synchroni>ed statement can only be e)ecuted a#ter a thread has ac3uired the loc! #or the object or class re#erenced in the
synchroni>ed statement.

What is daemon thread and which method is used to create the daemon thread?
1aemon thread is a lo& priority thread &hich runs intermittently in the bac! ground doing the garbage collection operation #or
the java runtime system. set1aemon method is used to create a daemon thread.

Can a""lets communicate with each other?
*t this point in time applets may communicate &ith other applets running in the same virtual machine. ,# the applets are o# the
same class, they can communicate via shared static variables. ,# the applets are o# di##erent classes, then each &ill need a
re#erence to the same class &ith static variables. ,n any case the basic idea is to pass the in#ormation bac! and #orth through a
static variable.
*n applet can also get re#erences to all other applets on the same page using the get*pplets"% method o#
java.applet.*pplet'onte)t. nce you get the re#erence to an applet, you can communicate &ith it by using its public members.
,t is conceivable to have applets in di##erent virtual machines that tal! to a server some&here on the ,nternet and store any data
that needs to be seriali>ed there. Then, &hen another applet needs this data, it could connect to this same server. ,mplementing
this is non-trivial.

What are the ste"s in the )+>C connection?
While ma!ing a J12' connection &e go through the #ollo&ing steps .
Step ; . Degister the database driver by using .
'lass.#or4ame"?A driver classs #or that speci#ic database?A %6
Step F . 4o& create a database connection using .
'onnection con : 1riverManager.get'onnection"url,username,pass&ord%6
Step Q. 4o& 'reate a 3uery using .
Statement stmt : 'onnection.Statement"?Aselect P #rom T*20- 4*M-?A%6
Step R . -)ceute the 3uery .
stmt.e)ceuteHpdate"%6
How does a tr statement determine which catch clause should be used to handle an exce"tion?
When an e)ception is thro&n &ithin the body o# a try statement, the catch clauses o# the try statement are e)amined in the
order in &hich they appear. The #irst catch clause that is capable o# handling the e)ceptionis e)ecuted. The remaining catch
clauses are ignored.

What are some alternatives to inheritance?
1elegation is an alternative to inheritance. 1elegation means that you include an instance o# another class as an instance
variable, and #or&ard messages to the instance. ,t is o#ten sa#er than inheritance because it #orces you to thin! about each
message you #or&ard, because the instance is o# a !no&n class, rather than a ne& class, and because it doesn$t #orce you to
accept all the methods o# the super class. you can provide only the methods that really ma!e sense. n the other hand, it ma!es
you &rite more code, and it is harder to re-use "because it is not a subclass%.

What does it mean that a method or field is BstaticB?
Static variables and methods are instantiated only once per class. ,n other &ords they are class variables, not instance
variables. ,# you change the value o# a static variable in a particular object, the value o# that variable changes #or all instances
o# that class.
Static methods can be re#erenced &ith the name o# the class rather than the name o# a particular object o# the class "though that
&or!s too%. That$s ho& library methods li!e System.out.println"% &or! out is a static #ield in the java.lang.System class.

What is the difference between "reem"tive schedulin' and time slicin'?
Hnder preemptive scheduling, the highest priority tas! e)ecutes until it enters the &aiting or dead states or a higher priority
tas! comes into e)istence. Hnder time slicing, a tas! e)ecutes #or a prede#ined slice o# time and then reenters the pool o# ready
tas!s. The scheduler then determines &hich tas! should e)ecute ne)t, based on priority and other #actors.

What are the different sco"es for )ava variables?
The scope o# a Java variable is determined by the conte)t in &hich the variable is declared. Thus a java variable can have one
o# the three scopes at any given point in time.
;. ,nstance . - These are typical object level variables, they are initiali>ed to de#ault values at the time o# creation o# object, and
remain accessible as long as the object accessible.
F. 0ocal . - These are the variables that are de#ined &ithin a method. They remain accessible only during the course o# method
e)ecution. When the method #inishes e)ecution, these variables #all out o# scope.
Q. Static. - These are the class level variables. They are initiali>ed &hen the class is loaded in JBM #or the #irst time and
remain there as long as the class remains loaded. They are not tied to any particular object instance.

What is the default value of the local variables?
The local variables are not initiali>ed to any de#ault value, neither primitives nor object re#erences. ,# you try to use these
variables &ithout initiali>ing them e)plicitly, the java compiler &ill not compile the code. ,t &ill complain about the local
variable not being initiali>ed..

What will be the default values of all the elements of an arra defined as an instance variable?
,# the array is an array o# primitive types, then all the elements o# the array &ill be initiali>ed to the de#ault value
corresponding to that primitive type. e.g. *ll the elements o# an array o# int &ill be initiali>ed to <, &hile that o# 2oolean type
&ill be initiali>ed to #alse. Whereas i# the array is an array o# re#erences "o# any type%, all the elements &ill be initiali>ed to
null.

S-ar putea să vă placă și