1.
What is the difference between a constructor and a
method?
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.
2. What
is Polymorphism?
The Polymorphism can be referred as one name many forms. It is the
ability of methods to behave differently, depending upon the object who is
calling it. The key features of Polymorphism are:
Allows using one interface for multiple implementations.
Supports Method Overloading: Multiple methods with same name, but
different formal argument.
Supports Method Overridden: Multiple methods have the same name, same
return type, and same formal argument list.
3. Explain
garbage collection.
The Java uses the garbage collection to free the memory. By cleaning
those objects that is no longer reference by any of the program. Step involve
in cleaning up the garbage collection:
Garbage Object Collection: first step is to collection and group all
those object which are no more reference with any of the program. We can use
the different methods to collect the garbage object like using runtime.gc() or
system.gc().
Run Finalize method: To free up those object which is collected by the
garbage collector java must execute the Finalize method to delete all those
dynamically created object
4. What
is an immutable object?
An immutable object is one that we cannot change once it is created.
Steps involved in creation of an immutable object are:
Make all of its data fields private.
Methods which can perform changes in any of the data fields after the
construction of object must be avoided.
5. How
are this() and super() used with constructors?
this() Constructors: is used to pointing the current class instance.
Can be used with variables or methods.
Used to call constructer of same class.
Private variable cannot be accessed using this().
super() Constructer: is used to call constructor of parent class.
Must be the first statement in the body of constructor.
Using this we can access private variables in the super class.
6. What
are Access Specifiers available in Java?
Java
offers four access specifiers, described below:
Public: public classes, methods, and fields can
be accessed by every class.
Protected: protected methods and fields can only
be accessed within the same class to which the methods and fields belong.
Default
(no specifier): when
we do not set access to specific level, then such a class, method, or field
will be accessible from inside the same package to which the class, method, or
field belongs.
Private: private methods and fields can only be
accessed within the same class to which the methods and fields belong. Private
methods and fields are not inherited by subclasses.
7. What
is Constructor?
A
constructor is used to initialize a newly created object.
It
is called just after the memory is allocated for the object.
It
can be used to initialize the objects.
It
is not mandatory to write a constructor for the class.
Name
of constructor is same as the class name.
Cannot
be inherited.
Constructor
is invoked whenever an object of its associated class is created.
8. What
are the List interface and its main implementation?
The
List helps in collections of objects. Lists may contain duplicate elements. The
main implementations of the List interface are as follows:
ArrayList: Resizable-array implementation of the
List interface.
Vector: Synchronized resizable-array
implementation of the List.
LinkedList: Doubly-linked list implementation of
the List interface. Better performance than the ArrayList implementation when
elements are inserted or deleted timely.
9. Explain
the user defined Exceptions.
User
Defined Exceptions are exceptions defined by the user for specific purposed.
This allows custom exceptions to be generated and caught in the same way as
normal exceptions. While defining a User Defined Exception, we need to take
care of the following aspects:
It
should be extend from Exception class.
Use
toString() method to display information about the exception.
10. Describe
life cycle of thread.
Threads
follow the single flow of control. A thread is execution in a program. The life
cycles of threads are listed below:
Newborn
state: After the creations
of Thread instance the thread is in this state but before the start() method
invocation. Thread is considered not alive in this phase.
Runnable
state: A thread starts its
life from Runnable state. After the invoking of start() method thread enters
Runnable state.
Running
state: A thread first enters
Runnable state.
Blocked
state: A thread can enter in
this state because of waiting the resources that are hold by another thread.
Dead
state: A thread can be
considered dead when its run() method completes. If any thread comes on this
state that means it cannot ever run again.
11. What
is an Applets?
Applets: These are small java programs.
They
can send from one computer to another computer over the internet using the
Applet Viewer that supports java.
Applets
can runs in a Web browser as it is a java program. It can be a fully functional
Java application because it has the entire Java API at its disposal.
Applets
follow the security rules given by the Web browser.
Applet
security is also known as sandbox security.
12. What
is the Set interface?
A
Set interface is collection of element which cannot be duplicated.
The
Set interface contains methods inherited from collection.
It
provides methods to access the elements of a finite mathematical set.
Two
Set objects are equal if they contain the same elements.
It
models the mathematical set abstraction.
13. What
is a HashSet and TreeSet?
The
HashSet is an unsorted, unordered Set.
It
is Collection set that restrict duplicate elements and also repositioning of
elements.
It
implements the Set interface and extends AbstractSet.
Uses
hash code of the object being inserted.
The TreeSet is a Set implemented when we want elements in a sorted order.
Sorting
of element is done according to the natural order of elements or by the help of
comparator provided at creation time.
14. What is the purpose
of garbage collection in Java, and when is it used?
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.
15. Describe
synchronization in respect to multithreading.
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.
16. What is an abstract
class?
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.
17. 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.
18. What is the
difference between an Interface and an Abstract class?
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.
19. Explain different way
of using thread?
The thread could be implemented by using runnable interface or by
inheriting from the Thread class. The former is more
advantageous, 'cause when you are going for multiple inheritance, the only
interface can help.
20. When should I use abstract classes and when
should I use interfaces?
Use
Interface, when:
- Design changing frequently or
when various implementations only share method signatures.
- We need some classes to use
some methods which we do not want to be included in the class.
Use
Abstract Class, when:
- Various implementations are of
the same kind and use common behavior.
- Enabling with generalized form
of abstraction and leave the implementation task with the inheriting
subclass.
- creating planned inheritance
hierarchies
No comments:
Post a Comment