Java Interview Questions and Answers
What is the difference between final,
finally and finalize?
final - declares constant
finally - relates with exception handling
finalize - helps in garbage collection
If asked to give details, explain:
final field, final method, final class
protected void finalize() in Object class
What kind of security tools are available in J2SE 5.0?
There are three tools that can be used to protect application working
within the scope of security policies set at remote sites.
keytool -- used to manage keystores and certificates.
jarsigner -- used to generate and verify JAR signatures.
policytool -- used for managing policy files.
There are three tools that help obtain, list and manage Kerberos
kinit -- used to obtain Kerberos V5 tickets.
tklist -- used to list entries in credential cache and key tab.
ktab -- used to help manage entries in the key table.
How to make an array copy from System?
There is a method called arraycopy in the System class. You can do it:
System.arraycopy(sourceArray, srcOffset, destinationArray, destOffset,
When you use this method, the destinationArray will be filled with the
elements of sourceArray at the length specified.
Can we use System.arraycopy() method to copy the same array?
Yes, you can. The source and destination arrays can be the same if you
want to copy a subset of the array to another area within that array.
What is shallow copy or shallow clone in array cloning?
Cloning an array invloves creating a new array of the same size and type
and copying all the old elements into the new array. But such copy is
called shallow copy or shallow clone because any changes to the object
would be reflected in both arrays.
When is the ArrayStoreException thrown?
When copying elements between different arrays, if the source or
destination arguments are not arrays or their types are not compatible,
an ArrayStoreException will be thrown.
How to check two arrays to see if contents have the same types and
contain the same elements?
One of options is to use the equals() method of Arrays class.
If the array types are different, a compile-time error will happen.
Can you call one constructor from another if a class has multiple
Yes. Use this() syntax.
What are the different types of inner classes?
There are four different types of inner classes in Java. They are:
a)Static member classes , a static member class has access to all static
methods of the parent, or top-level, class b) Member classes, the member
class is instance specific and has access to any and all methods and
members, even the parent's this reference c) Local classes, are declared
within a block of code and are visible only within that block, just as
any other method variable. d) Anonymous classes, is a local class that
has no name
In which case would you choose a static inner class?
Interesting one, static inner classes can access the outer class's
protected and private fields. This is both a positive and a negative
point for us since we can, in essence, violate the encapsulation of the
outer class by mucking up the outer class's protected and private
fields. The only proper use of that capability is to write white-box
tests of the class -- since we can induce cases that might be very hard
to induce via normal black-box tests (which don't have access to the
internal state of the object). Second advantage,if I can say, is that,
we can this static concept to impose restriction on the inner class.
Again as discussed in earlier point, an Inner class has access to all
the public, private and protected members of the parent class. Suppose
you want to restrict the access even to inner class, how would you go
ahead? Making the inner class static enforces it to access only the
public static members of the outer class( Since, protected and private
members are not supposed to be static and that static members can access
only other static members). If it has to access any non-static member,
it has to create an instance of the outer class which leads to accessing
only public members.
What is weak reference in Java
A weak reference is one that does not prevent the referenced object from
being garbage collected. You might use them to manage a HashMap to look
up a cache of objects. A weak reference is a reference that does not
keep the object it refers to alive. A weak reference is not counted as a
reference in garbage collection. If the object is not referred to
elsewhere as well, it will be garbage collected.
Page Numbers :