Sie sind auf Seite 1von 3

Java Technical Questions With Answers

Q1. How could Java classes direct program messages to the system console, but
error messages, say to a file
A. The class System has a variable out that represents the standard output, and the
variable err that represents the standard error device. By default, they both point at the
system console. This how the standard output could be re-directed:
Stream st = new Stream(new ile!utputStream("output.t#t"$$% System.set&rr(st$%
System.set!ut(st$%
Q!. What"s the difference between an interface and an abstract class
A. An abstract class may contain code in method bodies, which is not allowed in an
interface. 'ith abstract classes, you have to inherit your class from it and (ava does not
allow multiple inheritance. !n the other hand, you can implement multiple interfaces in your
class.
Q#. Why would you use a synchroni$ed bloc% vs. synchroni$ed method
A. Synchroni)ed bloc*s place loc*s for shorter periods than synchroni)ed methods.
Q&. '(plain the usage of the %eyword transient
A. This *eyword indicates that the value of this member variable does not have to be
seriali)ed with the ob+ect. 'hen the class will be de-seriali)ed, this variable will be initiali)ed
with a default value of its data type (i.e. )ero for inte,ers$.
Q). How can you force garbage collection
A. -ou can.t force /0, but could re1uest it by callin, System.,c($. (23 does not ,uarantee
that /0 will be started immediately.
Q*. How do you %now if an e(plicit ob+ect casting is needed
A. 4f you assi,n a superclass ob+ect to a variable of a subclass.s data type, you need to do
e#plicit castin,. or e#ample:
!b+ect a% 0ustomer b% b = (0ustomer$ a%
'hen you assi,n a subclass to a variable havin, a supeclass type, the castin, is performed
automatically.
Q,. What"s the difference between the methods sleep-. and wait-.
A. The code sleep(5666$% puts thread aside for e#actly one second. The code wait(5666$,
causes a wait of up to one second. A thread could stop waitin, earlier if it receives the
notify($ or notifyAll($ call. The method wait($ is defined in the class !b+ect and the method
sleep($ is defined in the class Thread.
Q/. 0an you write a Java class that could be used both as an applet as well as an
application
A. -es. Add a main($ method to the applet.
Q1. What"s the difference between constructors and other methods
A. 0onstructors must have the same name as the class and can not return a value. They are
only called once while re,ular methods could be called many times.
Q12. 0an you call one constructor from another if a class has multiple
constructors
A. -es. 7se this($ synta#.
Q11. '(plain the usage of Java pac%ages.
A. This is a way to or,ani)e files when a pro+ect consists of multiple modules. 4t also helps
resolve namin, conflicts when different pac*a,es have classes with the same names.
8ac*a,es access level also allows you to protect data from bein, used by the nonauthori)ed
classes.
Q1!. 3f a class is located in a pac%age, what do you need to change in the 45
environment to be able to use it
A. -ou need to add a directory or a +ar file that contains the pac*a,e directories to the
09ASS8AT: environment variable. 9et.s say a class &mployee belon,s to a pac*a,e
com.#y).hr% and is located in the file c:;dev;com;#y);hr;&mployee.+ava. 4n this case, you.d
need to add c:;dev to the variable 09ASS8AT:. 4f this class contains the method main($,
you could test it from a command prompt window as follows:
c:;<+ava com.#y).hr.&mployee
Q1#. What"s the difference between J!567 1.) and J!567 ).2
A.There.s no difference, Sun 3icrosystems +ust re-branded this version.
Q1&. What would you use to compare two 5tring variables 8 the operator 99 or
the method e:uals-.
A. 4.d use the method e1uals($ to compare the values of the Strin,s and the == to chec* if
two variables point at the same instance of a Strin, ob+ect.
Q1). 6oes it matter in what order catch statements for ;ile<ot;ound'(ception
and 34'(ceptipon are written
A. -es, it does. The ile=oound&#ception is inherited from the 4!&#ception. &#ception.s
subclasses have to be cau,ht first.
Q1*. 0an an inner class declared inside of a method access local variables of this
method
A. 4t.s possible if these variables are final.
Q1,. What can go wrong if you replace == with = in the following code>
Strin, a=null% if (a>=null ?? a.len,th($<56$ @...A
A. A sin,le ampersand here would lead to a =ull8ointer&#ception.
Q1/. What"s the main difference between a ?ector and an Array@ist
A. (ava 2ector class is internally synchroni)ed and Array9ist is not.
Q11. When should the method invo%e@ater-.be used
A. This method is used to ensure that Swin, components are updated throu,h the eventdispatchin,
thread.
Q!2. How can a subclass call a method or a constructor defined in a superclass
A. 7se the followin, synta#: super.my3ethod($% To call a constructor of the superclass, +ust
write super($% in the first line of the subclass.s constructor
or senior-level developers:
Q!1. What"s the difference between a :ueue and a stac%
A. Stac*s wor*s by last-in-first-out rule (94!$, while 1ueues use the 4! rule
Q!!. Aou can create an abstract class that contains only abstract methods. 4n
the other hand, you can create an interface that declares the same methods. 5o
can you use abstract classes instead of interfaces
A. Sometimes. But your class may be a descendent of another class and in this case the
interface is your only option.
Q!#. What comes to mind when you hear about a young generation in Java
A. /arba,e collection.
Q!&. What comes to mind when someone mentions a shallow copy in Java
A. !b+ect clonin,.
Q!). 3f you"re overriding the method e:uals-. of an ob+ect, which other method
you might also consider
A. hash0ode($
Q!*. Aou are planning to do an inde(ed search in a list of ob+ects. Which of the
two Java collections should you use>
Array@ist or @in%ed@ist
A. Array9ist
Q!,. How would you ma%e a copy of an entire Java ob+ect with its state
A. :ave this class implement 0loneable interface and call its method clone($.
Q!/. How can you minimi$e the need of garbage collection and ma%e the
memory use more effective
A. 7se ob+ect poolin, and wea* ob+ect references.
Q!1. There are two classes> A and B. The class B need to inform a class A when
some important event has happened. What Java techni:ue would you use to
implement it
A. 4f these classes are threads 4.d consider notify($ or notifyAll($. or re,ular classes you can
use the !bserver interface.
Q#2. What access level do you need to specify in the class declaration to ensure
that only classes from the same directory can access it
A. -ou do not need to specify any access level, and (ava will use a default pac*a,e access
level.

Das könnte Ihnen auch gefallen