| These are some
of the Java Interview Questions asked in Interviews |
Q: What is the difference
between an Interface and an Abstract class?
A: An abstract class can have instance methods
that implement a default behavior. An Interface
can only declare constants and instance methods,
but cannot implement default behavior and
all methods are implicitly abstract. An interface
has all public members and no implementation.
An abstract class is a class which may have
the usual flavors of class members (private,
protected, etc.), but has some abstract methods.
Q: Describe synchronization in respect to
multithreading.
A: With respect to multithreading, synchronization
is the capability to control the access of
multiple threads to shared resources. Without
synchonization, it is possible for one thread
to modify a shared variable while another
thread is in the process of using or updating
same shared variable. This usually leads to
significant errors.
Q: What is the purpose of garbage collection
in Java, and when is it used?
A: The purpose of garbage collection is to
identify and discard objects that are no longer
needed by a program so that their resources
can be reclaimed and reused. A Java object
is subject to garbage collection when it becomes
unreachable to the program in which it is
used.
Q: What is HashMap and Map?
A: Map is Interface and Hashmap is class that
implements that.
Q: Primitive data types are passed by reference
or pass by value?
A: Primitive data types are passed by value.
Q: What type of parameter passing does Java
support?
A: In Java the arguments are always passed
by value .
Q: Difference between Vector and ArrayList?
A: Vector is synchronized whereas arraylist
is not.
Q: What is the difference between a constructor
and a method?
A: A constructor is a member function of a
class that is used to create objects of that
class. It has the same name as the class itself,
has no return type, and is invoked using the
new operator.
A method is an ordinary member function of
a class. It has its own name, a return type
(which may be void), and is invoked using
the dot operator.
Q: What is the default value of an object
reference declared as an instance variable?
A: null unless we define it explicitly.
Q: Difference between HashMap and HashTable?
A: The HashMap class is roughly equivalent
to Hashtable, except that it is unsynchronized
and permits nulls. (HashMap allows null values
as key and value whereas Hashtable doesnt
allow). HashMap does not guarantee that the
order of the map will remain constant over
time. HashMap is non synchronized and Hashtable
is synchronized.
Q: What is an Iterators?
A: Some of the collection classes provide
traversal of their contents via a java.util.Iterator
interface. This interface allows you to walk
a collection of objects, operating on each
object in turn. Remember when using Iterators
that they contain a snapshot of the collection
at the time the Iterator was obtained; generally
it is not advisable to modify the collection
itself while traversing an Iterator.
Q: What is an abstract class?
A: Abstract class must be extended/subclassed
(to be useful). It serves as a template. A
class that is abstract may not be instantiated
(ie, you may not call its constructor), abstract
class may contain static data. Any class with
an abstract method is automatically abstract
itself, and must be declared as such.
A class may be declared abstract even if it
has no abstract methods. This prevents it
from being instantiated.
Q: What is final?
A: A final class can't be extended ie., final
class may not be subclassed. A final method
can't be overridden when its class is inherited.
You can't change value of a final variable
(is a constant).
Q: Difference between Swing and Awt?
A: AWT are heavy-weight componenets. Swings
are light-weight components. Hence swing works
faster than AWT.
Q: What if the static modifier is removed
from the signature of the main method?
A: Program compiles. But at runtime throws
an error "NoSuchMethodError".
Q: What if I do not provide the String array
as the argument to the method?
A: Program compiles but throws a runtime error
"NoSuchMethodError".
Q: What is Overriding?
A: When a class defines a method using the
same name, return type, and arguments as a
method in its superclass, the method in the
class overrides the method in the superclass.
When the method is invoked for an object of
the class, it is the new definition of the
method that is called, and not the method
definition from superclass. Methods may be
overridden to be more public, not more private.
Q: Does importing a package imports the subpackages
as well? e.g. Does importing com.simpleTest.*
also import com.simpleTest.UnitTests.*?
A: No you will have to import the subpackages
explicitly. Importing com.simpleTest.* will
import classes in the package simpleTest only.
It will not import any class in any of it's
subpackage. |
| |