Вы находитесь на странице: 1из 22

1

Core java Interview


Questions
Q1. What is the difference between an Abstract class and Interface?
1. Abstract classes may have some executable methods and methods left
unimplemented. Interfaces contain no implementation code.
2. An class can implement any number of interfaces, but subclass at most one abstract class.
3. An abstract class can have nonabstract methods. All methods of an interface are
abstract.
4. An abstract class can have instance variables. An interface cannot.
5. An abstract class can define constructor. An interface cannot.
6. An abstract class can have any visibility public, protected, private or none
!pac"a#e$. An interface%s visibility must be public or none !pac"a#e$.
&. An abstract class inherits from 'b(ect and includes methods such as clone!$ and
e)uals!$.
Q2.What are checked and unchecked exceptions?
*ava defines t+o "inds of exceptions
, Checked exceptions : -xceptions that inherit from the Exception class are
checked exceptions. .lient code has to handle the chec"ed exceptions thro+n by
the A/I, either in a catch clause or by for+ardin# it out+ard +ith the thro+s clause.
-xamples 0 Q!Exception" I#xception.
, $nchecked exceptions : 1untime-xception also extends from -xception. 2o+ever,
all of the exceptions that inherit from 1untime-xception #et special treatment.
3here is no re)uirement for the client code to deal +ith them, and hence they are
called unchec"ed exceptions. -xample 4nchec"ed exceptions are
%ull&ointerException" #ut#f'e(or)Error" *i+ide,)-eroException t)picall)"
pro.ra((in. errors.
Q/.What is a user defined exception?
4ser0defined exceptions may be implemented by
, definin# a class to respond to the exception and
, embeddin# a thro+ statement in the try bloc" +here the exception can occur or
declarin# that the method thro+s the exception !to another method +here it is
handled$.
3he developer can define a ne+ exception by derivin# it from the -xception class as follo+s
public class 5y-xception extends -xception 6
78 class definition of constructors !but 9'3 the exception handlin# code$ #oes here public
5y-xception!$ 6
super!$:
;
public 5y-xception! <trin# error5essa#e $ 6
super! error5essa#e $:
;
;
3he thro+ statement is used to si#nal the occurance of the exception +ithin a try bloc". 'ften,
exceptions are instantiated in the same statement in +hich they are thro+n usin# the
syntax.
thro+ ne+ 5y-xception!=I thre+ my o+n exception.=$
1
Core java Interview
Questions
3o handle the exception +ithin the method +here it is thro+n, a catch statement that handles
5y-xception, must follo+ the try bloc". If the developer does not +ant to handle the exception
in the method itself, the method must pass the exception usin# the syntax
public ()'ethod%a(e01 throws ')Exception
Q2.What is the difference between C33 4 5a+a?
>ell as ?(arne <troustrup says =..despite the syntactic similarities, .@@ and *ava are very
different lan#ua#es. In many +ays, *ava seems closer to <malltal" than to .@@..=. 2ere are fe+ I
discovered
, *ava is multithreaded
, *ava has no pointers
, *ava has automatic memory mana#ement !#arba#e collection$
, *ava is platform independent !<troustrup may differ by sayin# =*ava is a platform=
, *ava has built0in support for comment documentation
, *ava has no operator overloadin#
, *ava doesnAt provide multiple inheritance
, 3here are no destructors in *ava
B5.>hat are statements in *ACA D
<tatements are e)uivalent to sentences in natural lan#ua#es. A statement forms a complete
unit of execution. 3he follo+in# types of expressions can be made into a statement by
terminatin# the expression +ith a semicolon
, Assi#nment expressions
, Any use of @@ or 00
, 5ethod calls
, 'b(ect creation expressions
3hese "inds of statements are called expression statements. In addition to these "inds of
expression statements, there are t+o other "inds of statements. A declaration statement
declares a variable. A control flo+ statement re#ulates the order in +hich statements #et
executed. 3he for loop and the if statement are both examples of control flo+ statements.
Q6.What is 5A7 file?
*avaA1chive files are a bi# #lob of *ava classes, ima#es, audio, etc., compressed to ma"e
one simple, smaller file to ease Applet do+nloadin#. 9ormally +hen a bro+ser encounters
an applet, it #oes and do+nloads all the files, ima#es, audio, used by the Applet separately.
3his can lead to slo+er do+nloads.
Q8.What is 5%I?
*9I is an acronym of *ava 9ative Interface. 4sin# *9I +e can call functions +hich are +ritten in
other lan#ua#es from *ava. Eollo+in# are its advanta#es and disadvanta#es.
Ad+anta.es:
, Fou +ant to use your existin# library +hich +as previously +ritten in other
lan#ua#e.
, Fou +ant to call >indo+s A/I function.
, Eor the sa"e of execution speed.
, Fou +ant to call A/I function of some server product +hich is in c or c@@ from (ava client.
1
Core java Interview
Questions
*isad+anta.es:
, Fou canAt say +rite once run any+here.
, Gifficult to debu# runtime error in native code.
, /otential security ris".
, Fou canAt call it from Applet.
Q9.What is seriali:ation?
Buite simply, ob(ect serialiHation provides a pro#ram the ability to read or +rite a +hole ob(ect
to and from a ra+ byte stream. It allo+s *ava ob(ects and primitives to be encoded into a byte
stream suitable for streamin# to some type of net+or" or to a file0system, or more #enerally, to a
transmission medium or stora#e facility. A seraliHable ob(ect must implement the <eriliHable
interface. >e use 'b(ect'utput<tream to +rite this ob(ect to a stream and 'b(ectInput<tream to
read it from the stream.
BI.>hy there are some null interface in (ava D >hat does it (ean ? ;i+e (e so(e null
interfaces in 5A<A?
9ull interfaces act as mar"ers..they (ust tell the compiler that the ob(ects of this class need to be
treated differently..some mar"er interfaces are <erialiHable, 1emote, .loneable
Q1=. Is s)nchronised a (odifier?indentifier??what is it??
It%s a modifier. <ynchroniHed methods are methods that are used to control access to an ob(ect. A
thread only executes a synchroniHed method after it has ac)uired the loc" for the method%s ob(ect
or class. <ynchroniHed statements are similar to synchroniHed methods. A synchroniHed
statement can only be executed after a thread has ac)uired the loc" for the ob(ect or class
referenced in the synchroniHed statement.
Q11.What is sin.leton class?where is it used?
<in#leton is a desi#n pattern meant to provide one and only one instance of an ob(ect. 'ther
ob(ects can #et a reference to this instance throu#h a static method !class constructor is "ept
private$. >hy do +e need oneD <ometimes it is necessary, and often sufficient, to create a sin#le
instance of a #iven class. 3his has advanta#es in memory mana#ement, and for *ava, in #arba#e
collection. 5oreover, restrictin# the number of instances may be necessary or desirable for
technolo#ical or business reasons00for example, +e may only +ant a sin#le instance of a pool of
database connections.
Q12.What is a co(pilation unit?
3he smallest unit of source code that can be compiled, i.e. a .(ava file.
B13.Is strin# a +rapper classD
<trin# is a class, but not a +rapper class. >rapper classes li"e !Inte#er$ exist for each primitive
type. 3hey can be used to convert a primitive data value into an ob(ect, and viceversa.
Q12.Wh) >a+a does not ha+e (ultiple inheritance?
3he *ava desi#n team strove to ma"e *ava
, <imple, ob(ect oriented, and familiar
, 1obust and secure
1
Core java Interview
Questions
, Architecture neutral and portable
, 2i#h performance
, Interpreted, threaded, and dynamic
3he reasons for omittin# multiple inheritance from the *ava lan#ua#e mostly stem from the
=simple, ob(ect oriented, and familiar= #oal. As a simple lan#ua#e, *ava%s creators +anted a
lan#ua#e that most developers could #rasp +ithout extensive trainin#. 3o that end, they +or"ed
to ma"e the lan#ua#e as similar to .@@ as possible !familiar$ +ithout carryin# over .@@%s
unnecessary complexity !simple$.
In the desi#ners% opinion, multiple inheritance causes more problems and confusion than it
solves. <o they cut multiple inheritance from the lan#ua#e !(ust as they cut operator
overloadin#$. 3he desi#ners% extensive .@@ experience tau#ht them that multiple inheritance (ust
+asn%t +orth the headache.
Q1?.Wh) >a+a is not a 1==@ oops?
5any people say this because *ava uses primitive types such as int, char, double. ?ut then all the
rest are ob(ects. .onfusin# )uestion.
Q16.What is a resource bundle?
In its simplest form, a resource bundle is represented by a text file containin# "eys and a text
value for each "ey.
B1&.>hat is transient variableD
3ransient variable can%t be serialiHe. Eor example if a variable is declared as transient in a
<erialiHable class and the class is +ritten to an 'b(ect<tream, the value of the variable can%t be
+ritten to the stream instead +hen the class is retrieved from the 'b(ect<tream the value of the
variable becomes null.
Q19.What is Collection A&I?
3he .ollection A/I is a set of classes and interfaces that support operation on collections of
ob(ects. 3hese classes and interfaces are more flexible, more po+erful, and more re#ular than the
vectors, arrays, and hashtables if effectively replaces.
Exa(ple of classes: 2ash<et, 2ash5ap, ArrayJist, Jin"edJist, 3ree<et and 3ree5ap.
Exa(ple of interfaces: .ollection, <et, Jist and 5ap.
Q1A.Is Iterator a Class or Interface? What is its use?
Iterator is an interface +hich is used to step throu#h the elements of a .ollection.
Q2=.What is si(ilaritiesBdifference between an Abstract class and Interface?
*ifferences are as follows:
, Interfaces provide a form of multiple inheritance. A class can extend only one other
class.
, Interfaces are limited to public methods and constants +ith no implementation.
Abstract classes can have a partial implementation, protected parts, static methods,
etc.
, A .lass may implement several interfaces. ?ut in case of abstract class, a class may extend only
one abstract class.
1
Core java Interview
Questions
, Interfaces are slo+ as it re)uires extra indirection to to find correspondin# method
in in the actual class. Abstract classes are fast.
i(ilarities:
, 9either Abstract classes or Interface can be instantiated.
B21.>hat is a transient variableD
A transient variable is a variable that may not be serialiHed.
Q22.Which containers use a border !a)out as their default la)out?
3he +indo+, Erame and Gialo# classes use a border layout as their default layout.
Q2/.Wh) do threads block on IB#?
3hreads bloc" on i7o !that is enters the +aitin# state$ so that other threads may execute +hile the
i7o 'peration is performed.
Q22.Cow are #bser+er and #bser+able used?
'b(ects that subclass the 'bservable class maintain a list of observers. >hen an 'bservable
ob(ect is updated it invo"es the update!$ method of each of its observers to notify the observers
that it has chan#ed state. 3he 'bserver interface is implemented by ob(ects that observe
'bservable ob(ects.
B25.>hat is synchroniHation and +hy is it importantD
>ith respect to multithreadin#, synchroniHation is the capability to control the access of multiple
threads to shared resources. >ithout synchroniHation, it is possible for one thread to modify a
shared ob(ect +hile another thread is in the process of usin# or updatin# that ob(ect%s value. 3his
often leads to si#nificant errors.
Q26. Can a lock be acDuired on a class?
Fes, a loc" can be ac)uired on a class. 3his loc" is ac)uired on the class%s .lass ob(ect.
Q28. WhatEs new with the stop01" suspend01 and resu(e01 (ethods in 5*F 1.2?
3he stop!$, suspend!$ and resume!$ methods have been deprecated in *GK 1.2.
Q29. Is null a ke)word?
3he null value is not a "ey+ord.
B2I. >hat is the preferred siHe of a componentD
3he preferred siHe of a component is the minimum component siHe that +ill allo+ the
component to display normally.
B3L. >hat method is used to specify a container%s layoutD
3he setJayout!$ method is used to specify a container%s layout.
Q/1. Which containers use a Glow!a)out as their default la)out?
3he /anel and Applet classes use the Elo+Jayout as their default layout.
1
Core java Interview
Questions
Q/2. What state does a thread enter when it ter(inates its processin.?
>hen a thread terminates its processin#, it enters the dead state.
B33. >hat is the .ollections A/ID
3he .ollections A/I is a set of classes and interfaces that support operations on collections of
ob(ects.
Q/2. Which characters (a) be used as the second character
of an identifier" but not as the first character of an identifier?
3he di#its L throu#h I may not be used as the first character of an identifier but they may be used
after the first character of an identifier.
Q/?. What is the !ist interface?
3he Jist interface provides support for ordered collections of ob(ects.
Q/6. Cow does 5a+a handle inte.er o+erflows and underflows?
It uses those lo+ order bytes of the result that can fit into the siHe of the type allo+ed by the
operation.
B3&. >hat is the Cector classD
3he Cector class provides the capability to implement a #ro+able array of ob(ects
Q/9. What (odifiers (a) be used with an inner class that is a (e(ber of an outer class?
A !non0local$ inner class may be declared as public, protected, private, static, final, or abstract.
Q/A. What is an Iterator interface?
3he Iterator interface is used to step throu#h the elements of a .ollection.
Q2=. What is the difference between the HH and HHH operators?
3he MM operator carries the si#n bit +hen shiftin# ri#ht. 3he MMM Hero0fills bits that have been
shifted out.
Q21. Which (ethod of the Co(ponent class is used to set the position and si:e of a
co(ponent?
set?ounds!$
Q22. Cow (an) bits are used to represent $nicode" ACII" $IGJ16" and $IGJ9
characters?
4nicode re)uires 16 bits and A<.II re)uire & bits. Althou#h the A<.II character set uses only &
bits, it is usually represented as N bits. 43E0N represents characters usin# N, 16, and 1N bit
patterns. 43E016 uses 160bit and lar#er bit patterns.
Q2/. What is the difference between )ieldin. and sleepin.?
>hen a tas" invo"es its yield!$ method, it returns to the ready state. >hen a tas" invo"es its
sleep!$ method, it returns to the +aitin# state.
1
Core java Interview
Questions
Q22. Which >a+a.util classes and interfaces support e+ent handlin.?
3he -vent'b(ect class and the -ventJistener interface support event processin#.
B45. Is siHeof a "ey+ordD
3he siHeof operator is not a "ey+ord.
Q26. What are wrapped classes?
>rapped classes are classes that allo+ primitive types to be accessed as ob(ects.
Q28. *oes .arba.e collection .uarantee that a pro.ra( will not run out of (e(or)?
Oarba#e collection does not #uarantee that a pro#ram +ill not run out of memory. It is possible
for pro#rams to use up memory resources faster than they are #arba#e collected. It is also
possible for pro#rams to create ob(ects that are not sub(ect to #arba#e collection
Q29. What restrictions are placed on the location of a packa.e state(ent within a source
code file?
A pac"a#e statement must appear as the first line in a source code file !excludin# blan" lines and
comments$.
Q2A. Can an ob>ectEs finali:e01 (ethod be in+oked while it is reachable?
An ob(ect%s finaliHe!$ method cannot be invo"ed by the #arba#e collector +hile the ob(ect is still
reachable. 2o+ever, an ob(ect%s finaliHe!$ method may be invo"ed by other ob(ects.
Q?=. What is the i((ediate superclass of the Applet class?
/anel
Q?1. What is the difference between pree(pti+e schedulin. and ti(e slicin.?
4nder preemptive schedulin#, the hi#hest priority tas" executes until it enters the +aitin# or dead
states or a hi#her priority tas" comes into existence. 4nder time slicin#, a tas" executes for a
predefined slice of time and then reenters the pool of ready tas"s. 3he scheduler then determines
+hich tas" should execute next, based on priority and other factors.
Q?2 %a(e three Co(ponent subclasses that support paintin..
3he .anvas, Erame, /anel, and Applet classes support paintin#.
Q?/. What +alue does read!ine01 return when it has reached the end of a file?
3he readJine!$ method returns null +hen it has reached the end of a file.
B54. >hat is the immediate superclass of the Gialo# classD
>indo+
Q??. What is clippin.?
.lippin# is the process of confinin# paint operations to a limited area or shape.
Q?6. What is a nati+e (ethod?
A native method is a method that is implemented in a lan#ua#e other than *ava.
1
Core java Interview
Questions
B5&. .an a for statement loop indefinitelyD
Fes, a for statement can loop indefinitely. Eor example, consider the follo+in# for!::$ :
Q?9. What are order of precedence and associati+it)" and how are the) used?
'rder of precedence determines the order in +hich operators are evaluated in expressions.
Associatity determines +hether an expression is evaluated left0to0ri#ht or ri#ht0to0left
Q?A. When a thread blocks on IB#" what state does it enter?
A thread enters the +aitin# state +hen it bloc"s on I7'.
Q6=. Io what +alue is a +ariable of the trin. t)pe auto(aticall) initiali:ed?
3he default value of an <trin# type is null.
Q61. What is the catch or declare rule for (ethod declarations?
If a chec"ed exception may be thro+n +ithin the body of a method, the method must either catch
the exception or declare it in its thro+s clause.
Q62. What is the difference between a 'enuIte( and a Checkbox'enuIte(?
3he .hec"box5enuItem class extends the 5enuItem class to support a menu item that may be
chec"ed or unchec"ed.
Q6/. What is a taskEs priorit) and how is it used in schedulin.?
A tas"%s priority is an inte#er value that identifies the relative order in +hich it should be
executed +ith respect to other tas"s. 3he scheduler attempts to schedule hi#her priority tas"s
before lo+er priority tas"s.
Q62. What class is the top of the AWI e+ent hierarch)?
3he (ava.a+t.A>3-vent class is the hi#hest0level class in the A>3 event0class hierarchy.
Q6?. When a thread is created and started" what is its initial state?
A thread is in the ready state after it has been created and started.
Q66. Can an anon)(ous class be declared as i(ple(entin. an interface and extendin. a
class?
An anonymous class may implement an interface or extend a superclass, but may not be declared
to do both.
Q68. What is the ran.e of the short t)pe?
3he ran#e of the short type is 0!2P15$ to 2P15 0 1.
Q69. What is the ran.e of the char t)pe?
3he ran#e of the char type is L to 2P16 0 1.
Q6A. In which packa.e are (ost of the AWI e+ents that support the e+entJdele.ation (odel
defined?
1
Core java Interview
Questions
5ost of the A>30related events of the event0dele#ation model are defined in the
(ava.a+t.event pac"a#e. 3he A>3-vent class is defined in the (ava.a+t pac"a#e.
Q8=. What is the i((ediate superclass of 'enu?
5enuItem
Q81. What is the purpose of finali:ation?
3he purpose of finaliHation is to #ive an unreachable ob(ect the opportunity to perform any
cleanup processin# before the ob(ect is #arba#e collected.
Q82. Which class is the i((ediate superclass of the 'enuCo(ponent class.
'b(ect
B&3. >hat invo"es a thread%s run!$ methodD
After a thread is started, via its start!$ method or that of the 3hread class, the *C5 invo"es the
thread%s run!$ method +hen the thread is initially executed.
Q82. What is the difference between the ,oolean 4 operator and the 44 operator?
If an expression involvin# the ?oolean Q operator is evaluated, both operands are
evaluated. 3hen the Q operator is applied to the operand. >hen an expression involvin# the QQ
operator is evaluated, the first operand is evaluated. If the first operand returns a value of true
then the second operand is evaluated. 3he QQ operator is then applied to the first and second
operands. If the first operand evaluates to false, the evaluation of the second operand is s"ipped.
Q8?. %a(e three subclasses of the Co(ponent class.
?ox.Eiller, ?utton, .anvas, .hec"box, .hoice, .ontainer, Jabel, Jist, <crollbar, or
3ext.omponent
Q86. What is the ;re.orianCalendar class?
3he Ore#orian.alendar provides support for traditional >estern calendars.
Q88. Which Container (ethod is used to cause a container to be laid out and redispla)ed?
validate!$
Q89. What is the purpose of the 7unti(e class?
3he purpose of the 1untime class is to provide access to the *ava runtime system.
Q8A. Cow (an) ti(es (a) an ob>ectEs finali:e01 (ethod be in+oked b) the .arba.e
collector?
An ob(ect%s finaliHe!$ method may only be invo"ed once by the #arba#e collector.
Q9=. What is the purpose of the finall) clause of a tr)Jcatchfinall) state(ent?
3he finally clause is used to provide the capability to execute code no matter +hether or not an
exception is thro+n or cau#ht.
1
Core java Interview
Questions
Q91. What is the ar.u(ent t)pe of a pro.ra(Es (ain01 (ethod?
A pro#ram%s main!$ method ta"es an ar#ument of the <trin#RS type.
Q92. Which 5a+a operator is ri.ht associati+e?
3he T operator is ri#ht associative.
Q9/. What is the !ocale class?
3he Jocale class is used to tailor pro#ram output to the conventions of a particular
#eo#raphic, political, or cultural re#ion.
Q92. Can a double +alue be cast to a b)te?
Fes, a double value can be cast to a byte.
Q9?. What is the difference between a break state(ent and a continue state(ent?
A brea" statement results in the termination of the statement to +hich it applies !s+itch, for, do,
or +hile$. A continue statement is used to end the current loop iteration and return control to the
loop statement.
Q96. What (ust a class do to i(ple(ent an interface?
It must provide all of the methods in the interface and identify the interface in its
implements clause.
Q98. What (ethod is in+oked to cause an ob>ect to be.in executin. as a separate thread?
3he start!$ method of the 3hread class is invo"ed to cause an ob(ect to be#in executin# as a
separate thread.
Q99. %a(e two subclasses of the IextCo(ponent class.
3extEield and 3extArea
Q9A. What is the ad+anta.e of the e+entJdele.ation (odel o+er the earlier e+entinheritance
(odel?
3he event0dele#ation model has t+o advanta#es over the event0inheritance model. Eirst, it
enables event handlin# to be handled by ob(ects other than the ones that #enerate the events !or
their containers$. 3his allo+s a clean separation bet+een a component%s desi#n and its use. 3he
other advanta#e of the event0dele#ation model is that it performs much better in applications
+here many events are #enerated. 3his performance improvement is due to the fact that the
event0dele#ation model does not have to repeatedly process unhandled events, as is the case of
the event0inheritance model.
QA=. Which containers (a) ha+e a 'enu,ar?
Erame
QA1. Cow are co((as used in the intiali:ation and iteration parts of a for state(ent?
.ommas are used to separate multiple statements +ithin the initialiHation and iteration parts of a
for statement.
1
Core java Interview
Questions
QA2. What is the purpose of the wait01" notif)01" and notif)All01 (ethods?
3he +ait!$,notify!$, and notifyAll!$ methods are used to provide an efficient +ay for threads to
+ait for a shared resource. >hen a thread executes an ob(ect%s +ait!$ method, it enters the
+aitin# state. It only enters the ready state after another thread invo"es the ob(ect%s notify!$ or
notifyAll!$ methods..
BI3. >hat is an abstract methodD
An abstract method is a method +hose implementation is deferred to a subclass.
BI4. 2o+ are *ava source code files namedD
A *ava source code file ta"es the name of a public class or interface that is defined +ithin the
file. A source code file may contain at most one public class or interface. If a public class or
interface is defined +ithin a source code file, then the source code file must ta"e the name of the
public class or interface. If no public class or interface is defined +ithin a source code file, then
the file must ta"e on a name that is different than its classes and interfaces. <ource code files use
the .(ava extension.
QA?. What is the relationship between the Can+as class and the ;raphics class?
A .anvas ob(ect provides access to a Oraphics ob(ect via its paint!$ method.
QA6. What are the hi.hJle+el thread states?
3he hi#h0level thread states are ready, runnin#, +aitin#, and dead.
QA8. What +alue does read01 return when it has reached the end of a file?
3he read!$ method returns 01 +hen it has reached the end of a file.
QA9. Can a ,)te ob>ect be cast to a double +alue?
9o, an ob(ect cannot be cast to a primitive value.
QAA. What is the difference between a static and a nonstatic inner class?
A non0static inner class may have ob(ect instances that are associated +ith instances of the class%s
outer class. A static inner class does not have any ob(ect instances.
Q1==. What is the difference between the trin. and trin.,uffer classes?
<trin# ob(ects are constants. <trin#?uffer ob(ects are not.
Q1=1. If a +ariable is declared as pri+ate" where (a) the +ariable be accessed?
A private variable may only be accessed +ithin the class in +hich it is declared.
Q1=2. What is an ob>ectEs lock and which ob>ectEs ha+e locks?
An ob(ect%s loc" is a mechanism that is used by multiple threads to obtain synchroniHed access to
the ob(ect. A thread may execute a synchroniHed method of an ob(ect only after it has ac)uired
the ob(ect%s loc". All ob(ects and classes have loc"s. A class%s loc" is ac)uired on the class%s .lass
ob(ect.
1
Core java Interview
Questions
Q1=/. What is the *ictionar) class?
3he Gictionary class provides the capability to store "ey0value pairs.
Q1=2. Cow are the ele(ents of a ,order!a)out or.ani:ed?
3he elements of a ?orderJayout are or#aniHed at the borders !9orth, <outh, -ast, and >est$ and
the center of a container.
B1L5. >hat is the U operatorD
It is referred to as the modulo or remainder operator. It returns the remainder of dividin# the first
operand by the second operand.
Q1=6. When can an ob>ect reference be cast to an interface reference?
An ob(ect reference be cast to an interface reference +hen the ob(ect implements the referenced
interface.
Q1=8. What is the difference between a Window and a Gra(e?
3he Erame class extends >indo+ to define a main application +indo+ that can have a menu bar.
Q1=9. Which class is extended b) all other classes?
3he 'b(ect class is extended by all other classes.
Q1=A. Can an ob>ect be .arba.e collected while it is still reachable?
A reachable ob(ect cannot be #arba#e collected. 'nly unreachable ob(ects may be #arba#e
collected..
Q11=. Is the ternar) operator written x : ) ? : or x ? ) : : ?
It is +ritten x D y H.
Q111. What is the difference between the Gont and Gont'etrics classes?
3he Eont5etrics class is used to define implementation0specific properties, such as ascent and
descent, of a Eont ob(ect.
Q112. Cow is roundin. perfor(ed under inte.er di+ision?
3he fractional part of the result is truncated. 3his is "no+n as roundin# to+ard Hero.
Q11/. What happens when a thread cannot acDuire a lock on an ob>ect?
If a thread attempts to execute a synchroniHed method or synchroniHed statement and is unable to
ac)uire an ob(ect%s loc", it enters the +aitin# state until the loc" becomes available.
Q112. What is the difference between the 7eaderBWriter class hierarch) and the
Inputtrea(B#utputtrea( class hierarch)?
3he 1eader7>riter class hierarchy is character0oriented, and the Input<tream7 'utput<tream
class hierarchy is byte0oriented.
1
Core java Interview
Questions
Q11?. What classes of exceptions (a) be cau.ht b) a catch clause?
A catch clause can catch any exception that may be assi#ned to the 3hro+able type. 3his
includes the -rror and -xception types.
Q116. If a class is declared without an) access (odifiers" where (a) the class be accessed?
A class that is declared +ithout any access modifiers is said to have pac"a#e access. 3his means
that the class can only be accessed by other classes and interfaces that are defined +ithin the
same pac"a#e.
B11&. >hat is the <imple3imeVone classD
3he <imple3imeVone class provides support for a Ore#orian calendar.
Q119. What is the 'ap interface?
3he 5ap interface replaces the *GK 1.1 Gictionary class and is used associate "eys +ith values.
Q11A. *oes a class inherit the constructors of its superclass?
A class does not inherit constructors from any of its superclasses.
Q12=. Gor which state(ents does it (ake sense to use a label?
3he only statements for +hich it ma"es sense to use a label are those statements that can enclose
a brea" or continue statement.
B121. >hat is the purpose of the <ystem classD
3he purpose of the <ystem class is to provide access to system resources.
Q122. Which IextCo(ponent (ethod is used to set a IextCo(ponent to the readJonl)
state?
set-ditable!$
Q12/. Cow are the ele(ents of a Card!a)out or.ani:ed?
3he elements of a .ardJayout are stac"ed, one on top of the other, li"e a dec" of cards.
Q122. Is 44K a +alid 5a+a operator?
9o, it is not.
B125. 9ame the ei#ht primitive *ava typesD
3he ei#ht primitive types are byte, char, short, int, lon#, float, double, and boolean.
Q126. Which class should )ou use to obtain desi.n infor(ation about an ob>ect?
3he .lass class is used to obtain information about an ob(ect%s desi#n.
Q128. What is the relationship between clippin. and repaintin.?
>hen a +indo+ is repainted by the A>3 paintin# thread, it sets the clippin# re#ions to the area
of the +indo+ that re)uires repaintin#.
1
Core java Interview
Questions
Q129. Is LabcL a pri(iti+e +alue?
3he <trin# literal =abc= is not a primitive value. It is a <trin# ob(ect.
Q12A. What is the relationship between an e+entJlistener interface and an e+entJadapter
class?
An event0listener interface defines the methods that must be implemented by an event handler
for a particular "ind of event. An event adapter provides a default implementation of an event0
listener interface.
Q1/=. What restrictions are placed on the +alues of each case of a switch state(ent?
Gurin# compilation, the values of each case of a s+itch statement must evaluate to a value that
can be promoted to an int value.
Q1/1. What (odifiers (a) be used with an interface declaration?
An interface may be declared as public or abstract.
Q1/2. Is a class a subclass of itself?
A class is a subclass of itself.
Q1//. What is the hi.hestJle+el e+ent class of the e+entdele.ation (odel?
3he (ava.util.-vent'b(ect class is the hi#hest0level class in the event0dele#ation class hierarchy.
Q1/2. What e+ent results fro( the clickin. of a button?
3he Action-vent event is #enerated as the result of the clic"in# of a button.
Q1/?. Cow can a ;$I co(ponent handle its own e+ents?
A component can handle its o+n events by implementin# the re)uired event0listener interface
and addin# itself as its o+n event listener.
Q1/6. What is the difference between a while state(ent and a do state(ent?
A +hile statement chec"s at the be#innin# of a loop to see +hether the next loop iteration should
occur. A do statement chec"s at the end of a loop to see +hether the next iteration of a loop
should occur. 3he do statement +ill al+ays execute the body of a loop at least once.
B13&. 2o+ are the elements of a Orid?a#Jayout or#aniHedD
3he elements of a Orid?a#Jayout are or#aniHed accordin# to a #rid. 2o+ever, the elements are
of different siHes and may occupy more than one ro+ or column of the #rid. In addition, the ro+s
and columns may have different siHes.
Q1/9. What ad+anta.e do 5a+aEs la)out (ana.ers pro+ide o+er traditional windowin.
s)ste(s?
*ava uses layout mana#ers to lay out components in a consistent manner across all +indo+in#
platforms. <ince *ava%s layout mana#ers aren%t tied to absolute siHin# and positionin#, they are
able to accomodate platform0specific differences amon# +indo+in# systems.
1
Core java Interview
Questions
B13I. >hat is the .ollection interfaceD
3he .ollection interface provides support for the implementation of a mathematical ba# W an
unordered collection of ob(ects that may contain duplicates.
Q12=. What (odifiers can be used with a local inner class?
A local inner class may be final or abstract.
Q121. What is the difference between static and nonJstatic+ariables?
A static variable is associated +ith the class as a +hole rather than +ith specific instances of a
class. 9on0static variables ta"e on uni)ue values +ith each ob(ect instance.
Q122. What is the difference between the paint01 and repaint01 (ethods?
3he paint!$ method supports paintin# via a Oraphics ob(ect. 3he repaint!$ method is used to
cause paint!$ to be invo"ed by the A>3 paintin# thread.
Q12/. What is the purpose of the Gile class?
3he Eile class is used to create ob(ects that provide access to the files and directories of a local
file system.
Q122. Can an exception be rethrown?
Fes, an exception can be rethro+n.
Q12?. Which 'ath (ethod is used to calculate the absolute +alue of a nu(ber?
3he abs!$ method is used to calculate absolute values.
Q126. Cow does (ultithreadin. take place on a co(puter with a sin.le C&$?
3he operatin# system%s tas" scheduler allocates execution time to multiple tas"s. ?y )uic"ly
s+itchin# bet+een executin# tas"s, it creates the impression that tas"s execute se)uentially.
Q128. When does the co(piler suppl) a default constructor for a class?
3he compiler supplies a default constructor for a class if no other constructors are provided.
Q129. When is the finall) clause of a tr)JcatchJfinall) state(ent executed?
3he finally clause of the try0catch0finally statement is al+ays executed unless the thread of
execution terminates or an exception occurs +ithin the execution of the finally clause.
Q12A. Which class is the i((ediate superclass of the Container class?
.omponent
Q1?=. If a (ethod is declared as protected" where (a) the (ethod be accessed?
A protected method may only be accessed by classes or interfaces of the same pac"a#e or by
subclasses of the class in +hich it is declared.
Q1?1. Cow can the Checkbox class be used to create a radio button?
?y associatin# .hec"box ob(ects +ith a .hec"boxOroup.
1
Core java Interview
Questions
Q1?2. Which nonJ$nicode letter characters (a) be used as the first character of an
identifier?
3he non04nicode letter characters X and Y may appear as the first character of an identifier
B153. >hat restrictions are placed on method overloadin#D
3+o methods may not have the same name and ar#ument list but different return types.
Q1?2. What happens when )ou in+oke a threadEs interrupt (ethod while it is sleepin. or
waitin.?
>hen a tas"%s interrupt!$ method is executed, the tas" enters the ready state. 3he next time the
tas" enters the runnin# state, an Interrupted-xception is thro+n.
Q1??. What is castin.?
3here are t+o types of castin#, castin# bet+een primitive numeric types and castin# bet+een
ob(ect references. .astin# bet+een numeric types is used to convert lar#er values, such as double
values, to smaller values, such as byte values. .astin# bet+een ob(ect references is used to refer
to an ob(ect by a compatible class, interface, or array type reference.
Q1?6. What is the return t)pe of a pro.ra(Es (ain01 (ethod?
A pro#ram%s main!$ method has a void return type.
B15&. 9ame four .ontainer classes.
>indo+, Erame, Gialo#, EileGialo#, /anel, Applet, or <croll/ane
Q1?9. What is the difference between a Choice and a !ist?
A .hoice is displayed in a compact form that re)uires you to pull it do+n to see the list of
available choices. 'nly one item may be selected from a .hoice. A Jist may be displayed in such
a +ay that several Jist items are visible. A Jist supports the selection of one or more Jist items.
Q1?A. What class of exceptions are .enerated b) the 5a+a runJti(e s)ste(?
3he *ava runtime system #enerates 1untime-xception and -rror exceptions.
Q16=. What class allows )ou to read ob>ects directl) fro( a strea(?
3he 'b(ectInput<tream class supports the readin# of ob(ects from input streams.
Q161. What is the difference between a field +ariable and a local +ariable?
A field variable is a variable that is declared as a member of a class. A local variable is a variable
that is declared local to a method.
Q162. $nder what conditions is an ob>ectEs finali:e01 (ethod in+oked b) the .arba.e
collector?
3he #arba#e collector invo"es an ob(ect%s finaliHe!$ method +hen it detects that the ob(ect has
become unreachable.
1
Core java Interview
Questions
Q16/. Cow are this01 and super01 used with constructors?
this!$ is used to invo"e a constructor of the same class. super!$ is used to invo"e a superclass
constructor.
Q162. What is the relationship between a (ethodEs throws clause and the exceptions that
can be thrown durin. the (ethodEs execution?
A method%s thro+s clause must declare any chec"ed exceptions that are not cau#ht +ithin the
body of the method.
Q16?. What is the difference between the 5*F 1.=2 e+ent (odel and the e+entJdele.ation
(odel introduced with 5*F 1.1?
3he *GK 1.L2 event model uses an event inheritance or bubblin# approach. In this model,
components are re)uired to handle their o+n events. If they do not handle a particular event, the
event is inherited by !or bubbled up to$ the component%s container. 3he container then either
handles the event or it is bubbled up to its container and so on, until the hi#hest0level container
has been tried..In the event0dele#ation model, specific ob(ects are desi#nated as event handlers
for O4I components. 3hese ob(ects implement event0listener interfaces. 3he event0dele#ation
model is more efficient than the event0inheritance model because it eliminates the processin#
re)uired to support the bubblin# of unhandled events.
Q166. Cow is it possible for two trin. ob>ects with identical +alues not to be eDual under
the KK operator?
3he TT operator compares t+o ob(ects to determine if they are the same ob(ect in memory. It is
possible for t+o <trin# ob(ects to have the same value, but located indifferent areas of memory.
Q168. Wh) are the (ethods of the 'ath class static?
<o they can be invo"ed as if they are a mathematical code library.
Q169. What Checkbox (ethod allows )ou to tell if a Checkbox is checked?
#et<tate!$
B16I. >hat state is a thread in +hen it is executin#D
An executin# thread is in the runnin# state.
Q18=. What are the le.al operands of the instanceof operator?
3he left operand is an ob(ect reference or null value and the ri#ht operand is a class, interface, or
array type.
Q181. Cow are the ele(ents of a ;rid!a)out or.ani:ed?
3he elements of a Orid?ad layout are of e)ual siHe and are laid out usin# the s)uares of a #rid.
Q182. What an IB# filter?
An I7' filter is an ob(ect that reads from one stream and +rites to another, usually alterin# the
data in some +ay as it is passed from one stream to another.
1
Core java Interview
Questions
Q18/. If an ob>ect is .arba.e collected" can it beco(e reachable a.ain?
'nce an ob(ect is #arba#e collected, it ceases to exist. It can no lon#er become reachable a#ain.
Q182. What is the et interface?
3he <et interface provides methods for accessin# the elements of a finite mathematical set. <ets
do not allo+ duplicate elements.
Q18?. What classes of exceptions (a) be thrown b) a throw state(ent?
A thro+ statement may thro+ any expression that may be assi#ned to the 3hro+able type.
Q186. What are E and &I?
- is the base of the natural lo#arithm and /I is mathematical value pi.
B1&&. Are true and false "ey+ordsD
3he values true and false are not "ey+ords.
Q189. What is a +oid return t)pe?
A void return type indicates that a method does not return a value.
Q18A. What is the purpose of the enableE+ents01 (ethod?
3he enable-vents!$ method is used to enable an event for a particular ob(ect. 9ormally, an event
is enabled +hen a listener is added to an ob(ect for a particular event. 3he enable-vents!$ method
is used by ob(ects that handle events by overridin# their eventdispatch methods.
Q19=. What is the difference between the Gile and 7ando(AccessGile classes?
3he Eile class encapsulates the files and directories of the local file system. 3he
1andomAccessEile class provides the methods needed to directly access data contained in any
part of a file.
Q191. What happens when )ou add a double +alue to a trin.?
3he result is a <trin# ob(ect.
Q192. What is )our platfor(Es default character encodin.?
If you are runnin# *ava on -n#lish >indo+s platforms, it is probably .p1252. If you are runnin#
*ava on -n#lish <olaris platforms, it is most li"ely NN5IY1..
Q19/. Which packa.e is alwa)s i(ported b) default?
3he (ava.lan# pac"a#e is al+ays imported by default.
Q192. What interface (ust an ob>ect i(ple(ent before it can be written to a strea( as an
ob>ect?
An ob(ect must implement the <erialiHable or -xternaliHable interface before it can be +ritten to
a stream as an ob(ect.
1
Core java Interview
Questions
B1N5. 2o+ are this and super usedD
this is used to refer to the current ob(ect instance. super is used to refer to the variables and
methods of the superclass of the current ob(ect instance.
Q196. What is the purpose of .arba.e collection?
3he purpose of #arba#e collection is to identify and discard ob(ects that are no lon#er needed by
a pro#ram so that their resources may be reclaimed and reused.
Q198. What is a co(pilation unit?
A compilation unit is a *ava source code file.
Q199. What interface is extended b) AWI e+ent listeners?
All A>3 event listeners extend the (ava.util.-ventJistener interface.
B1NI. >hat restrictions are placed on method overridin#D
, 'verridden methods must have the same name, ar#ument list, and return type.
, 3he overridin# method may not limit the access of the method it overrides.
, 3he overridin# method may not thro+ any exceptions that may not be thro+nby the overridden
method.
B1IL. 2o+ can a dead thread be restartedD
A dead thread cannot be restarted.
Q1A1. What happens if an exception is not cau.ht?
An uncau#ht exception results in the uncau#ht-xception!$ method of the thread%s
3hreadOroup bein# invo"ed, +hich eventually results in the termination of the pro#ram in +hich
it is thro+n.
Q1A2. What is a la)out (ana.er?
A layout mana#er is an ob(ect that is used to or#aniHe components in a container.
Q1A/. Which arith(etic operations can result in the throwin. of an Arith(eticException?
Inte#er 7 and U can result in the thro+in# of an Arithmetic-xception.
Q1A2. What are three wa)s in which a thread can enter the waitin. state?
A thread can enter the +aitin# state by invo"in# its sleep!$ method, by bloc"in# on I7', by
unsuccessfully attemptin# to ac)uire an ob(ect%s loc", or by invo"in# an ob(ect%s +ait!$ method. It
can also enter the +aitin# state by invo"in# its !deprecated$ suspend!$ method.
Q1A?. Can an abstract class be final?
An abstract class may not be declared as final.
Q1A6. What is the 7esource,undle class?
3he 1esource?undle class is used to store locale0specific resources that can be loaded by a
pro#ram to tailor the pro#ram%s appearance to the particular locale in +hich it is bein# run.
1
Core java Interview
Questions
Q1A8. What happens if a tr)JcatchJfinall) state(ent doesnot ha+e a catch clause to handle
an exception that is
thrown within the bod) of the tr) state(ent?
3he exception propa#ates up to the next hi#her level try0catch statement !if any$ or results in the
pro#ram%s termination.
Q1A9. What is nu(eric pro(otion?
9umeric promotion is the conversion of a smaller numeric type to a lar#er numeric type, so that
inte#er and floatin#0point operations may ta"e place. In numerical promotion, byte, char, and
short values are converted to int values. 3he int values are also converted to lon# values, if
necessary. 3he lon# and float values are converted to double values, as re)uired.
Q1AA. What is the difference between a crollbar and a croll&ane?
A <crollbar is a .omponent, but not a .ontainer. A <croll/ane is a .ontainer. A <croll/ane
handles its o+n events and performs its o+n scrollin#.
Q2==. What is the difference between a public and a nonpublic class?
A public class may be accessed outside of its pac"a#e. A non0public class may not be accessed
outside of its pac"a#e.
Q2=1. Io what +alue is a +ariable of the boolean t)pe auto(aticall) initiali:ed?
3he default value of the boolean type is false.
Q2=2. Can tr) state(ents be nested?
3ry statements may be tested.
Q2=/. What is the difference between the prefix and postfix for(s of the 33 operator?
3he prefix form performs the increment operation and returns the value of the increment
operation. 3he postfix form returns the current value all of the expression and then performs the
increment operation on that value.
Q2=2. What is the purpose of a state(ent block?
A statement bloc" is used to or#aniHe a se)uence of statements as a sin#le statement #roup.
B2L5. >hat is a *ava pac"a#e and ho+ is it usedD
A *ava pac"a#e is a namin# context for classes and interfaces. A pac"a#e is used to create a
separate name space for #roups of classes and interfaces. /ac"a#es are also used to or#aniHe
related classes and interfaces into a sin#le A/I unit and to control accessibility to these classes
and interfaces.
Q2=6. What (odifiers (a) be used with a topJle+el class?
A top0level class may be public, abstract, or final.
1
Core java Interview
Questions
Q2=8. What are the #b>ect and Class classes used for?
3he 'b(ect class is the hi#hest0level class in the *ava class hierarchy. 3he .lass class is used to
represent the classes and interfaces that are loaded by a *ava pro#ram..
Q2=9. Cow does a tr) state(ent deter(ine which catch clause should be used to handle an
exception?
>hen an exception is thro+n +ithin the body of a try statement, the catch clauses of the try
statement are examined in the order in +hich they appear. 3he first catch clause that is capable of
handlin# the exception is executed. 3he remainin# catch clauses are i#nored.
B2LI. .an an unreachable ob(ect become reachable a#ainD
An unreachable ob(ect may become reachable a#ain. 3his can happen +hen the ob(ect%s finaliHe!$
method is invo"ed and the ob(ect performs an operation +hich causes it to become accessible to
reachable ob(ects.
Q21=. When is an ob>ect sub>ect to .arba.e collection?
An ob(ect is sub(ect to #arba#e collection +hen it becomes unreachable to the pro#ram in +hich
it is used.
Q211. What (ethod (ust be i(ple(ented b) all threads?
All tas"s must implement the run!$ method, +hether they are a subclass of 3hread or implement
the 1unnable interface.
Q212. What (ethods are used to .et and set the text label displa)ed b) a ,utton ob>ect?
#etJabel!$ and setJabel!$
Q21/. Which Co(ponent subclass is used for drawin. and paintin.?
.anvas
Q212. What are s)nchroni:ed (ethods and s)nchroni:ed state(ents?
<ynchroniHed methods are methods that are used to control access to an ob(ect. A thread only
executes a synchroniHed method after it has ac)uired the loc" for the method%s ob(ect or class.
<ynchroniHed statements are similar to synchroniHed methods. A synchroniHed statement can
only be executed after a thread has ac)uired the loc" for the ob(ect or class referenced in the
synchroniHed statement.
Q21?. What are the two basic wa)s in which classes that can be run as threads (a) be
defined?
A thread class may be declared as a subclass of 3hread, or it may implement the 1unnable
interface.
Q216. What are the proble(s faced b) 5a+a pro.ra((ers who donEt use la)out (ana.ers?
>ithout layout mana#ers, *ava pro#rammers are faced +ith determinin# ho+ their O4I +ill be
displayed across multiple +indo+in# systems and findin# a common siHin# and positionin# that
+ill +or" +ithin the constraints imposed by each +indo+in# system.
1
Core java Interview
Questions
Q218. What is the difference between an if state(ent and a switch state(ent?
3he if statement is used to select amon# t+o alternatives. It uses a boolean expression to decide
+hich alternative should be executed. 3he s+itch statement is used to select amon# multiple
alternatives. It uses an int expression to determine +hich alternative should be executed.
Q219. What happens when )ou add a double +alue to a trin.?
3he result is a <trin# ob(ect.
Q21A. What is the !ist interface?
3he Jist interface provides support for ordered collections of ob(ects.

Вам также может понравиться