Sei sulla pagina 1di 13

Master list of Java interview questions - 115 questions

115 questions total, not for the weak. Covers everything from basics to JDBC
connectivity, A! an" J#$.
1. What is the difference between procedural and object-oriented programs?%
a& 'n (roce"ural (rogram, (rogramming logic follows certain (roce"ures an" the
instructions are e)ecute" one after another. 'n **$ (rogram, unit of (rogram is
ob+ect, which is nothing but combination of "ata an" co"e. b& 'n (roce"ural
(rogram, "ata is e)(ose" to the whole (rogram whereas in **$s (rogram, it is
accessible with in the ob+ect an" which in turn assures the security of the co"e.
,. What are Encapsulation !nheritance and "ol#morphism?% -nca(sulation is
the mechanism that bin"s together co"e an" "ata it mani(ulates an" kee(s both
safe from outsi"e interference an" misuse. 'nheritance is the (rocess by which one
ob+ect acquires the (ro(erties of another ob+ect. $olymor(hism is the feature that
allows one interface to be use" for general class actions.
.. What is the difference between $ssignment and !nitiali%ation?% Assignment
can be "one as many times as "esire" whereas initiali/ation can be "one only
once.
0. What is &&"s?% *b+ect oriente" (rogramming organi/es a (rogram aroun" its
"ata, i. e. , ob+ects an" a set of well "efine" interfaces to that "ata. An ob+ect%
oriente" (rogram can be characteri/e" as "ata controlling access to co"e.
5. What are 'lass 'onstructor and "rimitive data t#pes?% Class is a tem(late for
multi(le ob+ects with similar features an" it is a blue (rint for ob+ects. 't "efines a
ty(e of ob+ect accor"ing to the "ata the ob+ect can hol" an" the o(erations the
ob+ect can (erform. Constructor is a s(ecial kin" of metho" that "etermines how
an ob+ect is initiali/e" when create". $rimitive "ata ty(es are 1 ty(es an" they are2
byte, short, int, long, float, "ouble, boolean, char.
3. What is an &bject and how do #ou allocate memor# to it?% *b+ect is an
instance of a class an" it is a software unit that combines a structure" set of "ata
with a set of o(erations for ins(ecting an" mani(ulating that "ata. hen an ob+ect
is create" using new o(erator, memory is allocate" to it.
4. What is the difference between constructor and method?% Constructor will be
automatically invoke" when an ob+ect is create" whereas metho" has to be calle"
e)(licitly.
1. What are methods and how are the# defined?% 5etho"s are functions that
o(erate on instances of classes in which they are "efine". *b+ects can
communicate with each other using metho"s an" can call metho"s in other
classes. 5etho" "efinition has four (arts. !hey are name of the metho", ty(e of
ob+ect or (rimitive ty(e the metho" returns, a list of (arameters an" the bo"y of
the metho". A metho"6s signature is a combination of the first three (arts
mentione" above.
7. What is the use of bin and lib in J()?% Bin contains all tools such as +avac,
a((letviewer, awt tool, etc., whereas lib contains A$' an" all (ackages.
18. What is casting?% Casting is use" to convert the value of one ty(e to another.
11. *ow man# wa#s can an argument be passed to a subroutine and e+plain
them?% An argument can be (asse" in two ways. !hey are (assing by value an"
(assing by reference. $assing by value2 !his metho" co(ies the value of an
argument into the formal (arameter of the subroutine. $assing by reference2 'n
this metho", a reference to an argument 9not the value of the argument& is (asse"
to the (arameter.
1,. What is the difference between an argument and a parameter?% hile
"efining metho", variables (asse" in the metho" are calle" (arameters. hile
using those metho"s, values (asse" to those variables are calle" arguments.
1.. What are different t#pes of access modifiers?% (ublic2 Any thing "eclare" as
(ublic can be accesse" from anywhere. (rivate2 Any thing "eclare" as (rivate
can6t be seen outsi"e of its class. (rotecte"2 Any thing "eclare" as (rotecte" can
be accesse" by classes in the same (ackage an" subclasses in the other (ackages.
"efault mo"ifier 2 Can be accesse" only to classes in the same (ackage.
10. What is final finali%e,- and finall#?% final 2 final keywor" can be use" for class,
metho" an" variables. A final class cannot be subclasse" an" it (revents other
(rogrammers from subclassing a secure class to invoke insecure metho"s. A final
metho" can6t be overri""en. A final variable can6t change from its initiali/e"
value. finali/e9& 2 finali/e9& metho" is use" +ust before an ob+ect is "estroye" an"
can be calle" +ust (rior to garbage collection. finally 2 finally, a key wor" use" in
e)ce(tion han"ling, creates a block of co"e that will be e)ecute" after a try:catch
block has com(lete" an" before the co"e following the try:catch block. !he
finally block will e)ecute whether or not an e)ce(tion is thrown. ;or e)am(le, if a
metho" o(ens a file u(on e)it, then you will not want the co"e that closes the file
to be by(asse" by the e)ce(tion%han"ling mechanism. !his finally keywor" is
"esigne" to a""ress this contingency.
15. What is ./!'&(E?% <nico"e is use" for internal re(resentation of characters
an" strings an" it uses 13 bits to re(resent each other.
13. What is 0arbage 'ollection and how to call it e+plicitl#?% hen an ob+ect is
no longer referre" to by any variable, +ava automatically reclaims memory use"
by that ob+ect. !his is known as garbage collection. #ystem. gc9& metho" may be
use" to call it e)(licitly.
14. What is finali%e,- method?% finali/e 9& metho" is use" +ust before an ob+ect is
"estroye" an" can be calle" +ust (rior to garbage collection.
11. What are 1ransient and 2olatile Modifiers?% !ransient2 !he transient mo"ifier
a((lies to variables only an" it is not store" as (art of its ob+ect6s $ersistent state.
!ransient variables are not seriali/e". =olatile2 =olatile mo"ifier a((lies to
variables only an" it tells the com(iler that the variable mo"ifie" by volatile can
be change" une)(ecte"ly by other (arts of the (rogram.
17. What is method overloading and method overriding?% 5etho" overloa"ing2
hen a metho" in a class having the same metho" name with "ifferent arguments
is sai" to be metho" overloa"ing. 5etho" overri"ing 2 hen a metho" in a class
having the same metho" name with same arguments is sai" to be metho"
overri"ing.
,8. What is difference between overloading and overriding?% a& 'n overloa"ing,
there is a relationshi( between metho"s available in the same class whereas in
overri"ing, there is relationshi( between a su(erclass metho" an" subclass
metho". b& *verloa"ing "oes not block inheritance from the su(erclass whereas
overri"ing blocks inheritance from the su(erclass. c& 'n overloa"ing, se(arate
metho"s share the same name whereas in overri"ing, subclass metho" re(laces the
su(erclass. "& *verloa"ing must have "ifferent metho" signatures whereas
overri"ing must have same signature.
,1. What is meant b# !nheritance and what are its advantages?% 'nheritance is the
(rocess of inheriting all the features from a class. !he a"vantages of inheritance
are reusability of co"e an" accessibility of variables an" metho"s of the su(er
class by subclasses.
,,. What is the difference between this,- and super,-?% this9& can be use" to invoke
a constructor of the same class whereas su(er9& can be use" to invoke a su(er
class constructor.
,.. What is the difference between superclass and subclass?% A su(er class is a
class that is inherite" whereas sub class is a class that "oes the inheriting.
,0. What modifiers ma# be used with top-level class?% (ublic, abstract an" final
can be use" for to(%level class.
,5. What are inner class and anon#mous class?% 'nner class 2 classes "efine" in
other classes, inclu"ing those "efine" in metho"s are calle" inner classes. An
inner class can have any accessibility inclu"ing (rivate. Anonymous class 2
Anonymous class is a class "efine" insi"e a metho" without a name an" is
instantiate" an" "eclare" in the same (lace an" cannot have e)(licit constructors.
,3. What is a pac3age?% A (ackage is a collection of classes an" interfaces that
(rovi"es a high%level layer of access (rotection an" name s(ace management.
,4. What is a reflection pac3age?% +ava. lang. reflect (ackage has the ability to
analy/e itself in runtime.
,1. What is interface and its use?% 'nterface is similar to a class which may contain
metho"6s signature only but not bo"ies an" it is a formal set of metho" an"
constant "eclarations that must be "efine" by the class that im(lements it.
'nterfaces are useful for2 a&Declaring metho"s that one or more classes are
e)(ecte" to im(lement b&Ca(turing similarities between unrelate" classes without
forcing a class relationshi(. c&Determining an ob+ect6s (rogramming interface
without revealing the actual bo"y of the class.
,7. What is an abstract class?% An abstract class is a class "esigne" with
im(lementation ga(s for subclasses to fill in an" is "eliberately incom(lete.
.8. What is the difference between !nteger and int?% a& 'nteger is a class "efine" in
the +ava. lang (ackage, whereas int is a (rimitive "ata ty(e "efine" in the Java
language itself. Java "oes not automatically convert from one to the other. b&
'nteger can be use" as an argument for a metho" that requires an ob+ect, whereas
int can be use" for calculations.
.1. What is a cloneable interface and how man# methods does it contain?% 't is
not having any metho" because it is a !A>>-D or 5A?@-? interface.
.,. What is the difference between abstract class and interface?% a& All the
metho"s "eclare" insi"e an interface are abstract whereas abstract class must have
at least one abstract metho" an" others may be concrete or abstract. b& 'n abstract
class, key wor" abstract must be use" for the metho"s whereas interface we nee"
not use that keywor" for the metho"s. c& Abstract class must have subclasses
whereas interface can6t have subclasses.
... 'an #ou have an inner class inside a method and what variables can #ou
access?% Aes, we can have an inner class insi"e a metho" an" final variables can
be accesse".
.0. What is the difference between 4tring and 4tring 5uffer?% a& #tring ob+ects are
constants an" immutable whereas #tringBuffer ob+ects are not. b& #tring class
su((orts constant strings whereas #tringBuffer class su((orts growable an"
mo"ifiable strings.
.5. What is the difference between $rra# and vector?% Array is a set of relate"
"ata ty(e an" static whereas vector is a growable array of ob+ects an" "ynamic.
.3. What is the difference between e+ception and error?% !he e)ce(tion class
"efines mil" error con"itions that your (rogram encounters. -)ce(tions can occur
when trying to o(en the file, which "oes not e)ist, the network connection is
"isru(te", o(eran"s being mani(ulate" are out of (rescribe" ranges, the class file
you are intereste" in loa"ing is missing. !he error class "efines serious error
con"itions that you shoul" not attem(t to recover from. 'n most cases it is
a"visable to let the (rogram terminate when such an error is encountere".
.4. What is the difference between process and thread?% $rocess is a (rogram in
e)ecution whereas threa" is a se(arate (ath of e)ecution in a (rogram.
.1. What is multithreading and what are the methods for inter-thread
communication and what is the class in which these methods are defined?%
5ultithrea"ing is the mechanism in which more than one threa" run in"e(en"ent
of each other within the (rocess. wait 9&, notify 9& an" notifyAll9& metho"s can be
use" for inter%threa" communication an" these metho"s are in *b+ect class. wait9&
2 hen a threa" e)ecutes a call to wait9& metho", it surren"ers the ob+ect lock an"
enters into a waiting state. notify9& or notifyAll9& 2 !o remove a threa" from the
waiting state, some other threa" must make a call to notify9& or notifyAll9& metho"
on the same ob+ect.
.7. What is the class and interface in java to create thread and which is the most
advantageous method?% !hrea" class an" ?unnable interface can be use" to
create threa"s an" using ?unnable interface is the most a"vantageous metho" to
create threa"s because we nee" not e)ten" threa" class here.
08. What are the states associated in the thread?% !hrea" contains rea"y, running,
waiting an" "ea" states.
01. What is s#nchroni%ation?% #ynchroni/ation is the mechanism that ensures that
only one threa" is accesse" the resources at a time.
0,. When #ou will s#nchroni%e a piece of #our code?% hen you e)(ect your co"e
will be accesse" by "ifferent threa"s an" these threa"s may change a (articular
"ata causing "ata corru(tion.
0.. What is deadloc3?% hen two threa"s are waiting each other an" can6t (rece"e
the (rogram is sai" to be "ea"lock.
00. What is daemon thread and which method is used to create the daemon
thread?% Daemon threa" is a low (riority threa" which runs intermittently in the
back groun" "oing the garbage collection o(eration for the +ava runtime system.
setDaemon metho" is use" to create a "aemon threa".
05. $re there an# global variables in Java which can be accessed b# other part
of #our program?% Bo, it is not the main metho" in which you "efine variables.
>lobal variables is not (ossible because conce(t of enca(sulation is eliminate"
here.
03. What is an applet?% A((let is a "ynamic an" interactive (rogram that runs insi"e
a web (age "is(laye" by a +ava ca(able browser.
04. What is the difference between applications and applets?% a&A((lication must
be run on local machine whereas a((let nee"s no e)(licit installation on local
machine. b&A((lication must be run e)(licitly within a +ava%com(atible virtual
machine whereas a((let loa"s an" runs itself automatically in a +ava%enable"
browser. "&A((lication starts e)ecution with its main metho" whereas a((let
starts e)ecution with its init metho". e&A((lication can run with or without
gra(hical user interface whereas a((let must run within a gra(hical user interface.
01. *ow does applet recogni%e the height and width?% <sing get$arameters9&
metho".
07. When do #ou use codebase in applet?% hen the a((let class file is not in the
same "irectory, co"ebase is use".
58. What is the lifec#cle of an applet?% init9& metho" % Can be calle" when an a((let
is first loa"e" start9& metho" % Can be calle" each time an a((let is starte". (aint9&
metho" % Can be calle" when the a((let is minimi/e" or ma)imi/e". sto(9&
metho" % Can be use" when the browser moves off the a((let6s (age. "estroy9&
metho" % Can be calle" when the browser is finishe" with the a((let.
51. *ow do #ou set securit# in applets?% using set#ecurity5anager9& metho"
5,. What is an event and what are the models available for event handling?% An
event is an event ob+ect that "escribes a state of change in a source. 'n other
wor"s, event occurs when an action is generate", like (ressing button, clicking
mouse, selecting a list, etc. !here are two ty(es of mo"els for han"ling events an"
they are2 a& event%inheritance mo"el an" b& event%"elegation mo"el
5.. What are the advantages of the model over the event-inheritance model?%
!he event%"elegation mo"el has two a"vantages over the event%inheritance mo"el.
!hey are2 a&'t enables event han"ling by ob+ects other than the ones that generate
the events. !his allows a clean se(aration between a com(onent6s "esign an" its
use. b&'t (erforms much better in a((lications where many events are generate".
!his (erformance im(rovement is "ue to the fact that the event%"elegation mo"el
"oes not have to be re(eate"ly (rocess unhan"le" events as is the case of the
event%inheritance.
50. What is source and listener?% source 2 A source is an ob+ect that generates an
event. !his occurs when the internal state of that ob+ect changes in some way.
listener 2 A listener is an ob+ect that is notifie" when an event occurs. 't has two
ma+or requirements. ;irst, it must have been registere" with one or more sources
to receive notifications about s(ecific ty(es of events. #econ", it must im(lement
metho"s to receive an" (rocess these notifications.
55. What is adapter class?% An a"a(ter class (rovi"es an em(ty im(lementation of
all metho"s in an event listener interface. A"a(ter classes are useful when you
want to receive an" (rocess only some of the events that are han"le" by a
(articular event listener interface. Aou can "efine a new class to act listener by
e)ten"ing one of the a"a(ter classes an" im(lementing only those events in which
you are intereste". ;or e)am(le, the 5ouse5otionA"a(ter class has two metho"s,
mouseDragge"9&an" mouse5ove"9&. !he signatures of these em(ty are e)actly as
"efine" in the 5ouse5otionCistener interface. 'f you are intereste" in only mouse
"rag events, then you coul" sim(ly e)ten" 5ouse5otionA"a(ter an" im(lement
mouseDragge"9& .
53. What is meant b# controls and what are different t#pes of controls in $W1?%
Controls are com(onents that allow a user to interact with your a((lication an"
the A! su((orts the following ty(es of controls2 Cabels, $ush Buttons, Check
Bo)es, Choice Cists, Cists, #crollbars, !e)t Com(onents. !hese controls are
subclasses of Com(onent.
54. What is the difference between choice and list?% A Choice is "is(laye" in a
com(act form that requires you to (ull it "own to see the list of available choices
an" only one item may be selecte" from a choice. A Cist may be "is(laye" in such
a way that several list items are visible an" it su((orts the selection of one or
more list items.
51. What is the difference between scrollbar and scrollpane?% A #crollbar is a
Com(onent, but not a Container whereas #croll(ane is a Conatiner an" han"les its
own events an" (erform its own scrolling.
57. What is a la#out manager and what are different t#pes of la#out managers
available in java $W1?% A layout manager is an ob+ect that is use" to organi/e
com(onents in a container. !he "ifferent layouts are available are ;lowCayout,
Bor"erCayout, Car"Cayout, >ri"Cayout an" >ri"BagCayout.
38. *ow are the elements of different la#outs organi%ed?% ;lowCayout2 !he
elements of a ;lowCayout are organi/e" in a to( to bottom, left to right fashion.
Bor"erCayout2 !he elements of a Bor"erCayout are organi/e" at the bor"ers
9Borth, #outh, -ast an" est& an" the center of a container. Car"Cayout2 !he
elements of a Car"Cayout are stacke", on to( of the other, like a "eck of car"s.
>ri"Cayout2 !he elements of a >ri"Cayout are of equal si/e an" are lai" out using
the square of a gri". >ri"BagCayout2 !he elements of a >ri"BagCayout are
organi/e" accor"ing to a gri". Dowever, the elements are of "ifferent si/e an"
may occu(y more than one row or column of the gri". 'n a""ition, the rows an"
columns may have "ifferent si/es.
31. Which containers use a 5order la#out as their default la#out?% in"ow,
;rame an" Dialog classes use a Bor"erCayout as their layout.
3,. Which containers use a 6low la#out as their default la#out?% $anel an" A((let
classes use the ;lowCayout as their "efault layout.
3.. What are wrapper classes?% ra((er classes are classes that allow (rimitive
ty(es to be accesse" as ob+ects.
30. What are 2ector *ashtable 7in3ed7ist and Enumeration?% =ector 2 !he
=ector class (rovi"es the ca(ability to im(lement a growable array of ob+ects.
Dashtable 2 !he Dashtable class im(lements a Dashtable "ata structure. A
Dashtable in"e)es an" stores ob+ects in a "ictionary using hash co"es as the
ob+ect6s keys. Dash co"es are integer values that i"entify ob+ects. Cinke"Cist2
?emoving or inserting elements in the mi""le of an array can be "one using
Cinke"Cist. A Cinke"Cist stores each ob+ect in a se(arate link whereas an array
stores ob+ect references in consecutive locations. -numeration2 An ob+ect that
im(lements the -numeration interface generates a series of elements, one at a
time. 't has two metho"s, namely has5ore-lements9& an" ne)t-lement9&.
Das5ore-lemnts9& tests if this enumeration has more elements an" ne)t-lement
metho" returns successive elements of the series.
35. What is the difference between set and list?% #et stores elements in an
unor"ere" way but "oes not contain "u(licate elements, whereas list stores
elements in an or"ere" way but may contain "u(licate elements.
33. What is a stream and what are the t#pes of 4treams and classes of the
4treams?% A #tream is an abstraction that either (ro"uces or consumes
information. !here are two ty(es of #treams an" they are2 Byte #treams2 $rovi"e
a convenient means for han"ling in(ut an" out(ut of bytes. Character #treams2
$rovi"e a convenient means for han"ling in(ut E out(ut of characters. Byte
#treams classes2 Are "efine" by using two abstract classes, namely 'n(ut#tream
an" *ut(ut#tream. Character #treams classes2 Are "efine" by using two abstract
classes, namely ?ea"er an" riter.
34. What is the difference between 8eader9Writer and !nput4tream9&utput
4tream?% !he ?ea"er:riter class is character%oriente" an" the
'n(ut#tream:*ut(ut#tream class is byte%oriente".
31. What is an !9& filter?% An ':* filter is an ob+ect that rea"s from one stream an"
writes to another, usually altering the "ata in some way as it is (asse" from one
stream to another.
37. What is seriali%ation and deseriali%ation?% #eriali/ation is the (rocess of
writing the state of an ob+ect to a byte stream. Deseriali/ation is the (rocess of
restoring these ob+ects.
48. What is J(5'?% JDBC is a set of Java A$' for e)ecuting #FC statements. !his
A$' consists of a set of classes an" interfaces to enable (rograms to write (ure
Java Database a((lications.
41. What are drivers available?% a& JDBC%*DBC Bri"ge "river b& Bative A$'
$artly%Java "river c& JDBC%Bet $ure Java "river "& Bative%$rotocol $ure Java
"river
4,. What is the difference between J(5' and &(5'?% a& *BDC is for 5icrosoft
an" JDBC is for Java a((lications. b& *DBC can6t be "irectly use" with Java
because it uses a C interface. c& *DBC makes use of (ointers which have been
remove" totally from Java. "& *DBC mi)es sim(le an" a"vance" features
together an" has com(le) o(tions for sim(le queries. But JDBC is "esigne" to
kee( things sim(le while allowing a"vance" ca(abilities when require". e& *DBC
requires manual installation of the *DBC "river manager an" "river on all client
machines. JDBC "rivers are written in Java an" JDBC co"e is automatically
installable, secure, an" (ortable on all (latforms. f& JDBC A$' is a natural Java
interface an" is built on *DBC. JDBC retains some of the basic features of
*DBC.
4.. What are the t#pes of J(5' (river Models and e+plain them?% !here are two
ty(es of JDBC Driver 5o"els an" they are2 a& !wo tier mo"el an" b& !hree tier
mo"el !wo tier mo"el2 'n this mo"el, Java a((lications interact "irectly with the
"atabase. A JDBC "river is require" to communicate with the (articular "atabase
management system that is being accesse". #FC statements are sent to the
"atabase an" the results are given to user. !his mo"el is referre" to as
client:server configuration where user is the client an" the machine that has the
"atabase is calle" as the server. !hree tier mo"el2 A mi""le tier is intro"uce" in
this mo"el. !he functions of this mo"el are2 a& Collection of #FC statements from
the client an" han"ing it over to the "atabase, b& ?eceiving results from "atabase
to the client an" c& 5aintaining control over accessing an" u("ating of the above.
40. What are the steps involved for ma3ing a connection with a database or how
do #ou connect to a database?a& Coa"ing the "river 2 !o loa" the "river, Class.
forBame9& metho" is use". Class. forBame9Gsun. +"bc. o"bc. J"bc*"bcDriverG&H
hen the "river is loa"e", it registers itself with the +ava. sql. Driver5anager
class as an available "atabase "river. b& 5aking a connection with "atabase2 !o
o(en a connection to a given "atabase, Driver5anager. getConnection9& metho" is
use". Connection con I Driver5anager. getConnection 9G+"bc2o"bc2some"bG,
JuserG, J(asswor"G&H c& -)ecuting #FC statements 2 !o e)ecute a #FC query,
+ava. sql. statements class is use". create#tatement9& metho" of Connection to
obtain a new #tatement ob+ect. #tatement stmt I con. create#tatement9&H A query
that returns "ata can be e)ecute" using the e)ecuteFuery9& metho" of #tatement.
!his metho" e)ecutes the statement an" returns a +ava. sql. ?esult#et that
enca(sulates the retrieve" "ata2 ?esult#et rs I stmt. e)ecuteFuery9G#-C-C! K
;?*5 some tableG&H "& $rocess the results 2 ?esult#et returns one row at a time.
Be)t9& metho" of ?esult#et ob+ect can be calle" to move to the ne)t row. !he
get#tring9& an" get*b+ect9& metho"s are use" for retrieving column values2
while9rs. ne)t9&& L #tring event I rs. get#tring9GeventG&H *b+ect count I 9'nteger&
rs. get*b+ect9GcountG&H
45. What t#pe of driver did #ou use in project?% JDBC%*DBC Bri"ge "river 9is a
"river that uses native9C language& libraries an" makes calls to an e)isting *DBC
"river to access a "atabase engine&.
43. What are the t#pes of statements in J(5'?% #tatement2 to be use"
create#tatement9& metho" for e)ecuting single #FC statement $re(are"#tatement
M !o be use" (re(are"#tatement9& metho" for e)ecuting same #FC statement
over an" over. Callable#tatement M !o be use" (re(areCall9& metho" for
multi(le #FC statements over an" over.
44. What is stored procedure?% #tore" (roce"ure is a grou( of #FC statements that
forms a logical unit an" (erforms a (articular task. #tore" $roce"ures are use" to
enca(sulate a set of o(erations or queries to e)ecute on "atabase. #tore"
(roce"ures can be com(ile" an" e)ecute" with "ifferent (arameters an" results
an" may have any combination of in(ut:out(ut (arameters.
41. *ow to create and call stored procedures?% !o create store" (roce"ures2 Create
(roce"ure (roce"urename 9s(ecify in, out an" in out (arameters& B->'B Any
multi(le #FC statementH -BDH !o call store" (roce"ures2 Callable#tatement csmt
I con. (re(areCall9GLcall (roce"ure name9N,N&OG&H csmt.
register*ut$arameter9column no. , "ata ty(e&H csmt. set'nt9column no. , column
name& csmt. e)ecute9&H
47. What is servlet?% #ervlets are mo"ules that e)ten" request:res(onse%oriente"
servers, such as +ava%enable" web servers. ;or e)am(le, a servlet might be
res(onsible for taking "ata in an D!5C or"er%entry form an" a((lying the
business logic use" to u("ate a com(any6s or"er "atabase.
18. What are the classes and interfaces for servlets?% !here are two (ackages in
servlets an" they are +ava). servlet an"
11. What is the difference between an applet and a servlet?% a& #ervlets are to
servers what a((lets are to browsers. b& A((lets must have gra(hical user
interfaces whereas servlets have no gra(hical user interfaces.
1,. What is the difference between do"ost and do0et methods?% a& "o>et9&
metho" is use" to get information, while "o$ost9& metho" is use" for (osting
information. b& "o>et9& requests can6t sen" large amount of information an" is
limite" to ,08%,55 characters. Dowever, "o$ost9&requests (asses all of its "ata, of
unlimite" length. c& A "o>et9& request is a((en"e" to the request <?C in a query
string an" this allows the e)change is visible to the client, whereas a "o$ost9&
request (asses "irectly over the socket connection as (art of its D!!$ request
bo"y an" the e)change are invisible to the client.
1.. What is the life c#cle of a servlet?% -ach #ervlet has the same life cycle2 a& A
server loa"s an" initiali/es the servlet by init 9& metho". b& !he servlet han"les
/ero or more client6s requests through service9& metho". c& !he server removes
the servlet through "estroy9& metho".
10. Who is loading the init,- method of servlet?% eb server
15. What are the different servers available for developing and deplo#ing
4ervlets?% a& Java eb #erver b& J?un g& A(ache #erver h& Betsca(e 'nformation
#erver i& eb Cogic
13. *ow man# wa#s can we trac3 client and what are the#?% !he servlet A$'
(rovi"es two ways to track client state an" they are2 a& <sing #ession tracking an"
b& <sing Cookies.
14. What is session trac3ing and how do #ou trac3 a user session in servlets?%
#ession tracking is a mechanism that servlets use to maintain state about a series
requests from the same user across some (erio" of time. !he metho"s use" for
session tracking are2 a& <ser Authentication % occurs when a web server restricts
access to some of its resources to only those clients that log in using a recogni/e"
username an" (asswor". b& Di""en form fiel"s % fiel"s are a""e" to an D!5C
form that are not "is(laye" in the client6s browser. hen the form containing the
fiel"s is submitte", the fiel"s are sent back to the server. c& <?C rewriting % every
<?C that the user clicks on is "ynamically mo"ifie" or rewritten to inclu"e e)tra
information. !he e)tra information can be in the form of e)tra (ath information,
a""e" (arameters or some custom, server%s(ecific <?C change. "& Cookies % a bit
of information that is sent by a web server to a browser an" which can later be
rea" back from that browser. e& Dtt(#ession% (laces a limit on the number of
sessions that can e)ist in memory. !his limit is set in the session. ma)resi"ents
(ro(erty.
11. What is 4erver-4ide !ncludes ,44!-?% #erver%#i"e 'nclu"es allows embe""ing
servlets within D!5C (ages using a s(ecial servlet tag. 'n many servlets that
su((ort servlets, a (age can be (rocesse" by the server to inclu"e out(ut from
servlets at certain (oints insi"e the D!5C (age. !his is accom(lishe" using a
s(ecial internal ##'BCC<D-, which (rocesses the servlet tags. ##'BCC<D-
servlet will be invoke" whenever a file with an. shtml e)tension is requeste". #o
D!5C files that inclu"e server%si"e inclu"es must be store" with an . shtml
e)tension.
17. What are coo3ies and how will #ou use them?% Cookies are a mechanism that a
servlet uses to have a client hol" a small amount of state%information associate"
with the user. a& Create a cookie with the Cookie constructor2 (ublic
Cookie9#tring name, #tring value& b& A servlet can sen" a cookie to the client by
(assing a Cookie ob+ect to the a""Cookie9& metho" of Dtt(#ervlet?es(onse2
(ublic voi" Dtt(#ervlet?es(onse. a""Cookie9Cookie cookie& c& A servlet
retrieves cookies by calling the getCookies9& metho" of Dtt(#ervlet?equest2
(ublic CookieP Q Dtt(#ervlet?equest. getCookie9&.
78. !s it possible to communicate from an applet to servlet and how man# wa#s
and how?% Aes, there are three ways to communicate from an a((let to servlet
an" they are2 a& D!!$ Communication9!e)t%base" an" ob+ect%base"& b& #ocket
Communication c& ?5' Communication
71. What is connection pooling?% ith servlets, o(ening a "atabase connection is a
ma+or bottleneck because we are creating an" tearing "own a new connection for
every (age request an" the time taken to create connection will be more. Creating
a connection (ool is an i"eal a((roach for a com(licate" servlet. ith a
connection (ool, we can "u(licate only the resources we nee" to "u(licate rather
than the entire servlet. A connection (ool can also intelligently manage the si/e of
the (ool an" make sure each connection remains vali". A number of connection
(ool (ackages are currently available. #ome like DbConnectionBroker are freely
available from Java -)change orks by creating an ob+ect that "is(enses
connections an" connection '"s on request. !he Connection$ool class maintains a
Dastable, using Connection ob+ects as keys an" Boolean values as store" values.
!he Boolean value in"icates whether a connection is in use or not. A (rogram
calls getConnection9& metho" of the Connection$ool for getting Connection
ob+ect it can useH it calls returnConnection9& to give the connection back to the
(ool.
7,. Wh# should we go for interservlet communication?% #ervlets running together
in the same server communicate with each other in several ways. !he three ma+or
reasons to use interservlet communication are2 a& Direct servlet mani(ulation %
allows to gain access to the other currently loa"e" servlets an" (erform certain
tasks 9through the #ervletConte)t ob+ect& b& #ervlet reuse % allows the servlet to
reuse the (ublic metho"s of another servlet. c& #ervlet collaboration % requires to
communicate with each other by sharing s(ecific information 9through metho"
invocation&
7.. !s it possible to call servlet with parameters in the .87?% Aes. Aou can call a
servlet with (arameters in the synta) as 9N$aram1 I ))) RR m, I yyy&.
70. What is 4ervlet chaining?% #ervlet chaining is a technique in which two or more
servlets can coo(erate in servicing a single request. 'n servlet chaining, one
servlet6s out(ut is (i(e" to the ne)t servlet6s in(ut. !his (rocess continues until
the last servlet is reache". 'ts out(ut is then sent back to the client.
75. *ow do servlets handle multiple simultaneous requests?% !he server has
multi(le threa"s that are available to han"le requests. hen a request comes in, it
is assigne" to a threa", which calls a service metho" 9for e)am(le2 "o>et9&,
"o$ost9& an" service9&& of the servlet. ;or this reason, a single servlet ob+ect can
have its service metho"s calle" by many threa"s at once.
73. What is the difference between 1'"9!" and .("?% !C$:'$ is a two%way
communication between the client an" the server an" it is a reliable an" there is a
confirmation regar"ing reaching the message to the "estination. 't is like a (hone
call. <D$ is a one%way communication only between the client an" the server an"
it is not a reliable an" there is no confirmation regar"ing reaching the message to
the "estination. 't is like a (ostal mail.
74. What is !net address?% -very com(uter connecte" to a network has an '$
a""ress. An '$ a""ress is a number that uniquely i"entifies each com(uter on the
Bet. An '$ a""ress is a .,%bit number.
71. What is (omain /aming 4ervice,(/4-?% 't is very "ifficult to remember a set
of numbers9'$ a""ress& to connect to the 'nternet. !he Domain Baming
#ervice9DB#& is use" to overcome this (roblem. 't ma(s one (articular '$ a""ress
to a string of characters. ;or e)am(le, www. mascom. com im(lies com is the
"omain name reserve" for <# commercial sites, moscom is the name of the
com(any an" www is the name of the s(ecific com(uter, which is mascom6s
server.
77. What is .87?% <?C stan"s for <niform ?esource Cocator an" it (oints to
resource files on the 'nternet. <?C has four com(onents2 htt(2::www. a""ress.
com218:in"e).html, where htt( % (rotocol name, a""ress % '$ a""ress or host
name, 18 % (ort number an" in"e).html % file (ath.
188. What is 8M! and steps involved in developing an 8M! object?%
?emote 5etho" 'nvocation 9?5'& allows +ava ob+ect that e)ecutes on one
machine an" to invoke the metho" of a Java ob+ect to e)ecute on another
machine. !he ste(s involve" in "evelo(ing an ?5' ob+ect are2 a& Define the
interfaces b& 'm(lementing these interfaces c& Com(ile the interfaces an" their
im(lementations with the +ava com(iler "& Com(ile the server im(lementation
with ?5' com(iler e& ?un the ?5' registry f& ?un the a((lication
181. What is 8M! architecture?% ?5' architecture consists of four layers an"
each layer (erforms s(ecific functions2 a& A((lication layer % contains the actual
ob+ect "efinition. b& $ro)y layer % consists of stub an" skeleton. c& ?emote
?eference layer % gets the stream of bytes from the trans(ort layer an" sen"s it to
the (ro)y layer. "& !rans(ortation layer % res(onsible for han"ling the actual
machine%to%machine communication.
18,. what is .nicast8emote&bject?% All remote ob+ects must e)ten"
<nicast?emote*b+ect, which (rovi"es functionality that is nee"e" to make
ob+ects available from remote machines.
18.. E+plain the methods rebind,- and loo3up,- in /aming class?% rebin"9&
of the Baming class9foun" in +ava. rmi& is use" to u("ate the ?5' registry on the
server machine. Baming. rebin"9GA""#everG, A""#erver'm(l&H looku(9& of the
Baming class acce(ts one argument, the rmi <?C an" returns a reference to an
ob+ect of ty(e A""#erver'm(l.
180. What is a Java 5ean?% A Java Bean is a software com(onent that has
been "esigne" to be reusable in a variety of "ifferent environments.
185. What is a Jar file?% Jar file allows to efficiently "e(loying a set of classes
an" their associate" resources. !he elements in a +ar file are com(resse", which
makes "ownloa"ing a Jar file much faster than se(arately "ownloa"ing several
uncom(resse" files. !he (ackage +ava. util. /i( contains classes that rea" an"
write +ar files.
183. What is 5()?% BD@, Bean Develo(ment @it is a tool that enables to
create, configure an" connect a set of set of Beans an" it can be use" to test Beans
without writing a co"e.
184. What is J4"?% J#$ is a "ynamic scri(ting ca(ability for web (ages that
allows Java as well as a few s(ecial tags to be embe""e" into a web file
9D!5C:S5C, etc&. !he suffi) tra"itionally en"s with .+s( to in"icate to the web
server that the file is a J#$ files. J#$ is a server si"e technology % you can6t "o any
client si"e vali"ation with it. !he a"vantages are2 a& !he J#$ assists in making the
D!5C more functional. #ervlets on the other han" allow out(utting of D!5C but
it is a te"ious (rocess. b& 't is easy to make a change an" then let the J#$
ca(ability of the web server you are using "eal with com(iling it into a servlet an"
running it.
181. What are J4" scripting elements?% J#$ scri(ting elements lets to insert
Java co"e into the servlet that will be generate" from the current J#$ (age. !here
are three forms2 a& -)(ressions of the form TUI e)(ression UV that are evaluate"
an" inserte" into the out(ut, b& #cri(tlets of the formthat are inserte" into the
servlet6s service metho", an" c& Declarations of the form TUW Co"e UVthat are
inserte" into the bo"y of the servlet class, outsi"e of any e)isting metho"s.
187. What are J4" (irectives?% A J#$ "irective affects the overall structure
of the servlet class. 't usually has the following form2TUX "irective
attributeIGvalueG UV Dowever, you can also combine multi(le attribute settings
for a single "irective, as follows2TUX "irective attribute1IGvalue1Y attribute
,IGvalue,Y . . . attributeB IGvalueBG UV !here are two main ty(es of "irective2
(age, which lets to "o things like im(ort classes, customi/e the servlet su(erclass,
an" the likeH an" inclu"e, which lets to insert a file into the servlet class at the
time the J#$ file is translate" into a servlet
118. What are "redefined variables or implicit objects?% !o sim(lify co"e in
J#$ e)(ressions an" scri(tlets, we can use eight automatically "efine" variables,
sometimes calle" im(licit ob+ects. !hey are request, res(onse, out, session,
a((lication, config, (ageConte)t, an" (age.
111. What are J4" $'1!&/4?% J#$ actions use constructs in S5C synta) to
control the behavior of the servlet engine. Aou can "ynamically insert a file, reuse
JavaBeans com(onents, forwar" the user to another (age, or generate D!5C for
the Java (lugin. Available actions inclu"e2 +s(2inclu"e % 'nclu"e a file at the time
the (age is requeste". +s(2useBean % ;in" or instantiate a JavaBean.
+s(2set$ro(erty % #et the (ro(erty of a JavaBean. +s(2get$ro(erty % 'nsert the
(ro(erty of a JavaBean into the out(ut. +s(2forwar" % ;orwar" the requester to a
new(age. Js(2 (lugin % >enerate browser%s(ecific co"e that makes an *BJ-C! or
-5B-D
11,. *ow do #ou pass data ,including Java5eans- to a J4" from a servlet?%
91& ?equest Cifetime2 <sing this technique to (ass beans, a request "is(atcher
9using either Jinclu"eG or forwar"G& can be calle". !his bean will "isa((ear after
(rocessing this request has been com(lete". #ervlet2 request.
setAttribute9GtheBeanG, myBean&H ?equestDis(atcher r" I get#ervletConte)t9&.
get?equestDis(atcher9Gthe(age. +s(G&H r". forwar"9request, res(onse&H J#$
$A>-2T+s(2 useBean i"IGtheBeanG sco(eIGrequestG classIG. . . . . G :V9,& #ession
Cifetime2 <sing this technique to (ass beans that are relevant to a (articular
session 9such as in in"ivi"ual user login& over a number of requests. !his bean
will "isa((ear when the session is invali"ate" or it times out, or when you remove
it. #ervlet2 Dtt(#ession session I request. get#ession9true&H session.
(ut=alue9GtheBeanG, myBean&H :K Aou can "o a request "is(atcher here, or +ust let
the bean be visible on the ne)t request K: J#$ $age2T+s(2useBean i"IGtheBeanG
sco(eIGsessionG classIG. . . G :V .& A((lication Cifetime2 <sing this technique to
(ass beans that are relevant to all servlets an" J#$ (ages in a (articular a((, for all
users. ;or e)am(le, ' use this to make a JDBC connection (ool ob+ect available to
the various servlets an" J#$ (ages in my a((s. !his bean will "isa((ear when the
servlet engine is shut "own, or when you remove it. #ervlet2 >et#ervletConte)t9&.
setAttribute9GtheBeanG, myBean&H J#$ $A>-2T+s(2useBean i"IGtheBeanG
sco(eIGa((licationG classIG. . . G :V
11.. *ow can ! set a coo3ie in J4"?% res(onse. setDea"er9G#et%CookieG,
Jcookie stringG&H !o give the res(onse%ob+ect to a bean, write a metho"
set?es(onse 9Dtt(#ervlet?es(onse res(onse& % to the bean, an" in +s(%file2TU
bean. set?es(onse 9res(onse&H UV
110. *ow can ! delete a coo3ie with J4"?% #ay that ' have a cookie calle"
Jfoo, G that ' set a while ago E ' want it to go away. ' sim(ly2 TU Cookie
killCookie I new Cookie9GfooG, null&H @illCookie. set$ath9G:G&H killCookie.
set5a)Age98&H res(onse. a""Cookie9killCookie&H UV
115. *ow are 4ervlets and J4" "ages related?% J#$ (ages are focuse" aroun"
D!5C 9or S5C& with Java co"es an" J#$ tags insi"e them. hen a web server
that has J#$ su((ort is aske" for a J#$ (age, it checks to see if it has alrea"y
com(ile" the (age into a servlet. !hus, J#$ (ages become servlets an" are
transforme" into (ure Java an" then com(ile", loa"e" into the server an"
e)ecute".

Potrebbero piacerti anche