You are on page 1of 5

Core Java Interview Question Page 1

Posted on: March 26, 2008 at 12:00 AM


Core Java Interview Question Page 1
Question: How could Java classes direct program messages to the system console,
but error messages, say to a file
!nswer: The cass !"ste# has a varia$e out that re%resents the standard out%ut, and the
varia$e err that re%resents the standard error device& '" de(aut, the" $oth %oint at the
s"ste# consoe& This how the standard out%ut coud $e re)directed:
!trea# st * new !trea#+new ,ie-ut%ut!trea#+.out%ut&t/t.001
!"ste#&set2rr+st01
!"ste#&set-ut+st01
Question: "hat#s the difference between an interface and an abstract class
!nswer: An a$stract cass #a" contain code in #ethod $odies, which is not aowed in
an inter(ace& 3ith a$stract casses, "ou have to inherit "our cass (ro# it and Java does
not aow #uti%e inheritance& -n the other hand, "ou can i#%e#ent #uti%e inter(aces
in "our cass&
Question: "hy would you use a synchroni$ed bloc% vs& synchroni$ed method
!nswer: !"nchroni4ed $oc5s %ace oc5s (or shorter %eriods than s"nchroni4ed
#ethods&
Question: '(plain the usage of the %eyword transient
!nswer: This 5e"word indicates that the vaue o( this #e#$er varia$e does not have to
$e seriai4ed with the o$6ect& 3hen the cass wi $e de)seriai4ed, this varia$e wi $e
initiai4ed with a de(aut vaue o( its data t"%e +i&e& 4ero (or integers0&
Question: How can you force garbage collection
!nswer: 7ou can8t (orce 9C, $ut coud re:uest it $" caing !"ste#&gc+0& J;M does not
guarantee that 9C wi $e started i##ediate"&
Question: How do you %now if an e(plicit ob)ect casting is needed
!nswer: I( "ou assign a su%ercass o$6ect to a varia$e o( a su$cass8s data t"%e, "ou need
to do e/%icit casting& ,or e/a#%e:
-$6ect a1
Custo#er $1
$ * +Custo#er0 a1
3hen "ou assign a su$cass to a varia$e having a su%ecass t"%e, the casting is
%er(or#ed auto#atica"&
Question: "hat#s the difference between the methods sleep*+ and wait*+
!nswer: The code see%+100001 %uts thread aside (or e/act" one second& The code
wait+10000, causes a wait o( u% to one second& A thread coud sto% waiting earier i( it
receives the noti("+0 or noti("A+0 ca& The #ethod wait+0 is de(ined in the cass -$6ect
and the #ethod see%+0 is de(ined in the cass Thread&
Question: Can you write a Java class that could be used both as an applet as well as
an application
!nswer: 7es& Add a #ain+0 #ethod to the a%%et&
Question: "hat#s the difference between constructors and other methods
!nswer: Constructors #ust have the sa#e na#e as the cass and can not return a vaue&
The" are on" caed once whie reguar #ethods coud $e caed #an" ti#es&
Question: Can you call one constructor from another if a class has multiple
constructors
Answer: 7es& <se this+0 s"nta/&
Question: '(plain the usage of Java pac%ages&
Answer: This is a wa" to organi4e (ies when a %ro6ect consists o( #uti%e #odues& It
aso he%s resove na#ing con(icts when di((erent %ac5ages have casses with the sa#e
na#es& Pac5ages access eve aso aows "ou to %rotect data (ro# $eing used $" the non)
authori4ed casses&
Question: If a class is located in a pac%age, what do you need to change in the ,-
environment to be able to use it
!nswer: 7ou need to add a director" or a 6ar (ie that contains the %ac5age directories to
the C=A!!PAT> environ#ent varia$e& =et8s sa" a cass 2#%o"ee $eongs to a %ac5age
co#&/"4&hr1 and is ocated in the (ie c:?dev?co#?/"4?hr?2#%o"ee&6ava& In this case,
"ou8d need to add c:?dev to the varia$e C=A!!PAT>& I( this cass contains the #ethod
#ain+0, "ou coud test it (ro# a co##and %ro#%t window as (oows:
c:?@6ava co#&/"4&hr&2#%o"ee
Question: "hat#s the difference between J.-/0 1&1 and J.-/0 1&2
!nswer: There8s no di((erence, !un Micros"ste#s 6ust re)$randed this version&
Question: 3hat woud "ou use to co#%are two !tring varia$es ) the o%erator ** or the
#ethod e:uas+0A
!nswer: I8d use the #ethod e:uas+0 to co#%are the vaues o( the !trings and the ** to
chec5 i( two varia$es %oint at the sa#e instance o( a !tring o$6ect&
Core Java Interview Question, Interview Question
Posted on: A%ri 18, 2011 at 12:00 AM
3his page discusses 4 Core Java Interview Question, Interview Question
Core Java Interview Question Page .

Question: /oes it matter in what order catch statements for 5ile6ot5ound'(ception
and I,'(ceptipon are written
!nswer: 7es, it does& The ,ieBo,ound2/ce%tion is inherited (ro# the I-2/ce%tion&
2/ce%tion8s su$casses have to $e caught (irst&
Question: Can an inner class declared inside of a method access local variables of
this method
!nswer: It8s %ossi$e i( these varia$es are (ina&
Question: "hat can go wrong if you replace 77 with 7 in the following code:
-tring a8null9
if *a:8null 77 a&length*+;12+ <&&&=
!nswer: A singe a#%ersand here woud ead to a BuPointer2/ce%tion&
Question: "hat#s the main difference between a >ector and an !rray?ist
!nswer: Java ;ector cass is interna" s"nchroni4ed and Arra"=ist is not&
Question: "hen should the method invo%e?ater*+be used
!nswer: This #ethod is used to ensure that !wing co#%onents are u%dated through the
event)dis%atching thread&
Question: How can a subclass call a method or a constructor defined in a
superclass
!nswer: <se the (oowing s"nta/: su%er&#"Method+01 To ca a constructor o( the
su%ercass, 6ust write su%er+01 in the (irst ine o( the su$cass8s constructor&
5or senior4level developers:
Question: "hat#s the difference between a @ueue and a stac%
!nswer: !tac5s wor5s $" ast)in)(irst)out rue +=I,-0, whie :ueues use the ,I,- rue
Question: Aou can create an abstract class that contains only abstract methods& ,n
the other hand, you can create an interface that declares the same methods& -o can
you use abstract classes instead of interfaces
!nswer: !o#eti#es& 'ut "our cass #a" $e a descendent o( another cass and in this case
the inter(ace is "our on" o%tion&
Question: "hat comes to mind when you hear about a young generation in Java
!nswer: 9ar$age coection&
Question: "hat comes to mind when someone mentions a shallow copy in Java
!nswer: -$6ect coning&
Question: If you#re overriding the method e@uals*+ of an ob)ect, which other method
you might also consider
!nswer: hashCode+0
Question: Aou are planning to do an inde(ed search in a list of ob)ects& "hich of the
two Java collections should you use: !rray?ist or ?in%ed?ist
!nswer: Arra"=ist
Question: How would you ma%e a copy of an entire Java ob)ect with its state
!nswer: >ave this cass i#%e#ent Conea$e inter(ace and ca its #ethod cone+0&
Question: How can you minimi$e the need of garbage collection and ma%e the
memory use more effective
!nswer: <se o$6ect %ooing and wea5 o$6ect re(erences&
Question: 3here are two classes: ! and B& 3he class B need to inform a class ! when
some important event has happened& "hat Java techni@ue would you use to
implement it
!nswer: I( these casses are threads I8d consider noti("+0 or noti("A+0& ,or reguar
casses "ou can use the -$server inter(ace&
Question: "hat access level do you need to specify in the class declaration to ensure
that only classes from the same directory can access it
!nswer: 7ou do not need to s%eci(" an" access eve, and Java wi use a de(aut %ac5age
access eve &
6ow lets see what a servlet container does for each H33P re@uest for a servlet, in
general :
The servet container oads the servlet class and calls the init method of the
servlet as soon as the servet is caed (or the (irst ti#e&
Then this container #a5es an instance o( )ava(&servlet&-ervletCe@uest and
)ava(&servlet&-ervletCesponse (or each re:uest&
Then it %asses the -ervletCe@uest and -ervletCesponse o$6ects $" invo5ing the
servet8s service method&
,ina", it cas the destroy method and unoad the servet cass when the servet
cass is to $e shut down&

You might also like