Top 500 Java Interview Questions and Answers
Top 500 Java Interview Questions and Answers
bytecode, which is a program that contains instructions that must be interpreted into instructions that can be sent
directly to the processor.
A: 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 virtual Machine (JVM) on whichever platform it is
being run.
A: 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-toobject
communication.
Q: Define class?
http://bangalorejobseeker.org/top-500-java-interview-questions-and-answers/ 1/18
5/19/2017 Top 500 Java Interview Questions and Answers
A: 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.
A: These are variables declared with in a class, outside any method, with the static keyword.
0.0d
an int.
http://bangalorejobseeker.org/top-500-java-interview-questions-and-answers/ 2/18
5/19/2017 Top 500 Java Interview Questions and Answers
A: 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.
subclasses in other package or any class within the package of the protected members’ class.
indicate that a method can be accessed by only one thread at a time. Q: According to Java Operator
precedence, which operator is considered to be with highest precedence?
A: Variables used in a switch statement can only be a byte, short, int, or char.
Q: When parseInt() method can be used?
A: This method is used to get the primitive data type of a certain String.
Q: Why is String class considered immutable?
A: The 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.
Q: What is an Exception?
http://bangalorejobseeker.org/top-500-java-interview-questions-and-answers/ 3/18
5/19/2017 Top 500 Java Interview Questions and Answers
A: 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.
example, if a file is to be opened, but the file cannot be found, an exception occurs. These exceptions cannot simply be
ignored at the time of compilation.
Q: What things should be kept in mind while creating your own exceptions in Java?
A: While creating your own exception:
• All exceptions must be a child of Throwable.
• If you want to write a checked exception that is automatically enforced by the Handle or DeclareRule, you need to
extend the Exception class.
• You want to write a runtime exception, you need to extend the RuntimeException class.
Q: Define Inheritance?
A: It is the process where one object acquires the properties of another. With the use of inheritance the information is
of the keyword super. It can be also used to refer to a hidden field Q: What is Polymorphism?
http://bangalorejobseeker.org/top-500-java-interview-questions-and-answers/ 4/18
5/19/2017 Top 500 Java Interview Questions and Answers
A: 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.
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
contains one or more abstract methods which are simply method declarations without a body.
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.
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.
http://bangalorejobseeker.org/top-500-java-interview-questions-and-answers/ 5/18
5/19/2017 Top 500 Java Interview Questions and Answers
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: Explain TreeSet?
http://bangalorejobseeker.org/top-500-java-interview-questions-and-answers/ 6/18
5/19/2017 Top 500 Java Interview Questions and Answers
A: Java Runtime Environment is an implementation of the Java Virtual Machine which executes Java programs. It
provides the minimum requirements for executing a Java application; Q: What is JAR file?
A: JAR files is Java Archive files and it aggregates many files into one. It holds Java classes in a library. JAR files are built
collection of JavaServer Pages, Java Servlets, Java classes, XML files, static Web pages etc.
A: Object based programming languages follow all the features of OOPs except Inheritance.
JavaScript is an example of object based programming languages Q: What is the
purpose of default constructor?
A: The java compiler creates a default constructor only if there is no constructor in the class.
Q: Define composition?
A: Holding the reference of the other class within some other class is known as composition.
http://bangalorejobseeker.org/top-500-java-interview-questions-and-answers/ 7/18
5/19/2017 Top 500 Java Interview Questions and Answers
A: 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
Q: What is NullPointerException?
A: A NullPointerException is thrown when calling the instance method of a null object, accessing or modifying the field
Q: What are the ways in which a thread can enter the waiting state?
A: 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.
Q: Does it matter in what order catch statements for FileNotFoundException and IOException are written?
A: Yes, it does. The FileNotFoundException is inherited from the IOException. Exception’s subclasses have to be caught
first.
Q: How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8 characters?
A: 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.
http://bangalorejobseeker.org/top-500-java-interview-questions-and-answers/ 8/18
5/19/2017 Top 500 Java Interview Questions and Answers
etc.
components.
hierarchy?
A: The Reader/Writer class hierarchy is character-oriented, and the InputStream/OutputStream class hierarchy is byte-
oriented.
Q: Which class should you use to obtain design information about an object?
A: The Class class is used to obtain information about an object’s design and java.lang.Class class instance represent
http://bangalorejobseeker.org/top-500-java-interview-questions-and-answers/ 9/18
5/19/2017 Top 500 Java Interview Questions and Answers
Q: Can you write a Java class that could be used both as an applet as well as an application?
A: Yes, just add a main() method to the applet.
depend on the local windowing toolkit. For example, java.awt.Button is a heavyweight component, when it is
running on the Java platform for Unix platform, it maps to a real Motif button.
and sometimes error-prone when dynamic overriding or dynamic overloading in some situation.
Q: Can you call one constructor from another if a class has multiple constructors?
A: Synchronization is the capability to control the access of multiple threads to shared resources. synchronized
keyword in java provides locking which ensures mutual exclusive access of shared resource and prevent data race.
http://bangalorejobseeker.org/top-500-java-interview-questions-and-answers/ 10/18
5/19/2017 Top 500 Java Interview Questions and Answers
Q: Does garbage collection guarantee that a program will not run out of memory?
A: Garbage collection does not guarantee that a program will not run out of memory. It is possible for programs to use
up memory resources faster than they are garbage collected. It is also possible for programs to create objects that are
http://bangalorejobseeker.org/top-500-java-interview-questions-and-answers/ 11/18
5/19/2017 Top 500 Java Interview Questions and Answers
Q: What will happen if static modifier is removed from the signature of the main method?
A: No, a top level class can not be private or protected. It can have either “public” or no modifier.
Q: Why do we need wrapper classes?
A: We can pass them around as method parameters where a method expects an object. It also provides utility
methods.
http://bangalorejobseeker.org/top-500-java-interview-questions-and-answers/ 12/18
5/19/2017 Top 500 Java Interview Questions and Answers
A: Synchronized methods are methods that are used to control access to an object. A synchronized statement can only
be executed after a thread has acquired the lock for the object or class referenced in the synchronized statement.
at runtime rather than at compile-time. In this process, an overridden method is called through the reference variable
of a superclass.
elements from any place other than the end, then ArrayList should be used.
http://bangalorejobseeker.org/top-500-java-interview-questions-and-answers/ 13/18
5/19/2017 Top 500 Java Interview Questions and Answers
• Newborn state
• Runnable state
• Running state
• Blocked state
• Dead state
Q: What is the difference between the >> and >>> operators?
A: The >> operator carries the sign bit when shifting right. The >>> zero-fills bits that have been shifted out.
Q: Which method of the Component class is used to set the position and size of a component?
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.
http://bangalorejobseeker.org/top-500-java-interview-questions-and-answers/ 14/18
5/19/2017 Top 500 Java Interview Questions and Answers
• Starting
• Stopping
• Destroying
• Painting
Q: Define canvas?
A: It is a simple drawing surface which are used for painting images or to perform other graphical operations.
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: Which class is used by server applications to obtain a port and listen for client requests? A:
java.net.ServerSocket class is used by server applications to obtain a port and listen for client requests
Q: Which class represents the socket that both the client and server use to communicate with each other?
A: java.net.Socket class represents the socket that both the client and server use to communicate with each other.
http://bangalorejobseeker.org/top-500-java-interview-questions-and-answers/ 15/18
5/19/2017 Top 500 Java Interview Questions and Answers
A: 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
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.
A: The GregorianCalendar provides support for traditional Western calendars Q: What is the
SimpleTimeZone class?
http://bangalorejobseeker.org/top-500-java-interview-questions-and-answers/ 16/18
5/19/2017 Top 500 Java Interview Questions and Answers
Q: What is an enumeration?
A: An enumeration is an interface containing methods for accessing the underlying data structure from which the
enumeration is obtained. It allows sequential access to all the elements stored in the collection.
executables(.exe) files and classpath is used to specify the location of .class files.
A: Polymorphism
Q: What is an object’s lock and which object’s have locks?
A: An object’s lock is a mechanism that is used by multiple threads to obtain synchronized access to the object. A
thread may execute a synchronized method of an object only after it has acquired the object’s lock.
Q: What is Downcasting?
A: It is the casting from a general to a more specific type, i.e. casting down the hierarchy.
Q: What are order of precedence and associativity and how are they used?
A: Order of precedence determines the order in which operators are evaluated in expressions.
Associatity determines whether an expression is evaluated left-to-right or right-to-left.
http://bangalorejobseeker.org/top-500-java-interview-questions-and-answers/ 17/18
5/19/2017 Top 500 Java Interview Questions and Answers
order in which they appear. The first catch clause that is capable of handling the exception is executed. The remaining
catch clauses are ignored.
Q: What will be the default values of all the elements of an array defined as an instance variable? A: 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.
http://bangalorejobseeker.org/top-500-java-interview-questions-and-answers/ 18/18