Java Interview Questions and Answers
|
|
-
What is Garbage
Collection and how to call it explicitly?-
When an object is no longer referred to by any variable, java automatically
reclaims memory used by that object. This is known as garbage collection.
System. gc() method may be used to call it explicitly.
-
What is finalize()
method?-
finalize () method is used just before an object is destroyed and can be called
just prior to garbage collection.
-
What are Transient and
Volatile Modifiers?-
Transient: The transient modifier applies to variables only and it is not
stored as part of its object’s Persistent state. Transient variables are not
serialized. Volatile: Volatile modifier applies to variables only and it tells
the compiler that the variable modified by volatile can be changed unexpectedly
by other parts of the program.
|
|
|
|
|
-
What is method
overloading and method overriding?-
Method overloading: When a method in a class having the same method name with
different arguments is said to be method overloading. Method overriding : When
a method in a class having the same method name with same arguments is said to
be method overriding.
-
What is difference
between overloading and overriding?-
a) In overloading, there is a relationship between methods available in the
same class whereas in overriding, there is relationship between a superclass
method and subclass method. b) Overloading does not block inheritance from the
superclass whereas overriding blocks inheritance from the superclass. c) In
overloading, separate methods share the same name whereas in overriding,
subclass method replaces the superclass. d) Overloading must have different
method signatures whereas overriding must have same signature.
-
What is meant by
Inheritance and what are its advantages?-
Inheritance is the process of inheriting all the features from a class. The
advantages of inheritance are reusability of code and accessibility of
variables and methods of the super class by subclasses.
|
|
|
|
|
-
What is the difference
between this() and super()?-
this() can be used to invoke a constructor of the same class whereas super()
can be used to invoke a super class constructor.
-
What is the difference
between superclass and subclass?-
A super class is a class that is inherited whereas sub class is a class that
does the inheriting.
-
What modifiers may be
used with top-level class?-
public, abstract and final can be used for top-level class.
|
|
|
|
|
-
What are inner class and
anonymous class?-
Inner class : classes defined in other classes, including those defined in
methods are called inner classes. An inner class can have any accessibility
including private. Anonymous class : Anonymous class is a class defined inside
a method without a name and is instantiated and declared in the same place and
cannot have explicit constructors.
-
What is a package?-
A package is a collection of classes and interfaces that provides a high-level
layer of access protection and name space management.
-
What is a reflection
package?-
java. lang. reflect package has the ability to analyze itself in runtime.
|
|
|
|
|
-
What is interface and
its use?-
Interface is similar to a class which may contain method’s signature only but
not bodies and it is a formal set of method and constant declarations that must
be defined by the class that implements it. Interfaces are useful for:
a)Declaring methods that one or more classes are expected to implement
b)Capturing similarities between unrelated classes without forcing a class
relationship. c)Determining an object’s programming interface without revealing
the actual body of the class.
-
What is an abstract
class?-
An abstract class is a class designed with implementation gaps for subclasses
to fill in and is deliberately incomplete.
-
What is the difference
between Integer and int?-
a) Integer is a class defined in the java. lang package, whereas int is a
primitive data type defined in the Java language itself. Java does not
automatically convert from one to the other. b) Integer can be used as an
argument for a method that requires an object, whereas int can be used for
calculations.
|
|
|
|
|
|