You are on page 1of 8

JAVA INTERVIEW QUESTIONS

What is Java?
Java is an object-oriented programming language developed initially by James Gosling and
colleagues at Sun Microsystems. The language, initially called Oak named a!ter the oak trees
outside Gosling"s o!!ice#, $as intended to replace %&&, although the !eature set better resembles
that o! Objective %. Java should not be con!used $ith JavaScript, $hich shares only the name
and a similar %-like synta'. Sun Microsystems currently maintains and updates Java regularly.
What does a well-written OO program look like?
( $ell-$ritten OO program e'hibits recurring structures that promote abstraction, !le'ibility,
modularity and elegance.
Can you have virtual functions in Java?
)es, all !unctions in Java are virtual by de!ault. This is actually a pseudo trick *uestion because
the $ord +virtual+ is not part o! the naming convention in Java as it is in %&&, %-sharp and
,-../T#, so this $ould be a !oreign concept !or someone $ho has only coded in Java. ,irtual
!unctions or virtual methods are !unctions or methods that $ill be rede!ined in derived classes.
Jack developed a program by using a Map container to hold key/value pairs !e wanted to
make a change to the map !e decided to make a clone of the map in order to save the
original data on side What do you think of it? ?
0! Jack 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 Jack made a $rong decision.
What is more advisable to create a thread" by implementing a #unnable interface or by
e$tending %hread class?
Strategically speaking, threads created by implementing 1unnable inter!ace are more advisable.
0! you create a thread by e'tending a thread class, you cannot e'tend any other class. 0! you
create a thread by implementing 1unnable inter!ace, you save a space !or your class to e'tend
another class no$ or in !uture.
What is &ull'ointer($ception and how to handle it?
2hen an object is not initiali3ed, the de!ault value is null. 2hen the !ollo$ing things happen, the
.ull4ointer/'ception is thro$n5
--%alling the instance method o! a null object.
--(ccessing or modi!ying the !ield o! a null object.
--Taking 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 .ull4ointer/'ception is a runtime e'ception. The best practice is to catch such e'ception
even i! it is not re*uired by language design.
)n application needs to load a library before it starts to run" how to code?
One option is to use a static block to load a library be!ore anything is called. 6or e'ample,
class Test 7
static 7
System.load8ibrary+path-to-library-!ile+#9
:
....
:
2hen you call ne$ Test#, the static block $ill be called !irst be!ore any initiali3ation happens.
.ote that the static block position may matter.
!ow could Java classes direct program messages to the system console" but error messages"
say to a file?
The class System has a variable out that represents the standard output, and the variable err that
represents the standard error device. -y de!ault, they both point at the system console. This ho$
the standard output could be re-directed5
Stream st ; ne$ Streamne$ 6ileOutputStream+output.t't+##9 System.set/rrst#9
System.setOutst#9
What*s the difference between an interface and an abstract class?
(n abstract class may contain code in method bodies, $hich is not allo$ed in an inter!ace. 2ith
abstract classes, you have to inherit your class !rom it and Java does not allo$ multiple
inheritance. On the other hand, you can implement multiple inter!aces in your class.
&ame the containers which uses +order ,ayout as their default layout?
%ontainers $hich uses -order 8ayout as their de!ault are5 $indo$, 6rame and <ialog classes.
What do you understand by -ynchroni.ation?
Synchroni3ation 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. 0n non synchroni3ed
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.
Synchroni3ation prevents such type o! data corruption.
/.g. Synchroni3ing a !unction5
public synchroni3ed void Method= # 7
>> (ppropriate method-related code.
:
/.g. Synchroni3ing a block o! code inside a !unction5
public my6unction #7
synchroni3ed this# 7
>> Synchroni3ed code here.
:
:
What is Collection )'/ ?
The %ollection (40 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! classes5 ?ashSet, ?ashMap, (rray8ist, 8inked8ist, TreeSet and TreeMap.
/'ample o! inter!aces5 %ollection, Set, 8ist and Map.
/s /terator a Class or /nterface? What is its use?
(ns$er5 0terator is an inter!ace $hich is used to step through the elements o! a %ollection.
What is similarities/difference between an )bstract class and /nterface?
<i!!erences are as !ollo$s5
0nter!aces provide a !orm o! multiple inheritance. ( class can e'tend only one other class.
0nter!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. -ut in case o! abstract class, a class may e'tend only
one abstract class. 0nter!aces are slo$ as it re*uires e'tra indirection to to !ind corresponding
method in in the actual class. (bstract classes are !ast.
Similarities5
.either (bstract classes or 0nter!ace can be instantiated.
Java /nterview 0uestions - !ow to define an )bstract class?
( class containing abstract method is called (bstract class. (n (bstract class can"t be
instantiated.
/'ample o! (bstract class5
abstract class test(bstract%lass 7
protected String myString9
public String getMyString# 7
return myString9
:
public abstract string any(bstract6unction#9
:
!ow to define an /nterface in Java ?
0n Java 0nter!ace de!ines the methods but does not implement them. 0nter!ace can include
constants. ( class that implements the inter!aces is bound to implement all the methods de!ined
in 0nter!ace.
/maple o! 0nter!ace5
public inter!ace sample0nter!ace 7
public void !unctionOne#9
public long %O.ST(.T@O./ ; =AAA9
:
/f a class is located in a package" what do you need to change in the O- environment to be
able to use it?
)ou need to add a directory or a jar !ile that contains the package directories to the
%8(SS4(T? environment variable. 8et"s say a class /mployee belongs to a package
com.'y3.hr9 and is located in the !ile c5BdevBcomB'y3BhrB/mployee.java. 0n this case, you"d need
to add c5Bdev to the variable %8(SS4(T?. 0! this class contains the method main#, you could
test it !rom a command prompt $indo$ as !ollo$s5
c5BCjava com.'y3.hr./mployee
!ow many methods in the -eriali.able interface?
There is no method in the Seriali3able inter!ace. The Seriali3able inter!ace acts as a marker,
telling the object seriali3ation tools that your class is seriali3able.
!ow many methods in the ($ternali.able interface?
There are t$o methods in the /'ternali3able inter!ace. )ou have to implement these t$o
methods in order to make your class e'ternali3able. These t$o methods are read/'ternal# and
$rite/'ternal#.
What is the difference between -eriali.alble and ($ternali.able interface?
2hen you use Seriali3able inter!ace, your class is seriali3ed automatically by de!ault. -ut you
can override $riteObject# and readObject# t$o methods to control more comple' object
seraili3ation process. 2hen you use /'ternali3able inter!ace, you have a complete control over
your class"s seriali3ation process.
What is a transient variable in Java?
( transient variable is a variable that may not be seriali3ed. 0! you don"t $ant some !ield to be
seriali3ed, you can mark that !ield transient or static.
Which containers use a border layout as their default layout?
The 2indo$, 6rame and <ialog classes use a border layout as their de!ault layout.
!ow are Observer and Observable used?
Objects that subclass the Observable class maintain a list o! observers. 2hen an Observable
object is updated, it invokes the update# method o! each o! its observers to noti!y the observers
that it has changed state. The Observer inter!ace is implemented by objects that observe
Observable objects.
What is synchroni.ation and why is it important?
2ith respect to multithreading, synchroni3ation is the capability to control the access o! multiple
threads to shared resources. 2ithout synchroni3ation, 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 synchroni.ed methods and synchroni.ed statements?
Synchroni3ed methods are methods that are used to control access to a method or an object. (
thread only e'ecutes a synchroni3ed method a!ter it has ac*uired the lock !or the method"s object
or class. Synchroni3ed statements are similar to synchroni3ed methods. ( synchroni3ed
statement can only be e'ecuted a!ter a thread has ac*uired the lock !or the object or class
re!erenced in the synchroni3ed statement.
What are three ways in which a thread can enter the waiting state?
( thread can enter the $aiting state by invoking its sleep# method, by blocking on 0O, by
unsuccess!ully attempting to ac*uire an object"s lock, or by invoking an object"s $ait# method. 0t
can also enter the $aiting state by invoking its deprecated# suspend# method.
Can a lock be ac1uired on a class?
)es, a lock can be ac*uired on a class. This lock is ac*uired on the class"s %lass object.
What*s new with the stop23" suspend23 and resume23 methods in J45 67?
The stop#, suspend# and resume# methods have been deprecated in J<D =.E.
What is the preferred si.e of a component?
The pre!erred si3e o! a component is the minimum component si3e that $ill allo$ the component
to display normally.
What*s the difference between J7-45 68 and J7-45 89?
There"s no di!!erence, Sun Microsystems just re-branded this version.
What would you use to compare two -tring variables - the operator :: or the method
e1uals23?
0"d use the method e*uals# to compare the values o! the Strings and the ;; to check 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.
!ow does multi-threading take place on a computer with a single C';?
The operating system"s task scheduler allocates e'ecution time to multiple tasks. -y *uickly
s$itching bet$een e'ecuting tasks, it creates the impression that tasks e'ecute se*uentially.
!ow to create a thread in a program?
)ou have t$o $ays to do so. 6irst, making your class +e'tends+ Thread class. Second, making
your class +implements+ 1unnable inter!ace. 4ut jobs in a run# method and call start# method to
start the thread.
Can Java ob<ect be locked down for e$clusive use by a given thread?
)es. )ou can lock an object by putting it in a +synchroni3ed+ block. The locked object is
inaccessible to any thread other than the one that e'plicitly claimed it.
Can each Java ob<ect keep track of all the threads that want to e$clusively access to it?
)es. Fse Thread.currentThread# method to track the accessing thread.
4oes it matter in what order catch statements for =ile&ot=ound($ception and
/O($ceptipon are written?
)es, it does. The 6ile.o6ound/'ception is inherited !rom the 0O/'ception. /'ception"s
subclasses have to be caught !irst.
What invokes a thread*s run23 method?
(!ter a thread is started, via its start# method o! the Thread class, the J,M invokes the thread"s
run# method $hen the thread is initially e'ecuted.
What is the purpose of the wait23" notify23" and notify)ll23 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 difference between yielding and sleeping?
2hen a task invokes its yield# method, it returns to the ready state. 2hen a task invokes its
sleep# method, it returns to the $aiting state.
What happens when a thread cannot ac1uire a lock on an ob<ect?
0! a thread attempts to e'ecute a synchroni3ed method or synchroni3ed statement and is unable to
ac*uire an object"s lock, it enters the $aiting state until the lock becomes available.
What is the difference between 'rocess and %hread?
( process can contain multiple threads. 0n most multithreading operating systems, a process gets
its o$n memory address space9 a thread doesn"t. Threads typically share the heap belonging to
their parent process. 6or instance, a J,M runs in a single process in the host O>S. Threads in the
J,M share the heap belonging to that process9 that"s $hy several threads may access the same
object. Typically, even though they share a common heap, threads have their o$n stack space.
This is ho$ one thread"s invocation o! a method is kept separate !rom another"s. This is all a
gross oversimpli!ication, but it"s accurate enough at a high level. 8ots o! details di!!er bet$een
operating systems. 4rocess vs. Thread ( program vs. similar to a se*uential program an run on
its o$n vs. %annot run on its o$n Fnit o! allocation vs. Fnit 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. One process cannot corrupt another process vs. 8ess 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?
0t"s possible i! these variables are !inal.
What can go wrong if you replace >emp?>emp? with >emp? in the following code@ -tring
a:null? if 2aA:null >> alength23B693 CD
( single ampersand here $ould lead to a .ull4ointer/'ception.
What is the Eector class?
The ,ector class provides the capability to implement a gro$able array o! objects
What modifiers may 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.
/f a method is declared as protected" where may the method be accessed?
( protected method may only be accessed by classes or inter!aces o! the same package or by
subclasses o! the class in $hich it is declared.
What is an /terator interface?
The 0terator inter!ace is used to step through the elements o! a %ollection.
!ow many bits are used to represent ;nicode" )-C//" ;%=-6F" and ;%=-G characters?
Fnicode re*uires =G bits and (S%00 re*uire H bits. (lthough the (S%00 character set uses only H
bits, it is usually represented as I bits. FT6-I represents characters using I, =G, and =I bit
patterns. FT6-=G uses =G-bit and larger bit patterns.
What*s the main difference between a Eector and an )rray,ist?
Java ,ector class is internally synchroni3ed and (rray8ist is not.
What are wrapped classes?
2rapped classes are classes that allo$ primitive types to be accessed as objects.
4oes garbage collection guarantee that a program will not run out of memory?
.o, it doesn"t. 0t is possible !or programs to use up memory resources !aster than they are
garbage collected. 0t is also possible !or programs to create objects that are not subject to garbage
collection.
What is the difference between preemptive scheduling and time slicing?
Fnder preemptive scheduling, the highest priority task e'ecutes until it enters the $aiting or dead
states or a higher priority task comes into e'istence. Fnder time slicing, a task e'ecutes !or a
prede!ined slice o! time and then reenters the pool o! ready tasks. The scheduler then determines
$hich task should e'ecute ne't, based on priority and other !actors.
J ?ome J 6orum-?elp J Mail -o' K Other Services J GroupsJ %ontact Fs J Sitemap J
L EAAM - - /% )cumens
( 6ree 1esource Sharing 4lace 6or /ngineers.

You might also like