Professional Documents
Culture Documents
Java Interview Questions
Java Interview Questions
Q: What is Abstraction?
A: It refers to the ability to make a class
abstract in OOP. It helps to reduce the
complexity and also improves the
maintainability of the system.
Q: What is Abstract class
A: These classes cannot be instantiated
and are either partially implemented or
not at all implemented. This class
contains one or more abstract methods
which are simply method declarations
without a body.
Q: When Abstract methods are
used?
A: If you want a class to contain a
particular method but you want the
actual implementation of that method
to be determined by child classes, you
can declare the method in the parent
class as abstract.
Q: What is Encapsulation?
A: It is the technique of making the
fields in a class private and providing
access to the fields via public methods.
If a field is declared private, it cannot be
accessed by anyone outside the class,
thereby hiding the fields within the
class. Therefore encapsulation is also
referred to as data hiding.
Q: What is the primary benefit of
Encapsulation?
A: The main benefit of encapsulation is
the ability to modify our implemented
code without breaking the code of
others who use our code. With this
Encapsulation
gives maintainability,
flexibility and extensibility to our code.
Q: What is an Interface?
A: An interface is a collection of
abstract methods. A class implements
an interface, thereby inheriting the
abstract methods of the interface.
Q: Give some features of Interface?
A: It includes:
Interface cannot be instantiated
An interface does not contain any
constructors.
All of the methods in an interface
are abstract.
Q: Define Packages in Java?
A: A Package can be defined as a
grouping of related types(classes,
interfaces,
enumerations
and
annotations
)
providing
access
protection
and
name
space
management.
Q: Why Packages are used?
A: Packages are used in Java in-order to
prevent naming conflicts, to control
access, to make searching/locating and
usage
of
classes,
interfaces,
enumerations and annotations, etc.,
easier.
Q: What do you mean by
Multithreaded program?
A: A multithreaded program contains
two or more parts that can run
concurrently. Each part of such a
program is called a thread, and each
thread defines a separate path of
execution.
Q: What are the two ways in which
Thread can be created?
A: Thread
can
be
created
by:
implementing
Runnable
interface,
extending the Thread class.
Q: What is an applet?
A: An applet is a Java program that runs
in a Web browser. An applet can be a
fully functional Java application because
it has the entire Java API at its disposal.
Q: An applet extend which class?
A: An applet extends java.applet.Applet
class.
Java?
A: It uses garbage collection to free the
memory. By cleaning those objects that
is no longer reference by any of the
program.
Q: Define immutable object?
A: An immutable object cant be
changed once it is created.
Q: Explain the usage of this() with
constructors?
A: It is used with variables or methods
and used to call constructer of same
class.
Q: Explain Set Interface?
A: It is a collection of element which
cannot contain duplicate elements. The
Set interface contains only methods
inherited from Collection and adds the
restriction that duplicate elements are
prohibited.
Q: Explain TreeSet?
A: It is a Set implemented when we
want elements in a sorted order.
Q: What is Comparable Interface?
A: It is used to sort collections and
arrays
of
objects
using
the
collections.sort() and java.utils. The
objects of the class implementing the
Comparable interface can be ordered.
Q: Difference between throw and
throws?
A: It includes:
Throw is used to trigger an
exception where as throws is used in
declaration of exception.
Without
throws,
Checked
exception cannot be handled where as
checked exception can be propagated
with throws.
Q: Explain the following line used
under Java Program:
public static void main (String args[
])
A: The following shows the explanation
individually:
public: it is the access specifier.
Q: When ArithmeticException is
thrown?
A: The ArithmeticException is thrown
when integer is divided by zero or
taking the remainder of a number by
zero. It is never thrown in floating-point
operations.
Q: What is a transient variable?
A: A transient variable is a variable that
may
not
be
serialized
during
Serialization and which is initialized by
its default value during de-serialization,
Q: What is synchronization?
superclass of Menu?
A: MenuItem class
Q: Does Java allow Default
Arguments?
A: No, Java does not allow Default
Arguments.
Q: Which number is denoted by
leading zero in java?
A: Octal Numbers are denoted by
leading zero in java, example: 06
Q: Which number is denoted by
leading 0x or 0X in java?
A: Hexadecimal Numbers are denoted
by leading 0x or 0X in java, example:
0XF
Q: Break statement can be used as
labels in Java?
A: Yes, an example can be break one;
Q: Where import statement is used
in a Java program?
A: Import statement is allowed at the
beginning of the program file after
package statement.
Q: Explain suspend() method under
Thread class>
A: It is used to pause or temporarily
stop the execution of the thread.
Q: Explain isAlive() method under
Thread class?
A: It is used to find out whether a
thread is still running or not.
Q: What is currentThread()?
A: It is a public static method used to
obtain a reference to the current thread.
Q: Explain main thread under
Thread class execution?
A: The
main
thread
is
created
automatically and it begins to execute
immediately when a program starts. It
ia thread from which all other child
threads originate.
Q: Life cycle of an applet includes
which steps?
A: Life cycle involves the following
steps:
Initialization
Starting
Stopping
Destroying
Painting
Q: Why is the role of init() method
under applets?
A: It initializes the applet and is the first
method to be called.
Q: Which method is called by
Applet class to load an image?
A: getImage(URL object, filename) is
used for this purpose.
Q: Define code as an attribute of
Applet?
A: It is used to specify the name of the
applet class.
Q: Define canvas?
A: It is a simple drawing surface which
are used for painting images or to
perform other graphical operations.
Q: Define Network Programming?
A: It refers to writing programs that
execute
across
multiple
devices
(computers), in which the devices are all
connected to each other using a
network.
Q: What is a Socket?
A: Sockets provide the communication
mechanism between two computers
using TCP. A client program creates a
socket on its end of the communication
and attempts to connect that socket to
a server.
Q: Advantages of Java Sockets?
A: Sockets are flexible and sufficient.
Efficient socket based programming can
be easily implemented for general
communications. It cause low network
traffic.
Q: Disadvantages of Java Sockets?
A: Socket based communications allows
only to send packets of raw data
between applications. Both the clientside and server-side have to provide
mechanisms to make the data useful in
any way.