Basic Java Interview Questions and Answers
Q: What is Java?
A: Java is a high-level, class-based, object-oriented programming language that is designed to have
as few implementation dependencies as possible.
Q: What are the main features of Java?
A: Java is platform-independent, object-oriented, secure, robust, multithreaded, and supports
automatic garbage collection.
Q: What is JVM?
A: JVM stands for Java Virtual Machine. It is an engine that provides a runtime environment to drive
Java applications. It converts bytecode into machine language.
Q: What is the difference between JDK and JRE?
A: JDK (Java Development Kit) is a software development kit used to develop Java applications,
whereas JRE (Java Runtime Environment) provides libraries and JVM to run Java applications.
Q: What is a Class in Java?
A: A class in Java is a blueprint for creating objects. It defines a datatype by bundling data and
methods that operate on the data.
Q: What is the difference between a method and a function in Java?
A: In Java, the term method is used for functions that are part of a class. Both perform the same
role, but methods are part of an object or class.
Q: What is an object in Java?
A: An object is an instance of a class. It has a state (fields) and behavior (methods).
Q: What is the difference between '==' and 'equals()' in Java?
A: '==' is used to compare primitive data types and references, while 'equals()' is used to compare
the contents of objects.
Q: What is inheritance in Java?
A: Inheritance is a mechanism in Java where one class acquires the properties and behaviors of
another class. It allows for code reusability and method overriding.
Q: What is polymorphism?
A: Polymorphism is the ability of an object to take many forms. In Java, it is achieved through
method overloading and method overriding.
Q: What is encapsulation?
A: Encapsulation is the concept of wrapping data and methods into a single unit (class). It restricts
access to certain components of an object by using access modifiers.
Q: What is abstraction?
A: Abstraction is the concept of hiding the complexity and showing only the essential features of an
object. It is achieved using abstract classes and interfaces.
Q: What are access modifiers in Java?
A: Access modifiers define the accessibility of classes, methods, and variables. They are: public,
private, protected, and default.
Q: What is a constructor in Java?
A: A constructor in Java is a special method that is used to initialize objects. It is invoked when an
object of a class is created.
Q: What is the difference between String and StringBuffer in Java?
A: String is immutable, while StringBuffer is mutable, meaning the contents of String cannot be
changed, but StringBuffer allows modifications.
Q: What is an interface in Java?
A: An interface in Java is an abstract type used to specify a set of methods that a class must
implement. It is used to achieve abstraction and multiple inheritance.
Q: What is a thread in Java?
A: A thread is a lightweight process. It is the smallest unit of execution in a Java program. Java
supports multithreading to perform multiple tasks simultaneously.
Q: What is the difference between ArrayList and LinkedList?
A: ArrayList is backed by an array and provides fast random access but slower insertions/deletions,
whereas LinkedList is backed by a doubly linked list and provides faster insertions/deletions but
slower access.
Q: What is the use of the 'final' keyword in Java?
A: The 'final' keyword is used to define constants, prevent method overriding, and prevent
inheritance.
Q: What is exception handling in Java?
A: Exception handling in Java is a mechanism to handle runtime errors, using constructs like try,
catch, throw, throws, and finally.
Q: What is garbage collection in Java?
A: Garbage collection is the process of automatically reclaiming memory by deleting objects that are
no longer in use.
Q: What is the difference between 'throw' and 'throws' in Java?
A: 'throw' is used to explicitly throw an exception, whereas 'throws' is used in a method signature to
declare that a method can throw an exception.