Top 200+ Java Interview QA Tutorial Point
Top 200+ Java Interview QA Tutorial Point
What is Java?
Java programming language was originally developed by Sun Microsystems which was
initiated by James Gosling and released in 1995 as the core component of Sun
Microsystems' Java platform (Java 1.0 [J2SE]). The latest release of the Java Standard
Edition is Java SE 8. With the advancement of Java and its widespread popularity,
multiple configurations were built to suit various types of platforms. For example, J2EE
for Enterprise Applications, and J2ME for Mobile Applications.
We have categorized these questions into 3 sections. Let's practice the interview
questions and answers on Java programming language:
https://www.tutorialspoint.com/java/java_interview_questions.htm 1/27
Page 2 of 27
Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions
of UNIX/Linux like HP-Unix, Sun Solaris, Redhat Linux, Ubuntu, CentOS, etc.
When Java is compiled, it is not compiled into platform specific machine, rather into
platform independent byte code. This byte code is distributed over the web and
interpreted by Java virtual Machine (JVM) on whichever platform it is being run.
The two important and commonly used Java IDE's are Netbeans and Eclipse.
https://www.tutorialspoint.com/java/java_interview_questions.htm 2/27
Page 3 of 27
Object is a runtime entity and it's state is stored in fields and behavior is shown via
methods. Methods operate on an object's internal state and serve as the primary
mechanism for object-to-object communication.
A class is a blue print from which individual objects are created. A class can contain fields
and methods to describe the behavior of an object.
Variables defined inside methods, constructors or blocks are called local variables. The
variable will be declared and initialized within the method and it will be destroyed when
the method has completed.
Instance variables are variables within a class but outside any method. These variables
are instantiated when the class is loaded.
These are variables declared with in a class, outside any method, with the static
keyword.
Singleton class control object creation, limiting the number to one but allowing the
flexibility to create more objects if the situation changes.
https://www.tutorialspoint.com/java/java_interview_questions.htm 3/27
Page 4 of 27
Java constructor gets invoked when a new object is created. Every class has a
constructor. If we do not explicitly write a constructor for a class the java compiler builds
a default constructor for that class.
18. List the three steps for creating an Object for a class?
20. What is the default value of float and double datatype in Java?
In Java, the default value of float is 0.0f and the default value of double is 0.0d.
The byte data type is used to save space in large arrays, mainly in place of integers,
since a byte is four times smaller than an int.
Class variables also known as static variables are declared with the static keyword in a
class, but outside a method, constructor or a block.
Java provides access modifiers to set access levels for classes, variables, methods and
constructors. A member has package or default accessibility when no accessibility
modifier is specified.
Variables, methods and constructors which are declared protected in a superclass can be
accessed only by the subclasses in other package or any class within the package of the
protected members' class.
Java provides these modifiers for providing functionalities other than Access Modifiers,
synchronized used to indicate that a method can be accessed by only one thread at a
time.
https://www.tutorialspoint.com/java/java_interview_questions.htm 4/27
Page 5 of 27
Variables used in a switch statement can only be a string, enum, byte, short, int, or char.
Java parseInt() method is used to get the primitive data type of a certain String.
Java String class is immutable, so that once it is created a String object cannot be
changed. Since String is immutable it can safely be shared between many threads ,which
is considered very important for multithreaded programming.
Java String class is considered as immutable, so that once it is created a String object
cannot be changed. If there is a necessity to make alot of modifications to Strings of
characters then StringBuffer should be used.
Use StringBuilder whenever possible because it is faster than StringBuffer. But, if thread
safety is necessary then use StringBuffer objects.
https://www.tutorialspoint.com/java/java_interview_questions.htm 5/27
Page 6 of 27
It is possible to define a method that will be called just before an object's final
destruction by the garbage collector. This method is called finalize(), and it can be used
to ensure that an object terminates cleanly.
An exception is a problem that arises during the execution of a program. Exceptions are
caught by handlers positioned along the thread's method invocation stack.
It is an exception that occurs that probably could have been avoided by the programmer.
As opposed to checked exceptions, runtime exceptions are ignored at the time of
compliation.
https://www.tutorialspoint.com/java/java_interview_questions.htm 6/27
Page 7 of 27
42. What things should be kept in mind while creating your own
exceptions in Java?
While creating your own exception −
You want to write a runtime exception, you need to extend the RuntimeException
class.
If the method overrides one of its superclass's methods, overridden method can be
invoked through the use of the keyword super. It can be also used to refer to a hidden
field.
Polymorphism is the ability of an object to take on many forms. The most common use
of polymorphism in OOP occurs when a parent class reference is used to refer to a child
class object.
Abstraction 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.
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.
https://www.tutorialspoint.com/java/java_interview_questions.htm 7/27
Page 8 of 27
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.
Encapsulation 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.
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.
https://www.tutorialspoint.com/java/java_interview_questions.htm 8/27
Page 9 of 27
etc., easier.
56. What are the two ways in which Thread can be created?
Thread can be created by: implementing Runnable interface, extending the Thread class.
It uses garbage collection to free the memory. By cleaning those objects that is no
longer reference by any of the program.
https://www.tutorialspoint.com/java/java_interview_questions.htm 9/27
Page 10 of 27
It includes:
https://www.tutorialspoint.com/java/java_interview_questions.htm 10/27
Page 11 of 27
This is Web Archive File and used to store XML, java classes, and JavaServer pages.
which is used to distribute a collection of JavaServer Pages, Java Servlets, Java classes,
XML files, static Web pages etc.
Object based programming languages follow all the features of OOPs except Inheritance.
JavaScript is an example of object based programming languages.
https://www.tutorialspoint.com/java/java_interview_questions.htm 11/27
Page 12 of 27
Method overloading increases the readability of the program. Method overriding provides
the specific implementation of the method that is already provided by its super class
parameter must be different in case of overloading, parameter must be same in case of
overriding.
81. What are the ways in which a thread can enter the waiting
state?
A thread can enter the waiting state by invoking its sleep() method, by blocking on IO,
by unsuccessfully attempting to acquire an object's lock, or by invoking an object's
wait() method. It can also enter the waiting state by invoking its (deprecated) suspend()
method.
https://www.tutorialspoint.com/java/java_interview_questions.htm 12/27
Page 13 of 27
When a task invokes its yield() method, it returns to the ready state. When a task
invokes its sleep() method, it returns to the waiting state.
87. How many bits are used to represent Unicode, ASCII, UTF-16,
and UTF-8 characters?
Unicode requires 16 bits and ASCII require 7 bits. Although the ASCII character set uses
only 7 bits, it is usually represented as 8 bits. UTF-8 represents characters using 8, 16,
and 18 bit patterns. UTF-16 uses 16-bit and larger bit patterns.
https://www.tutorialspoint.com/java/java_interview_questions.htm 13/27
Page 14 of 27
94. Which class should you use to obtain design information about
an object?
The Class class is used to obtain information about an object's design and
java.lang.Class class instance represent classes, interfaces in a running Java application.
A static variable is associated with the class as a whole rather than with specific
instances of a class. Non-static variables take on unique values with each object
instance.
It is part of the analysis of a program and describes a situation that a program might
encounter and what behavior the program should exhibit in that circumstance.
100. Can you write a Java class that could be used both as an
applet as well as an application?
Yes, just add a main() method to the applet.
https://www.tutorialspoint.com/java/java_interview_questions.htm 14/27
Page 15 of 27
105. Can you call one constructor from another if a class has
multiple constructors?
Yes, use this() syntax.
https://www.tutorialspoint.com/java/java_interview_questions.htm 15/27
Page 16 of 27
https://www.tutorialspoint.com/java/java_interview_questions.htm 16/27
Page 17 of 27
A private variable may only be accessed within the class in which it is declared.
https://www.tutorialspoint.com/java/java_interview_questions.htm 17/27
Page 18 of 27
Yes
A class loader is an object that is responsible for loading classes. The class ClassLoader
is 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.
No, a top level class can not be private or protected. It can have either "public" or no
modifier.
We can pass them around as method parameters where a method expects an object. The
wrapper class also provides utility methods.
https://www.tutorialspoint.com/java/java_interview_questions.htm 18/27
Page 19 of 27
134. When a thread is created and started, what is its initial state?
A thread is in the ready state as initial state after it has been created and started.
https://www.tutorialspoint.com/java/java_interview_questions.htm 19/27
Page 20 of 27
Newborn state
Runnable state
Running state
Blocked state
Dead state
https://www.tutorialspoint.com/java/java_interview_questions.htm 20/27
Page 21 of 27
148. What is the difference between the >> and >>> operators?
The >> operator carries the sign bit when shifting right. The >>> zero-fills bits that
have been shifted out.
https://www.tutorialspoint.com/java/java_interview_questions.htm 21/27
Page 22 of 27
Initialization
Starting
Stopping
Destroying
Painting
It is a simple drawing surface which are used for painting images or to perform other
graphical operations.
https://www.tutorialspoint.com/java/java_interview_questions.htm 22/27
Page 23 of 27
It refers to writing programs that execute across multiple devices (computers), in which
the devices are all connected to each other using a network.
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.
Sockets are flexible and sufficient. Efficient socket based programming can be easily
implemented for general communications. It cause low network traffic.
Socket based communications allows only to send packets of raw data between
applications. Both the client-side and server-side have to provide mechanisms to make
the data useful in any way.
java.net.ServerSocket class is used by server applications to obtain a port and listen for
client requests
171. Which class represents the socket that both the client and
server use to communicate with each other?
java.net.Socket class represents the socket that both the client and server use to
communicate with each other.
Generics provide compile-time type safety that allows programmers to catch invalid
types at compile time. Java Generic methods and generic classes enable programmers to
specify, with a single method declaration, a set of related methods or, with a single class
declaration, a set of related types.
https://www.tutorialspoint.com/java/java_interview_questions.htm 23/27
Page 24 of 27
No, there is no need to import this package. It is by default loaded internally by the JVM.
If a class is declared within a class and specify the static modifier, the compiler treats the
class just like any other top-level class. Nested top-level class is an Inner class.
177. If System.exit (0); is written at the end of the try block, will
the finally block still execute?
No in this case the finally block will not execute because when you say System.exit (0);
the control immediately goes out of the program, and thus finally never executes.
Daemon thread is a low priority thread, which runs intermittently in the back ground
doing the garbage collection operation for the java runtime system.
https://www.tutorialspoint.com/java/java_interview_questions.htm 24/27
Page 25 of 27
The size is the number of elements actually stored in the vector, while capacity is the
maximum number of elements it can store at a given instance of time.
Path and Classpath are operating system level environment variales. Path is defines
where the system can find the executables(.exe) files and classpath is used to specify
the location of .class files.
A static method should not refer to instance variables without creating an instance and
cannot use "this" operator to refer the instance.
Polymorphism
https://www.tutorialspoint.com/java/java_interview_questions.htm 25/27
Page 26 of 27
It is the casting from a general to a more specific type, i.e. casting down the hierarchy.
193. What are order of precedence and associativity and how are
they used?
A protected method may only be accessed by classes or interfaces of the same package
or by subclasses of the class in which it is declared.
195. What is the difference between inner class and nested class?
When a class is defined within a scope of another class, then it becomes inner class. If
the access modifier of the inner class is static, then it becomes nested class.
A child object constructor always first needs to construct its parent. In Java it is done via
an implicit call to the no-args constructor as the first statement.
https://www.tutorialspoint.com/java/java_interview_questions.htm 26/27
Page 27 of 27
When an exception is thrown within the body of a try statement, the catch clauses of the
try statement are examined in the order in which they appear. The first catch clause that
is capable of handling the exception is executed. The remaining catch clauses are
ignored.
200. What will be the default values of all the elements of an array
defined as an instance variable?
If the array is an array of primitive types, then all the elements of the array will be
initialized to the default value corresponding to that primitive type.
https://www.tutorialspoint.com/java/java_interview_questions.htm 27/27