Anda di halaman 1dari 22

FREQUENTLY ASKED QUESTIONS (JAVA) IN INTERVIEWS

1) What is OOPs?
Ans: Object oriented programming organizes a program around its data,i.e.,objects
and a set of well defined interfaces to that data.An object-oriented program can
be characterized as data controlling access to code.
2) what is th !i""#$% &tw$ P#'%!(#a) a$! OOPs?
Ans: a) In procedural program, programming logic follows certain procedures and
the instructions are executed one after another. In OOs program, unit of program
is object, which is nothing but combination of data and code.
b) In procedural program,data is exposed to the whole program whereas in OOs
program,it is accessible with in the object and which in turn assures the securit! of
the code.
*) What a# E$%a+s()ati'$, I$h#ita$% a$! P')-.'#+his.?
Ans: "ncapsulation is the mechanism that binds together code and data it
manipulates and #eeps both safe from outside interference and misuse.
Inheritance is the process b! which one object ac$uires the properties of another
object.
ol!morphism is the feature that allows one interface to be used for general class
actions.
/) What is th !i""#$% &tw$ Assi0$.$t a$! I$itia)i1ati'$?
Ans: Assignment can be done as man! times as desired whereas initialization can be
done onl! once.
2) What a# 3)ass, 3'$st#(%t'# a$! P#i.iti4 !ata t-+s?
Ans: %lass is a template for multiple objects with similar features and it is a blue
print for objects. It defines a t!pe of object according to the data the object can
hold and the operations the object can perform.
%onstructor is a special #ind of method that determines how an object is initialized
when created.
rimiti&e data t!pes are ' t!pes and the! are: b!te, short, int, long, float, double,
(oolean, char
5) What is a$ O&6%t a$! h'w !' -'( a))'%at ..'#- t' it?
Ans: Object is an instance of a class and it is a software unit that combines a
structured set of data with a set of operations for inspecting and manipulating that
data. )hen an object is created using new operator, memor! is allocated to it.
7) What is th !i""#$% &tw$ %'$st#(%t'# a$! .th'!?
Ans: %onstructor will be automaticall! in&o#ed when an object is created whereas
method has to be called explicitl!.
8) What a# .th'!s a$! h'w a# th- !"i$!?
Ans: *ethods are functions that operate on instances of classes in which the! are
defined. Objects can communicate with each other using methods and can call
methods in other classes. *ethod definition has four parts. +he! are name of the
method, t!pe of object or primiti&e t!pe the method returns, a list of parameters
and the bod! of the method. A method-s signature is a combination of the first
three parts mentioned abo&e.
9) What is th (s '" &i$ a$! )i& i$ JDK?
Ans: (in contains all tools such as ja&ac, applet&iewer, awt tool, etc., whereas lib
contains AI and all pac#ages.
1:) What is %asti$0?
Ans: %asting is used to con&ert the &alue of one t!pe to another.
11) ;'w .a$- wa-s %a$ a$ a#0(.$t & +ass! t' a s(&#'(ti$ a$! <+)ai$
th.?
Ans: An argument can be passed in two wa!s. +he! are passing b! &alue and passing
b! reference.
Passi$0 &- 4a)(= +his method copies the &alue of an argument into the formal
parameter of the subroutine.
Passi$0 &- #"#$%= In this method, a reference to an argument .not the &alue
of the argument) is passed to the parameter.
12) What is th !i""#$% &tw$ a$ a#0(.$t a$! a +a#a.t#?
Ans: )hile defining method, &ariables passed in the method are called parameters.
)hile using those methods, &alues passed to those &ariables are called arguments.
1*) What a# !i""#$t t-+s '" a%%ss .'!i"i#s?
Ans: +(&)i%= An! thing declared as public can be accessed from an!where.
+#i4at= An! thing declared as pri&ate can-t be seen outside of its class.
+#'t%t!= An! thing declared as protected can be accessed b! classes in
the same pac#age and subclasses in the other pac#ages.
!"a()t .'!i"i# : %an be accessed onl! to classes in the same pac#age.
1/) What is "i$a), "i$a)i1() a$! "i$a))-?
Ans: final : final #e!word can be used for class, method and &ariables.
A final class cannot be subclassed and it pre&ents other programmers
from subclassing a secure class to in&o#e insecure methods.
A final method can- t be o&erridden
A final &ariable can-t change from its initialized &alue.
finalize. ) : finalize. ) method is used just before an object is destro!ed and can be
called just prior to garbage collection.
finall! : finall!, a #e! word used in exception handling, creates a bloc# of code that
will be executed after a tr!/catch bloc# has completed and before the code
following the tr!/catch bloc#. +he finall! bloc# will execute whether or not an
exception is thrown.
0or example, if a method opens a file upon exit, then !ou will not want the code that
closes the file to be b!passed b! the exception-handling mechanism. +his finall!
#e!word is designed to address this contingenc!.
12) What is UNI3ODE?
Ans: 1nicode is used for internal representation of characters and strings and it
uses 23 bits to represent each other.
15) What is >a#&a0 3'))%ti'$ a$! h'w t' %a)) it <+)i%it)-?
Ans: )hen an object is no longer referred to b! an! &ariable, ja&a automaticall!
reclaims memor! used b! that object. +his is #nown as garbage collection.
4!stem.gc.) method ma! be used to call it explicitl!.
17) What is "i$a)i1() .th'! ?
Ans: finalize .) method is used just before an object is destro!ed and can be called
just prior to garbage collection.
18) What a# T#a$si$t a$! V')ati) ?'!i"i#s?
Ans: T#a$si$t= +he transient modifier applies to &ariables onl! and it is not stored
as part of its object-s ersistent state. +ransient &ariables are not serialized.
V')ati)= 5olatile modifier applies to &ariables onl! and it tells the compiler that
the &ariable modified b! &olatile can be changed unexpectedl! b! other parts of the
program.
19) What is .th'! '4#)'a!i$0 a$! .th'! '4##i!i$0?
Ans: *ethod o&erloading: )hen a method in a class ha&ing the same method name
with different arguments is said to be method o&erloading.
?th'! '4##i!i$0 = )hen a method in a class ha&ing the same method name with
same arguments is said to be method o&erriding.
2:) What is !i""#$% &tw$ '4#)'a!i$0 a$! '4##i!i$0?
Ans: a) In o&erloading, there is a relationship between methods a&ailable in the
same class whereas in o&erriding, there is relationship between a superclass method
and subclass method.
b) O&erloading does not bloc# inheritance from the superclass whereas o&erriding
bloc#s inheritance from the superclass.
c) In o&erloading, separate methods share the same name whereas in
o&erriding,subclass method replaces the superclass.
d) O&erloading must ha&e different method signatures whereas o&erriding must
ha&e same signature.
21) What is .a$t &- I$h#ita$% a$! what a# its a!4a$ta0s?
Ans: Inheritance is the process of inheriting all the features from a class. +he
ad&antages of inheritance are reusabilit! of code and accessibilit! of &ariables and
methods of the super class b! subclasses.
22) What is th !i""#$% &tw$ this() a$! s(+#()?
Ans: this.) can be used to in&o#e a constructor of the same class whereas super.)
can be used to in&o#e a super class constructor.
2*) What is th !i""#$% &tw$ s(+#%)ass a$! s(&%)ass?
Ans: A super class is a class that is inherited whereas sub class is a class
that does the inheriting.
2/) What .'!i"i#s .a- & (s! with t'+@)4) %)ass?
Ans: public, abstract and final can be used for top-le&el class.
22) What a# i$$# %)ass a$! a$'$-.'(s %)ass?
Ans: I$$# %)ass = classes defined in other classes, including those defined in
methods are called inner classes.
An inner class can ha&e an! accessibilit! including pri&ate.
A$'$-.'(s %)ass = Anon!mous class is a class defined inside a method without a
name and is instantiated and declared in the same place and cannot ha&e explicit
constructors.
25) What is a +a%Aa0?
Ans: A pac#age is a collection of classes and interfaces that pro&ides a high-le&el
la!er of access protection and name space management.
27) What is a #")%ti'$ +a%Aa0?
Ans: ja&a.lang.reflect pac#age has the abilit! to anal!ze itself in runtime.
28) What is i$t#"a% a$! its (s?
Ans: Interface is similar to a class which ma! contain method-s signature
onl! but not bodies and it is a formal set of method and constant declarations that
must be defined b! the class that implements it.
Interfaces are useful for:
a) 6eclaring methods that one or more classes are expected to implement
b) %apturing similarities between unrelated classes without forcing a class
relationship.
c) 6etermining an object-s programming interface without re&ealing the actual bod!
of the class.
29) What is a$ a&st#a%t %)ass?
Ans: An abstract class is a class designed with implementation gaps for subclasses
to fill in and is deliberatel! incomplete.
*:) What is th !i""#$% &tw$ I$t0# a$! i$t?
Ans: a) Integer is a class defined in the ja&a.lang pac#age, whereas int is a primiti&e
data t!pe defined in the 7a&a language itself. 7a&a does not automaticall! con&ert
from one to the other.
b) Integer can be used as an argument for a method that re$uires an object,
whereas int can be used for calculations.
*1) What is a %)'$a&) i$t#"a% a$! h'w .a$- .th'!s !'s it %'$tai$?
Ans- It is not ha&ing an! method because it is a +A88"6 or *A9:"9 interface.
*2) What is th !i""#$% &tw$ a&st#a%t %)ass a$! i$t#"a%?
Ans: a) All the methods declared inside an interface are abstract whereas abstract
class must ha&e at least one abstract method and others ma! be concrete or
abstract.
b) In abstract class, #e! word abstract must be used for the methods
whereas interface we need not use that #e!word for the methods.
c) Abstract class must ha&e subclasses whereas interface can-t ha&e subclasses.
**) 3a$ -'( ha4 a$ i$$# %)ass i$si! a .th'! a$! what 4a#ia&)s %a$ -'(
a%%ss?
Ans: ;es, we can ha&e an inner class inside a method and final &ariables can be
accessed.
*/) What is th !i""#$% &tw$ St#i$0 a$! St#i$0 B(""#?
Ans: a) 4tring objects are constants and immutable whereas
4tring(uffer objects are not.
b) 4tring class supports constant strings whereas
4tring(uffer class supports growable and modifiable strings.
*2) What is th !i""#$% &tw$ A##a- a$! 4%t'#?
Ans: Arra! is a set of related data t!pe and static whereas &ectoris a growable
arra! of objects and d!namic.
*5) What is th !i""#$% &tw$ <%+ti'$ a$! ##'#?
Ans: +he exception class defines mild error conditions that !our program
encounters.
"x: Arithmetic "xception, 0ilenot0ound exception
"xceptions can occur when
-- tr! to open the file, which does not exist
-- the networ# connection is disrupted
-- operands being manipulated are out of prescribed ranges
-- the class file !ou are interested in loading is missing
+he error class defines serious error conditions that !ou should not attempt to
reco&er from. In most cases it is ad&isable to let the program terminate when such
an error is encountered.
"x: 9unning out of memor! error, 4tac# o&erflow error.
*7) What is th !i""#$% &tw$ +#'%ss a$! th#a!?
Ans: rocess is a program in execution whereas thread is a separate path of
execution in a program.
*8) What is .()tith#a!i$0 a$! what a# th .th'!s "'# i$t#@th#a!
%'..($i%ati'$ a$! what is th %)ass i$ whi%h ths .th'!s a# !"i$!?
Ans: *ultithreading is the mechanism in which more than one thread run
independent of each other within the process.
wait .), notif! .) and notif!All.) methods can be used for inter-thread
communication and these methods are in Object class.
wait. ) : )hen a thread executes a call to wait. ) method, it surrenders the object
loc# and enters into a waiting state.
notif!. ) or notif!All. ) : +o remo&e a thread from the waiting state, some other
thread must ma#e a call to notif!. ) or notif!All. ) method on the same object.
*9) What is th %)ass a$! i$t#"a% i$ 6a4a t' %#at th#a! a$! whi%h is th
.'st a!4a$ta0'(s .th'!?
Ans: +hread class and 9unnable interface can be used to create threads and using
9unnable interface is the most ad&antageous method to create threads because we
need not extend thread class here.
/:) What a# th stats ass'%iat! i$ th th#a!?
Ans: +hread contains read!, running, waiting and dead states.
/1) What is s-$%h#'$i1ati'$?
Ans: 4!nchronization is the mechanism that ensures that onl! one thread is
accessed the resources at a time.
/2) Wh$ -'( wi)) s-$%h#'$i1 a +i% '" -'(# %'!?
Ans: )hen !ou expect !our code will be accessed b! different threads and these
threads ma! change a particular data causing data corruption.
/*) What is !a!)'%A?
Ans: )hen two threads are waiting each other and can-t precede the program is
said to be deadloc#.
//) What is !a.'$ th#a! a$! whi%h .th'! is (s! t' %#at th !a.'$
th#a!?
Ans: 6aemon thread is a low priorit! thread which runs intermittentl! in the bac#
ground doing the garbage collection operation for the ja&a runtime s!stem.
set6aemon method is used to create a daemon thread.
/2) A# th# a$- 0)'&a) 4a#ia&)s i$ Ja4a, whi%h %a$ & a%%ss! &- 'th#
+a#t '" -'(# +#'0#a.?
Ans: <o, it is not the main method in which !ou define &ariables. 8lobal &ariables is
not possible because concept of encapsulation is eliminated here.
/5) What is a$ a++)t?
Ans: Applet is a d!namic and interacti&e program that runs inside a web page
displa!ed b! a ja&a capable browser.
/7) What is th !i""#$% &tw$ a++)i%ati'$s a$! a++)ts?
Ans: a)Application must be run on local machine whereas applet needs no explicit
installation on local machine.
b)Application must be run explicitl! within a ja&a-compatible &irtual machine
whereas applet loads and runs itself automaticall! in a ja&a-enabled browser.
d)Application starts execution with its main method whereas applet starts
execution with its init method.
e)Application can run with or without graphical user interface whereas applet must
run within a graphical user interface.
/8) ;'w !'s a++)t #%'0$i1 th hi0ht a$! wi!th?
Ans:1sing getarameters.) method.
/9) Wh$ !' -'( (s %'!&as i$ a++)t?
Ans:)hen the applet class file is not in the same director!, codebase is used.
2:) What is th )i"%-%) '" a$ a++)t?
Ans:init. ) method - %an be called when an applet is first loaded
start. ) method - %an be called each time an applet is started
paint. ) method - %an be called when the applet is minimized or
maximized
stop. ) method - %an be used when the browser mo&es off the applet-s
page
destro!. ) method - %an be called when the browser is finished with the applet
21) ;'w !' -'( st s%(#it- i$ a++)ts?
Ans: using set4ecurit!*anager.) method
22) What is a$ 4$t a$! what a# th .'!)s a4ai)a&) "'# 4$t ha$!)i$0?
Ans: An e&ent is an e&ent object that describes a state of change in a source. In
other words, e&ent occurs when an
action is generated, li#e pressing button, clic#ing mouse, selecting a list, etc.
+here are two t!pes of models for handling e&ents and the! are:
a) e&ent-inheritance model and b) e&ent-delegation model
2*) What a# th a!4a$ta0s '" th .'!) '4# th 4$t@i$h#ita$% .'!)?
Ans: +he e&ent-delegation model has two ad&antages o&er the e&ent-inheritance
model. +he! are:
a)It enables e&ent handling b! objects other than the ones that generate the
e&ents. +his allows a clean separation between a component-s design and its use.
b)It performs much better in applications where man! e&ents are generated. +his
performance impro&ement is due to the fact that the e&ent-delegation model does
not ha&e to be repeatedl! process unhandled e&ents as is the case of the e&ent-
inheritance.
2/) What is s'(#% a$! )ist$# ?
Ans: s'(#% = A source is an object that generates an e&ent. +his occurs when the
internal state of that object changes in some wa!.
)ist$# = A listener is an object that is notified when an e&ent occurs. It has two
major re$uirements. 0irst, it must ha&e been registered with one or more sources
to recei&e notifications about specific t!pes of e&ents.
4econd, it must implement methods to recei&e and process these notifications.
22) What is a!a+t# %)ass?
Ans: An adapter class pro&ides an empt! implementation of all methods in an e&ent
listener interface. Adapter classes are useful when !ou want to recei&e and process
onl! some of the e&ents that are handled b! a particular e&ent listener interface.
;ou can define a new class to act listener b! extending one of the adapter classes
and implementing onl! those e&ents in which !ou are interested.
0or example, the *ouse*otionAdapter class has two methods, mouse6ragged. )and
mouse*o&ed.). +he signatures of these empt! are exactl! as defined in the
*ouse*otion=istener interface. If !ou are interested in onl! mouse drag e&ents,
then !ou could simpl! extend *ouse*otionAdapter and implement
mouse6ragged. ) .
25)What is .a$t &- %'$t#')s a$! what a# !i""#$t t-+s '" %'$t#')s i$
AWT?
Ans: %ontrols are components that allow a user to interact with !our application and
the A)+ supports the following t!pes of controls:
=abels, ush (uttons, %hec# (oxes, %hoice =ists, =ists, 4crollbars, +ext
%omponents.
+hese controls are subclasses of %omponent.
27) What is th !i""#$% &tw$ %h'i% a$! )ist?
Ans: A %hoice is displa!ed in a compact form that re$uires !ou to pull it down to see
the list of a&ailable choices and onl! one item ma! be selected from a choice.
A =ist ma! be displa!ed in such a wa! that se&eral list items are &isible and it
supports the selection of one or more list items.
28) What is th !i""#$% &tw$ s%#'))&a# a$! s%#'))+a$?
Ans: A 4crollbar is a %omponent, but not a %ontainer whereas 4crollpane is a
%onatiner and handles its own e&ents and perform its own scrolling.
29) What is a )a-'(t .a$a0# a$! what a# !i""#$t t-+s '" )a-'(t .a$a0#s
a4ai)a&) i$ 6a4aCawt?
Ans: A la!out manager is an object that is used to organize components in a
container. +he different la!outs are a&ailable are 0low=a!out, (order=a!out,
%ard=a!out, 8rid=a!out and 8rid(ag=a!out.
5:) ;'w a# th ).$ts '" !i""#$t )a-'(ts '#0a$i1!?
Ans: F)'wLa-'(t= +he elements of a 0low=a!out are organized in a top to bottom,
left to right fashion.
B'#!#La-'(t= +he elements of a (order=a!out are organized at the borders
.<orth, 4outh, "ast and )est)
and the center of a container.
3a#!La-'(t= +he elements of a %ard=a!out are stac#ed, on top of the other, li#e a
dec# of cards.
>#i!La-'(t= +he elements of a 8rid=a!out are of e$ual size and are laid out using
the s$uare of a grid.
>#i!Ba0La-'(t= +he elements of a 8rid(ag=a!out are organized according to a grid.
>owe&er, the elements are of different size and ma! occup! more than one row or
column of the grid. In addition, the rows and columns ma! ha&e different sizes.
51) Whi%h %'$tai$#s (s a B'#!# )a-'(t as thi# !"a()t )a-'(t?
Ans: )indow, 0rame and 6ialog classes use a (order=a!out as their la!out.
52) Whi%h %'$tai$#s (s a F)'w )a-'(t as thi# !"a()t )a-'(t?
Ans: anel and Applet classes use the 0low=a!out as their default la!out.
5*) What a# w#a++# %)asss?
Ans: )rapper classes are classes that allow primiti&e t!pes to be accessed as
objects.
5/) What a# V%t'#, ;ashta&), Li$A!List a$! E$(.#ati'$?
Ans: V%t'# = +he 5ector class pro&ides the capabilit! to implement a growable
arra! of objects.
;ashta&) = +he >ashtable class implements a >ashtable data structure. A
>ashtable indexes and stores
objects in a dictionar! using hash codes as the object-s #e!s. >ash codes are
integer &alues that identif! objects.
Li$A!List= 9emo&ing or inserting elements in the middle of an arra! can be done
using =in#ed=ist. A =in#ed=ist stores each object in a separate lin# whereas an
arra! stores object references in consecuti&e locations.
E$(.#ati'$= An object that implements the "numeration interface generates a
series of elements, one at a time. It has two methods, namel! has*ore"lements. )
and next"lement. ). has*ore"lemnts. ) tests if this enumeration has more elements
and next"lement method returns successi&e elements of the series.
52) What is th !i""#$% &tw$ st a$! )ist?
Ans: 4et stores elements in an unordered wa! but does not contain duplicate
elements, whereas list stores elements in an ordered wa! but ma! contain duplicate
elements.
55) What is a st#a. a$! what a# th t-+s '" St#a.s a$! %)asss '" th
St#a.s?
Ans: A 4tream is an abstraction that either produces or consumes information.
+here are two t!pes of 4treams and the! are:
B-t St#a.s= ro&ide a con&enient means for handling input and output of b!tes.
3ha#a%t# St#a.s= ro&ide a con&enient means for handling input ? output of
characters.
B-t St#a.s %)asss= Are defined b! using two abstract classes, namel!
Input4tream and Output4tream.
3ha#a%t# St#a.s %)asss= Are defined b! using two abstract classes, namel!
9eader and )riter.
57) What is th !i""#$% &tw$ Ra!#DW#it# a$! I$+(tSt#a.DO(t+(t
St#a.?
Ans: +he 9eader/)riter class is character-oriented and the
Input4tream/Output4tream class is b!te-oriented.
58) What is a$ IDO "i)t#?
Ans: An I/O filter is an object that reads from one stream and writes to another,
usuall! altering the data in some wa! as it is passed from one stream to another.
59) What is s#ia)i1ati'$ a$! !s#ia)i1ati'$?
Ans: 4erialization is the process of writing the state of an object to a b!te stream.
6eserialization is the process of restoring these objects.
7:) What is JDB3?
Ans: 76(% is a set of 7a&a AI for executing 4@= statements. +his AI consists of
a set of classes and interfaces to enable programs to write pure 7a&a 6atabase
applications.
71) What a# !#i4#s a4ai)a&)?
Ans: a) 76(%-O6(% (ridge dri&er
b) <ati&e AI artl!-7a&a dri&er
c) 76(%-<et ure 7a&a dri&er
d) <ati&e-rotocol ure 7a&a dri&er
72) What is th !i""#$% &tw$ JDB3 a$! ODB3?
Ans: a) O(6% is for *icrosoft and 76(% is for 7a&a applications.
b) O6(% can-t be directl! used with 7a&a because it uses a % interface.
c) O6(% ma#es use of pointers which ha&e been remo&ed totall! from 7a&a.
d) O6(% mixes simple and ad&anced features together and has complex options for
simple $ueries. (ut 76(% is designed to #eep things simple while allowing ad&anced
capabilities when re$uired.
e) O6(% re$uires manual installation of the O6(% dri&er manager and dri&er on all
client machines. 76(% dri&ers are written in 7a&a and 76(% code is automaticall!
installable, secure, and portable on all platforms.
f) 76(% AI is a natural 7a&a interface and is built on O6(%. 76(% retains some of
the basic features of O6(%.
7*) What a# th t-+s '" JDB3 D#i4# ?'!)s a$! <+)ai$ th.?
Ans: +here are two t!pes of 76(% 6ri&er *odels and the! are:
a) +wo tier model and b) +hree tier model
Tw' ti# .'!)= In this model, 7a&a applications interact directl! with the
database. A 76(% dri&er is re$uired to communicate with the particular database
management s!stem that is being accessed. 4@= statements are sent to the
database and the results are gi&en to user. +his model is referred to as
client/ser&er configuration where user is the client and the machine that has the
database is called as the ser&er.
Th# ti# .'!)= A middle tier is introduced in this model. +he functions of this
model are:
a) %ollection of 4@= statements from the client and handing it o&er to the
database,
b) 9ecei&ing results from database to the client and
c) *aintaining control o&er accessing and updating of the abo&e.
7/) What a# th st+s i$4')4! "'# .aAi$0 a %'$$%ti'$ with a !ata&as '#
h'w !' -'( %'$$%t t' a !ata&as?
Ans: a) L'a!i$0 th !#i4# = +o load the dri&er, %lass.for<ame. ) method is used.
%lass.for<ame.Asun.jdbc.odbc.7dbcOdbc6ri&erA),
)hen the dri&er is loaded, it registers itself with the ja&a.s$l.6ri&er*anager class
as an a&ailable database dri&er.
b) *a#ing a connection with database : +o open a connection to a gi&en database,
6ri&er*anager.get%onnection. ) method is used.
%onnection con B 6ri&er*anager.get%onnection .Ajdbc:odbc:somedbA, AuserA,
ApasswordA),
c) "xecuting 4@= statements : +o execute a 4@= $uer!, ja&a.s$l.statements class is
used.
create4tatement. ) method of %onnection to obtain a new 4tatement object.
4tatement stmt B con.create4tatement. ),
A $uer! that returns data can be executed using the execute@uer!. ) method of
4tatement. +his method executes the statement and returns a ja&a.s$l.9esult4et
that encapsulates the retrie&ed data:
9esult4et rs B stmt.execute@uer!.A4"="%+ C 09O* some tableA),
d) rocess the results : 9esult4et returns one row at a time. <ext. ) method of
9esult4et object can be called to mo&e to the next row. +he get4tring. ) and
getObject. ) methods are used for retrie&ing column &alues:
while.rs.next. ) ) D
4tring e&ent B rs.get4tring.Ae&entA),
Object count B .Integer) rs.getObject.AcountA),
72) What t-+ '" !#i4# !i! -'( (s i$ +#'6%t?
Ans: 76(%-O6(% (ridge dri&er .is a dri&er that uses nati&e.% language) libraries
and ma#es calls to an existing O6(% dri&er to access a database engine).
75) What a# th t-+s '" stat.$ts i$ JDB3?
Ans: 4tatement -- +o be used create4tatement.) method for executing
single 4@= statement
repared4tatement -- +o be used prepared4tatement.) method for
executing same 4@= statement o&er and o&er
%allable4tatement -- +o be used prepare%all. ) method for multiple 4@=
statements o&er and o&er
77) What is st'#! +#'%!(#?
Ans: 4tored procedure is a group of 4@= statements that forms a logical unit and
performs a particular tas#.
4tored rocedures are used to encapsulate a set of operations or $ueries to
execute on database.
4tored procedures can be compiled and executed with different parameters and
results and ma! ha&e an! combination of input/output parameters.
78) ;'w t' %#at a$! %a)) st'#! +#'%!(#s?
Ans: +o create stored procedures:
%reate procedure procedurename .specif! in, out and in out parameters)
("8I<
An! multiple 4@= statement,
"<6,
+o call stored procedures:
%allable4tatement csmt B con.prepare%all.ADcall procedure name.E,E)FA),
csmt.registerOutarameter.column no., data t!pe),
csmt.setInt.column no., column name)
csmt.execute. ),
79) What is s#4)t?
Ans: a) 4er&lets are modules that extend re$uest/response-oriented ser&ers, such
as ja&a-enabled web ser&ers.
b) 4er&let is bod! of ja&a code that is loaded into and runs inside a networ# ser&ice,
such as a )eb ser&er.
0or example, a ser&let might be responsible for ta#ing data in an >+*= order-entr!
form and appl!ing the business logic used to update a compan!-s order database.
8:) What a# th %)asss a$! i$t#"a%s "'# s#4)ts?
Ans: +here are two pac#ages in ser&lets and the! are ja&ax.ser&let and
ja&ax.ser&let.http. 7a&ax.ser&let contains:
I$t#"a%s 3)asss
4er&let 8eneric 4er&let
4er&let9e$uest 4er&letInput4tream
4er&let9esponse 4er&letOutput4tream
4er&let%onfig 4er&let"xception
4er&let%ontext 1na&ailable"xception
4ingle+hread*odel
7a&ax.ser&let.http contains:
I$t#"a%s 3)asss
>ttp4er&let9e$uest %oo#ie
>ttp4er&let9esponse >ttp4er&let
>ttp4ession >ttp4ession(inding"&ent
>ttp4ession%intext >ttp1tils
>ttp4eesion(inding=istener
81) What is th !i""#$% &tw$ a$ a++)t a$! a s#4)t?
Ans: a) 4er&lets are to ser&ers what applets are to browsers.
b) Applets must ha&e graphical user interfaces whereas ser&lets ha&e no graphical
user interfaces.
82) What is th !i""#$% &tw$ !'P'st a$! !'>t .th'!s?
Ans: a) do8et.) method is used to get information, while doost. ) method is used
for posting information.
b) do8et.) re$uests can-t send large amount of information and is limited to GHI-
GJJ characters. >owe&er, doost. )re$uests passes all of its data, of unlimited
length.
c) A do8et. ) re$uest is appended to the re$uest 19= in a $uer! string and this
allows the exchange is &isible to the client, whereas a doost.) re$uest passes
directl! o&er the soc#et connection as part of its >++ re$uest bod! and the
exchange are in&isible to the client.
8*) What is th )i" %-%) '" a s#4)t?
Ans: "ach 4er&let has the same life c!cle:
a) A ser&er loads and initializes the ser&let b! init .) method.
b) +he ser&let handles zero or more client-s re$uests through ser&ice. ) method.
c)+he ser&er remo&es the ser&let through destro!.) method.
8/) Wh' is )'a!i$0 th i$it() .th'! '" s#4)t?
Ans: )eb ser&er
82) What a# th !i""#$t s#4#s a4ai)a&) "'# !4)'+i$0 a$! !+)'-i$0
S#4)ts?
Ans: a) 7a&a )eb 4er&er
b) 79un
c) Apache 4er&er
f) <etscape Information 4er&er
g) )eb =ogic
85) ;'w .a$- wa-s %a$ w t#a%A %)i$t a$! what a# th-?
Ans: +he ser&let AI pro&ides two wa!s to trac# client state and the! are:
a) 1sing 4ession trac#ing and b) 1sing %oo#ies.
87) What is sssi'$ t#a%Ai$0 a$! h'w !' -'( t#a%A a (s# sssi'$ i$ s#4)ts?
Ans: 4ession trac#ing is a mechanism that ser&lets use to maintain state about a
series re$uests from the same user across some period of time. +he methods used
for session trac#ing are:
a) Us# A(th$ti%ati'$ - occurs when a web ser&er restricts access to some of its
resources to onl! those clients that log in using a recognized username and
password
b) ;i!!$ "'#. "i)!s - fields are added to an >+*= form that are not displa!ed
in the client-s browser. )hen the form containing the fields is submitted, the
fields are sent bac# to the ser&er
c) URL #w#iti$0 - e&er! 19= that the user clic#s on is d!namicall! modified or
rewritten to include extra information. +he extra information can be in the form of
extra path information, added parameters or some custom, ser&er-specific 19=
change.
d) 3''Ais - a bit of information that is sent b! a web ser&er to a browser and
which can later be read bac# from that browser.
e) ;tt+Sssi'$- places a limit on the number of sessions that can exist in memor!.
+his limit is set in the session.maxresidents propert!
88) What is S#4#@Si! I$%)(!s (SSI)?
Ans: 4er&er-4ide Includes allows embedding ser&lets within >+*= pages using a
special ser&let tag. In man! ser&lets that support ser&lets, a page can be processed
b! the ser&er to include output from ser&lets at certain points inside the >+*=
page. +his is accomplished using a special internal 44I<%=16", which processes
the ser&let tags. 44I<%=16" ser&let will be in&o#ed whene&er a file with an. shtml
extension is re$uested.
4o >+*= files that include ser&er-side includes must be stored with an .shtml
extension.
89) What a# %''Ais a$! h'w wi)) -'( (s th.?
Ans: %oo#ies are a mechanism that a ser&let uses to ha&e a client hold a small
amount of state-information associated with the user.
a) %reate a coo#ie with the %oo#ie constructor:
public %oo#ie.4tring name, 4tring &alue)
b) A ser&let can send a coo#ie to the client b! passing a %oo#ie object to the
add%oo#ie.) method of >ttp4er&let9esponse:
public &oid >ttp4er&let9esponse.add%oo#ie.%oo#ie coo#ie)
c) A ser&let retrie&es coo#ies b! calling the get%oo#ies.) method of
>ttp4er&let9e$uest:
public %oo#ieK L >ttp4er&let9e$uest.get%oo#ie. ).
9:) Is it +'ssi&) t' %'..($i%at "#'. a$ a++)t t' s#4)t a$! h'w .a$- wa-s
a$! h'w?
Ans: ;es, there are three wa!s to communicate from an applet to ser&let and the!
are: a) >++ %ommunication.+ext-based and object-based)
b) 4oc#et %ommunication
c) 9*I %ommunication
.;ou can sa!, b! using 19= object open the connection to ser&er and get the
Input4tream from 19=%onnection object).
4teps in&ol&ed for applet-ser&let communication:
2) 8et the ser&er 19=.
19= url B new 19=.),
G) %onnect to the host
19=%onnection %on B url.open%onnection.),
M) Initialize the connection
%on.set1se%atches.false):
%on.set6oOutput.true),
%on.set6oInput.true),
H) 6ata will be written to a b!te arra! buffer so that we can tell the ser&er the
length of the data.
(!teArra!Output4tream b!teout B new (!teArra!Output4tream.),
J) %reate the Output4tream to be used to write the data to the buffer.
6ataOutput4tream out B new 6ataOutput4tream.b!teout),
91) What is %'$$%ti'$ +'')i$0?
Ans: )ith ser&lets, opening a database connection is a major bottlenec# because we
are creating and tearing down a new connection for e&er! page re$uest and the time
ta#en to create connection will be more.
%reating a connection pool is an ideal approach for a complicated ser&let. )ith a
connection pool, we can duplicate onl! the resources we need to duplicate rather
than the entire ser&let. A connection pool can also intelligentl! manage the size of
the pool and ma#e sure each connection remains &alid. A number of connection pool
pac#ages are currentl! a&ailable. 4ome li#e 6b%onnection(ro#er are freel! a&ailable
from 7a&a "xchange )or#s b! creating an object that dispenses connections and
connection Ids on re$uest.
+he %onnectionool class maintains a >astable, using %onnection objects as #e!s and
(oolean &alues as stored &alues. +he (oolean &alue indicates whether a connection is
in use or not. A program calls get%onnection. ) method of the %onnectionool for
getting %onnection object it can use, it calls return%onnection. ) to gi&e the
connection bac# to the pool.
92) Wh- sh'()! w 0' "'# i$t#s#4)t %'..($i%ati'$?
Ans: 4er&lets running together in the same ser&er communicate with each other in
se&eral wa!s.
+he three major reasons to use interser&let communication are:
a) Di#%t s#4)t .a$i+()ati'$ - allows to gain access to the other currentl! loaded
ser&lets and perform certain tas#s .through the 4er&let%ontext object)
b) S#4)t #(s - allows the ser&let to reuse the public methods of another
ser&let.
c) S#4)t %'))a&'#ati'$ - re$uires to communicate with each other b! sharing
specific information .through method in&ocation)
9*) Is it +'ssi&) t' %a)) s#4)t with +a#a.t#s i$ th URL?
Ans: ;es. ;ou can call a ser&let with parameters in the s!ntax as .Earam2 B xxx NN
mG B !!!).
9/) What is S#4)t %hai$i$0?
Ans: 4er&let chaining is a techni$ue in which two or more ser&lets can cooperate in
ser&icing a single re$uest.
In ser&let chaining, one ser&let-s output is piped to the next ser&let-s input. +his
process continues until the last ser&let is reached. Its output is then sent bac# to
the client.
92) ;'w !' s#4)ts ha$!) .()ti+) si.()ta$'(s #E(sts?
Ans: +he ser&er has multiple threads that are a&ailable to handle re$uests. )hen a
re$uest comes in, it is assigned to a thread, which calls a ser&ice method .for
example: do8et.), doost. ) and ser&ice. ) ) of the ser&let.
0or this reason, a single ser&let object can ha&e its ser&ice methods called b! man!
threads at once.
95) What is th !i""#$% &tw$ T3PDIP a$! UDP?
Ans: +%/I is a two-wa! communication between the client and the ser&er and it is
a reliable and there is a confirmation regarding reaching the message to the
destination. It is li#e a phone call.
16 is a one-wa! communication onl! between the client and the ser&er and it is not
a reliable and there is no confirmation regarding reaching the message to the
destination. It is li#e a postal mail.
97) What is I$t a!!#ss?
Ans: "&er! computer connected to a networ# has an I address. An I address is a
number that uni$uel! identifies each computer on the <et. An I address is a MG-
bit number.
98) What is D'.ai$ Na.i$0 S#4i%(DNS)?
Ans: It is &er! difficult to remember a set of numbers.I address) to connect to
the Internet. +he 6omain <aming 4er&ice.6<4) is used to o&ercome this problem.
It maps one particular I address to a string of characters.
0or example, www.mascom.com implies com is the domain name reser&ed for 14
commercial sites, moscom is the name of the compan! and www is the name of the
specific computer, which is mascom-s ser&er.
99) What is URL?
Ans: 19= stands for 1niform 9esource =ocator and it points to resource files on
the Internet.
19= has four components:
http://www.entafour.com:'I/index.html
http - protocol name, entafour - I address or host name, 'I - port number and
index.html - file path.
1::) What is R?I a$! st+s i$4')4! i$ !4)'+i$0 a$ R?I '&6%t?
Ans: 9emote *ethod In&ocation .9*I) allows ja&a object that executes on one
machine and to in&o#e the method of a 7a&a object to execute on another machine.
+he steps in&ol&ed in de&eloping an 9*I object are:
a) 6efine the interfaces
b) Implementing these interfaces
c) %ompile the interfaces and their implementations with the ja&a compiler
d) %ompile the ser&er implementation with 9*I compiler
e) 9un the 9*I registr! f) 9un the application

1:1) What is R?I a#%hit%t(#?
Ans: - 9*I architecture consists of four la!ers and each la!er performs specific
functions:
a) Application la!er ---- contains the actual object definition
b) rox! la!er ---- consists of stub and s#eleton
c) 9emote 9eference la!er ---- gets the stream of b!tes from the transport
la!er and sends it to the prox! la!er
d) +ransportation la!er ---- responsible for handling the actual
machine-to-machine communication
1:2) what is U$i%astR.'tO&6%t?
Ans: All remote objects must extend 1nicast9emoteObject, which pro&ides
functionalit! that is needed to ma#e objects a&ailable from remote machines.
1:*) E<+)ai$ th .th'!s, #&i$!( ) a$! )''A(+() i$ Na.i$0 %)ass?
Ans: rebind. ) of the <aming class.found in ja&a.rmi) is used to update the 9*I
registr! on the ser&er machine.
<aming. rebind.AAdd4e&erA, Add4er&erImpl),
loo#up. ) of the <aming class accepts one argument, the rmi 19= and returns a
reference to an object of t!pe Add4er&erImpl.
1:/) What is a Ja4a Ba$?
Ans: A 7a&a (ean is a reusable, interoperable software component that can be
&isuall! manipulated in a builder tool.I6").
1:2) What is a Ja# "i)?
Ans: 7ar file allows to efficientl! deplo!ing a set of classes and their associated
resources. +he elements in a jar file are compressed, which ma#es downloading a
7ar file much faster than separatel! downloading se&eral uncompressed files.
+he pac#age ja&a.util.zip contains classes that read and write jar files.
1:5) What is BDK?
Ans: (6:, (ean 6e&elopment :it is a tool that enables to create, configure and
connect a set of set of (eans and it can be used to test (eans without writing a
code.
1:7) What is JSP?
Ans: 74 is a d!namic scripting capabilit! for web pages that allows 7a&a as well as
a few special tags to be embedded into a web file .>+*=/O*=, etc). +he suffix
traditionall! ends with .jsp to indicate to the web ser&er that the file is a 74 files.
74 is a ser&er side technolog! - !ou can-t do an! client side &alidation with it.
+he ad&antages are:
a) +he 74 assists in ma#ing the >+*= more functional. 4er&lets on the other hand
allow outputting of >+*= but it is a tedious process.
b) It is eas! to ma#e a change and then let the 74 capabilit! of the web ser&er
!ou are using deal with compiling it into a ser&let and running it.
1:8) What a# JSP s%#i+ti$0 ).$ts?
Ans: 74 scripting elements lets to insert 7a&a code into the ser&let that will be
generated from the current 74 page. +here are three forms:
a) E<+#ssi'$s of the form PQB expression QR that are e&aluated and
inserted into the output,
b) S%#i+t)ts of the form PQ code QR that are inserted into the ser&let-s
ser&ice method, and
c) D%)a#ati'$s of the form PQS %ode QR that are inserted into the bod! of
the ser&let class, outside of an! existing methods.
1:9) What a# JSP Di#%ti4s?
Ans: A 74 directi&e affects the o&erall structure of the ser&let class. It usuall!
has the following form:
PQT directi&e attributeBA&alueA QR
>owe&er, !ou can also combine multiple attribute settings for a single directi&e, as
follows:
PQT directi&e attribute2BA&alue2A
attribute GBA&alueGA
...
attribute< BA&alue<A QR
+here are two main t!pes of directi&e: page, which lets to do things li#e import
classes, customize the ser&let superclass, and the li#e, and include, which lets to
insert a file into the ser&let class at the time the 74 file is translated into a
ser&let
11:) What a# P#!"i$! 4a#ia&)s '# i.+)i%it '&6%ts?
Ans: +o simplif! code in 74 expressions and scriptlets, we can use eight
automaticall! defined &ariables, sometimes called implicit objects. +he! are
re$uest, response, out, session, application, config, page%ontext, and page.
111) What a# JSP A3TIONS?
Ans: 74 actions use constructs in O*= s!ntax to control the beha&ior of the
ser&let engine. ;ou can d!namicall! insert a file, reuse 7a&a(eans components,
forward the user to another page, or generate >+*= for the 7a&a plugin. A&ailable
actions include:
E jsp:include - Include a file at the time the page is re$uested.
E jsp:use(ean - 0ind or instantiate a 7a&a(ean.
E jsp:setropert! - 4et the propert! of a 7a&a(ean.
E jsp:getropert! - Insert the propert! of a 7a&a(ean into the output.
E jsp:forward - 0orward the re$uester to a newpage.
E 7sp: plugin - 8enerate browser-specific code that ma#es an O(7"%+ or "*("6
112) ;'w !' -'( +ass !ata (i$%)(!i$0 Ja4aBa$s) t' a JSP "#'. a s#4)t?
Ans: (1) RE(st Li"ti.= 1sing this techni$ue to pass beans, a re$uest
dispatcher .using either AincludeA or forwardA) can be called. +his bean will
disappear after processing this re$uest has been completed.
4er&let:
re$uest.setAttribute.Athe(eanA, m!(ean),
9e$uest6ispatcher rd Bget4er&let%ontext.).get9e$uest6ispatcher.Athepage.jspA),
rd.forward.re$uest, response),
74 A8":
Pjsp: use(ean idBAthe(eanA scopeBAre$uestA classBA.....A /R
.G) Sssi'$ Li"ti.= 1sing this techni$ue to pass beans that are rele&ant
to a particular session .such as in indi&idual user login) o&er a number of re$uests.
+his bean will disappear when the session is in&alidated or it times out, or when !ou
remo&e it.
4er&let:
>ttp4ession session B re$uest.get4ession.true),
session.put5alue.Athe(eanA, m!(ean),
/C ;ou can do a re$uest dispatcher here,
or just let the bean be &isible on the
next re$uest C/
74 age:
Pjsp:use(ean idBAthe(eanA scopeBAsessionA classBA...A /R
.M) A++)i%ati'$ Li"ti.= 1sing this techni$ue to pass beans that are
rele&ant to all ser&lets and 74 pages in a particular app, for all users. 0or example,
I use this to ma#e a 76(% connection pool object a&ailable to the &arious ser&lets
and 74 pages in m! apps. +his bean will disappear when the ser&let engine is shut
down, or when !ou remo&e it.
4er&let:
8et4er&let%ontext.). setAttribute.Athe(eanA, m!(ean),
74 A8":
Pjsp:use(ean idBAthe(eanA scopeBAapplicationA classBA...A /R
11*) ;'w %a$ I st a %''Ai i$ JSP?
Ans: response.set>eader.A4et-%oo#ieA, Acoo#ie stringA),
+o gi&e the response-object to a bean, write a method set9esponse
.>ttp4er&let9esponse response)
- to the bean, and in jsp-file:
PQ
bean.set9esponse .response),
QR
11/) ;'w %a$ I !)t a %''Ai with JSP?
Ans: 4a! that I ha&e a coo#ie called Afoo,A that I set a while ago ? I want it to go
awa!. I simpl!:
PQ
%oo#ie #ill%oo#ie B new %oo#ie.AfooA, null),
:ill%oo#ie.setath.A/A),
#ill%oo#ie.set*axAge.I),
response.add%oo#ie.#ill%oo#ie),
QR
112) ;'w a# S#4)ts a$! JSP Pa0s #)at!?
Ans: 74 pages are focused around >+*= .or O*=) with 7a&a codes and 74 tags
inside them. )hen a web ser&er that has 74 support is as#ed for a 74 page, it
chec#s to see if it has alread! compiled the page into a ser&let. +hus, 74 pages
become ser&lets and are transformed into pure 7a&a and then compiled, loaded into
the ser&er and executed.

Anda mungkin juga menyukai