Está en la página 1de 10

Java interview questions

What are Native methods in Java?


Java applications can call code written in C, C++, or assembler. This is sometimes
done for performance and sometimes to access the underlying host operating system
or GUI !I using the J"I.
The steps for doing that are#
$irst write the Java code and compile it
Then create a C header file
Create C stubs file
%rite the C code
Create shared code library &or '(()
*un application
What are class loaders?
The class loader describes the behavior of converting a named class into the bits
responsible for implementing that class.
Class loaders eradicate the J*+s need to ,now anything about files and file systems
when running Java programs.
class loader creates a flat name space of class bodies that are referenced by a
string name and are written as#
Class r - loadClass&.tring class"ame, boolean resolveIt)/
What is Reflection API in Java?
The *eflection !I allows Java code to e0amine classes and ob1ects at run time. The
new reflection classes allow you to call another class2s methods dynamically at run
time. %ith the reflection classes, you can also e0amine an instance2s fields and
change the fields2 contents.
The *eflection !I consists of the 1ava.lang.Class class and the 1ava.lang.reflect
classes# $ield, 3ethod, Constructor, rray, and 3odifier.
Explain the difference between static and dnamic class loadin!"
The static class loading is done through the new operator.
'ynamic class loading is achieved through *un time type identification. lso called as
reflection
This is done with the help of the following methods#
getClass&)/ get"ame&)/ get'eclared$ields&)/
Instance can also be created using for"ame&) method. It loads the class into the
current class memory.
Explain #hallow and deep clonin!"
Cloning of ob1ects can be very useful if you use the prototype pattern or if you want
to store an internal copy of an ob1ect inside an aggregation class for e0ample.
'eep cloning 4 5ou clone the ob1ect and their constituent parts.
It should be used when it is inappropriate to separate the parts/ the ob1ect is formed
of, from it.
.hallow cloning 4 5ou clone only the ob1ect, not their parts. 5ou add references to
their parts.
It should be used when it is ade6uate to have the references added to the cloned
ob1ect
What is the purpose of $omparator Interface?
Comparators can be used to control the order of certain data structures and
collection of ob1ects too.
The interface can be found in 1ava.util.Comparator
Comparator must define a compare function which ta,es two 7b1ects and returns a
48, 9, or 8
.orting can be done implicitly by using data structures of by implementing sort
methods e0plicitly.
Explain the impact of private constructor"
!rivate Constructors can2t be access from any derived classes neither from another
class.
.o you have to provide a public function that calls the private constructor if the
ob1ect has not been initiali:ed, or you have to return an instance to the ob1ect, if it
was initiali:ed.
This can be useful for ob1ects that can2t be instantiated.
What are static Initiali%ers?
static initiali:er bloc, resembles a method with no name, no arguments, and no
return type. There is no need to refer to it from outside the class definition.
.ynta0#
static
;
<<C7'+
=
The code in a static initiali:er bloc, is e0ecuted by the virtual machine when the class
is loaded.
>ecause it is e0ecuted automatically when the class is loaded, parameters don2t
ma,e any sense, so a static initiali:er bloc, doesn2t have an argument list.
&efine the purpose of Externali%able Interface"
The +0terni:able interface e0tends the seriali:able interface.
%hen you use .eriali:able interface, your class is seriali:ed automatically by default.
>ut you can override write7b1ect&) and read7b1ect&)two methods to control more
comple0 ob1ect seraili:ation process.
%hen you use +0ternali:able interface, you have a complete control over your class2s
seriali:ation process. The two methods to be implemented are #
void read+0ternal&7b1ectInput)
The ob1ect implements the read+0ternal method to restore its contents by calling the
methods of 'ataInput for primitive types and read7b1ect for ob1ects, strings and
arrays.
void write+0ternal&7b1ect7utput)
The ob1ect implements the write+0ternal method to save its contents by calling the
methods of 'ata7utput for its primitive values or calling the write7b1ect method of
7b1ect7utput for ob1ects, strings, and arrays.
What are transient and volatile modifiers?
%hen seriali:able interface is declared, the compiler ,nows that the ob1ect has to be
handled so as so be able to seriali:e it. ?owever, if you declare a variable in an
ob1ect as transient, then it doesn@t get seriali:ed.
Aolatile
.pecifying a variable as volatile tells the JA3 that any threads using that variable are
not allowed to cache that value at all.
Aolatile modifier tells the compiler that the variable modified by volatile can be
changed une0pectedly by other parts of the program.
What are daemon threads?
Threads that wor, in the bac,ground to support the runtime environment are called
daemon threads.
+g garbage collector threads.
%hen the only remaining threads in a process are daemon threads, the interpreter
e0its. This ma,es sense because when only daemon threads remain, there is no
other thread for which a daemon thread can provide a service.
5ou cannot create a daemon method but you can use
public final void set'aemon&boolean is'aemon) method to turn it into one.
What is JA'Adoc utilit?
Javadoc utility enables you to ,eep the code and the documentation in sync easily.
The 1avadoc utility lets you put your comments right ne0t to your code, inside your
B.1avaB source files.
ll you need to do after completing your code is to run the Javadoc utility to create
your ?T3( documentation automatically.
Explain the difference between #trin!(uilder and #trin!(uffer class"
.tring>uilder is unsynchroni:ed whereas .tring>uffer is synchroni:ed. .o when the
application needs to be run only in a single thread then it is better to use
.tring>uilder.
.tring>uilder is more efficient than .tring>uffer.
Explain semaphore and monitors in )ava threadin!"
semaphore is a flag variable used to chec, whether a resource is currently being
used by another thread or process.
The drawbac, of semaphores is that there is no control or guarantee of proper
usage.
3onitor defines a loc, and condition variables for managing concurrent access to
shared data. The monitor uses the loc, to ensure that only a single thread is active
in the monitor code at any time.
semaphore is a generali:ation of a monitor. monitor allows only one thread to
loc, an ob1ect at once.
&escribe snchroni%ation in respect to multithreadin!"
3ultithreading occurs asynchronously, meaning one thread e0ecutes independently
of the other threads. In this way, threads don@t depend on each other@s e0ecution. In
contrast, processes that run synchronously depend on each other. That is, one
process waits until the other process terminates before it can e0ecute
What are $hec*ed and +n$hec*ed Exception?
The 1ava.lang.Throwable class has two subclasses +rror and +0ception. There are two
types of e0ceptions non runtime e0ceptions and runtime e0ceptions. "on runtime
e0ceptions are called chec,ed e0ceptions and the unchec,ed e0ceptions are runtime
e0ceptions.
*untime +0ceptions occur when the code is not robust and non runtime e0ceptions
occur due to the problems is environment, settings, etc.
What are different tpes of inner classes?
(ocal classes 4 (ocal classes are li,e local variables, specific to a bloc, of code. Their
visibility is only within the bloc, of their declaration
3ember classes 4 3ember inner classes are 1ust li,e other member methods and
member variables and access to the member class is restricted, 1ust li,e methods
and variables.
nonymous classes 4 nonymous classes have no name, you cannot even provide a
constructor.
What is seriali%able Interface?
If we want to transfer data over a networ, then it needs to be seriali:ed. 7b1ects
cannot be transferred as they are. ?ence, we need to declare that a class
implements seriali:able so that a compiler ,nows that the data needs to be
seriali:ed.
,ow does thread snchroni%ation occurs inside a monitor?
3onitor defines a loc, and condition variables for managing concurrent access to
shared data. The monitor uses the loc, to ensure that only a single thread is active
in the monitor code at any time. monitor allows only one thread to loc, an ob1ect
at once.
What is the difference between AW- and #win!?
Classes in swing are not 7. dependent. They don@t create peer components, so they
are light weight unli,e %T.
They don@t ta,e the loo, and feel of the target platform so they have a consistent
appearance
What is meant b #tream -o*eni%er?
The .treamTo,eni:er class ta,es an input stream and parses it into Bto,ensB,
allowing the to,ens to be read one at a time. The parsing process is controlled by a
table and a number of flags that can be set to various states. The stream to,eni:er
can recogni:e identifiers, numbers, 6uoted strings, and various comment styles.
What is resource bundle?
The place where an application stores its locale4specific data &isolated from source
code).
%hen your program needs a locale4specific resource your program can load it from
the resource bundle that is appropriate for the current user2s locale.
What is a thread? What are the advanta!es we derived b pro!rammin!
with thread?
Threads allow programs to e0ecute simultaneously. thread is an independent path
of e0ecution in a program. These threads can be e0ecuted synchronously or
asynchronously. ll threads have a priority attached. Thread with a higher priority is
e0ecuted first.
dvantages#
C llows multiple programs to be e0ecuted concurrently
C Cost of thread is low compared to processes in terms of space and
communication.
C Threads are lightweight.
Explain how to create a thread and start it runnin!"
Creating a thread#
'eclare a class as a sub class of Thread
Class .ampleThread e0tends Thread
;
(ong min.ample/ ;
.ampleThread& (ong min.ample)/
=
!ublic void run&)
;
!rogram goes here
=
=
*un the thread# n instance is created to start the thread.
.ampleThread s - new .ampleThread&899)/
s.start&)/
What is multithreaded pro!ram? What is the importance of thread
snchroni%ation?
multithreaded program involves multiple threads of control in a single program.
+ach thread has its own stac,. 7ther resources of the process are shared by all
threads and while trying to access them it should be synchroni:ed.
Importance of thread synchroni:ation#
voids issues li,e deadloc,s and starvation
Can be used for de bugging multi threaded programs
What is the difference between ieldin! and sleepin!?
.leep holds the threads e0ecution for the specified time. 7n the other hand, yield will
cause the thread to re1oin the 6ueue. %hen a tas, is invo,ed in yielding, it returns to
the ready state. %hile when a tas, is invo,ed in sleeping, it returns to the waiting
state.
What is the difference between $.. / Java?
C Java does not support +nums, .tructures or Unions but supports classes.
C Java does not support multiple inheritance or operator overloading
C Java allows functions to be overloaded
C In 1ava, memory lea,s are prevented by automatic garbage collection
C C++ allows direct calls to be made to the native system libraries. 7n the
other hand 1ava calls these libraries trough 1ava native interface.
C In c++ parameters are passed by either value or pointers. In 1ava, due to the
absence of pointers, parameters are passed by value.
What is JAR file?
J* is a Java rchived file which allows many files to be stored. ll applets and
classes can be stored in a J* file thereby reducing the si:e. J* files can be created
using the J* command that comes with J'D. This J* file can also be digitally
signed. In this case, the J* file itself is not signed. >ut, all the files inside it are
signed.
What is JNI?
Java "ative Interface is a framewor, that allows the Java code running in the Java
Airtual 3achine to interact and communicate with other applications and libraries
written in some other languages. J"I is typically used when an application cannot be
entirely written in Java. J"I can be invo,ed only by signed applets and applications.
What is seriali%ation?
.eriali:ation is an operation in which an ob1ect@s internal state is converted into a
stream of bytes. This stream is then written to the dis,. This stream can also be sent
over a soc,et. .eriali:ation is a very compact and accurate method. $or any ob1ect to
be seriali:ed .it must be an instance of a class that implements either the .eriali:able
or +0ternali:able interface.
Uses of seriali:ation#
?elps to persist data for future use
To e0change data between servlets and applets
To store users session
,ow are 0bserver and 0bservable used?
The observer pattern in 1ava is ,nown for its use in design. %henever an observable
ob1ect changes its state, its corresponding observer classes are notified. 7bservable
is implemented as a class which includes methods for managing 7bserver lists and
notifying 7bservers. %hen an 7bservable ob1ect is updated it invo,es the update&)
method of each of its observers to notify the observers that it has changed state.
$or e0ample, third4party software cannot be made 7bservable without changing an
e0isting class hierarchy.
What is snchroni%ation and wh is it important?
Java supports multiple threads to be e0ecuted. This may cause two or more threads
to access the same fields or ob1ects. .ynchroni:ation is a process which ,eeps all
concurrent threads in e0ecution to be in synch. .ynchroni:ation avoids memory
consistence errors caused due to inconsistent view of shared memory. %hen a
method is declared as synchroni:ed/ the thread holds the monitor for that method2s
ob1ect. If another thread is e0ecuting the synchroni:ed method, your thread is
bloc,ed until that thread releases the monitor.
&oes !arba!e collection !uarantee that a pro!ram will not run out of
memor?
Garbage collection does not guarantee that a program will not run out of memory.
The garbage collection is dependant on the JA3. ?ence it is possible for the
resources to be used faster than they are garbage collected. Garbage collection
cannot be predicted if it will happen or not.
What is the difference between a brea* statement and a continue
statement?
brea, statement when applied to a loop ends the statement. continue statement
ends the iteration of the current loop and returns the control to the loop statement.
If the brea, ,eyword is followed by an identifier that is the label of a random
enclosing statement, e0ecution transfers out of that enclosing statement. If the
continue ,eyword is followed by an identifier that is the label of an enclosing loop,
e0ecution s,ips to the end of that loop instead.
What are snchroni%ed methods and snchroni%ed statements?
.ynchroni:ed methods
%hen a method in Java needs to be synchroni:ed, the ,eyword synchroni:ed should
be added.
+0ample#
!ublic synchroni:ed void increment&)
;
E++/
=
.ynchroni:ation does not allow invocation of this .ynchroni:ed method for the same
ob1ect until the first thread is done with the ob1ect. .ynchroni:ation allows having
control over the data in the class.
.ynchroni:ed .tatement#
synchroni:ed .tatement can only be e0ecuted once the thread has obtained a loc,
for the ob1ect or the class that has been referred to in the statement. .ynchroni:ed
statement contains a synchroni:ed bloc,, within which is placed ob1ects and methods
that are to be synchroni:ed.
+0ample#
public void run&)
;
synchroni:ed&p8)
;
<<synchroni:e statement. !8 here is an ob1ect of some class !
p8.display&s8)/
=
=
1or more questions with answers2 follow the lin* below3
http#<<www.careerride.com<Java4Interview4Fuestions.asp0
%e do not warrant the correctness of content. The ris, from using it lies entirely with the user. %hile using
this document, you agree to have read and accepted the terms of use and privacy policy.

También podría gustarte