Java Interview Questions and Answers
Basic Level Java Interview Questions
Q: What is Java?
A: Java is a high-level, class-based, object-oriented programming language designed to have as few
implementation dependencies as possible.
Q: What are the features of Java?
A: Platform independent, Object-Oriented, Secure, Robust, Portable, Multithreaded, and High
performance.
Q: What is the difference between JDK, JRE, and JVM?
A: - JVM: Java Virtual Machine, runs Java bytecode
- JRE: Java Runtime Environment, contains JVM + libraries
- JDK: Java Development Kit, contains JRE + development tools
Q: What is a class and object in Java?
A: A class is a blueprint for objects. An object is an instance of a class.
Q: What is the difference between method overloading and method overriding?
A: Overloading: Same method name, different parameters (compile time).
Overriding: Subclass provides specific implementation (runtime).
Q: What is inheritance in Java?
A: Inheritance is when one class acquires the properties and methods of another class using the
'extends' keyword.
Q: What are access modifiers in Java?
A: Access modifiers define the visibility of class members. Types: public, private, protected, and
default.
Q: What is the difference between == and .equals()?
A: == compares references, .equals() compares actual content/values.
Q: What is a constructor? Types?
A: A constructor initializes objects. Types: Default, Parameterized, Copy constructor.
Q: What is the difference between static and non-static methods?
A: Static methods belong to the class and can be called without objects. Non-static methods require
object creation.
OOPs Concepts in Java (Frequently Asked)
Q: Explain the 4 pillars of OOPs
A: Abstraction, Encapsulation, Inheritance, Polymorphism.
Q: What is abstraction? How is it implemented in Java?
A: Abstraction hides complexity using abstract classes or interfaces.
Q: What is interface and how is it different from abstract class?
A: Interface has only abstract methods (Java 7), while abstract class can have both. A class can
implement multiple interfaces.
Q: What is encapsulation and its real-life example in Java?
A: Encapsulation binds data and code together. Example: Using private variables with public
getter/setter methods.
Intermediate to Advanced Java Questions
Q: What is exception handling?
A: It is the process of handling runtime errors using try-catch-finally blocks.
Q: Difference between checked and unchecked exceptions?
A: Checked: Checked at compile time (e.g., IOException). Unchecked: Occur at runtime (e.g.,
NullPointerException).
Q: What is multithreading in Java?
A: Multithreading allows concurrent execution of two or more threads for maximum CPU utilization.
Q: Explain the use of synchronized keyword
A: Used to avoid thread interference by locking access to a block/method.
Q: Difference between ArrayList and LinkedList?
A: ArrayList is better for storing and accessing data. LinkedList is better for manipulating data
(inserts/deletes).
Q: Difference between HashMap and Hashtable?
A: HashMap is non-synchronized and allows one null key. Hashtable is synchronized and doesn't
allow null keys or values.
Q: What are Wrapper classes in Java?
A: They convert primitive types into objects (e.g., int to Integer).
Q: Difference between String, StringBuffer, and StringBuilder?
A: String: Immutable
StringBuffer: Mutable and thread-safe
StringBuilder: Mutable and not thread-safe
Q: What is the Collection Framework in Java?
A: A set of classes and interfaces for storing and manipulating groups of data.
Q: What is garbage collection in Java?
A: Automatic memory management that removes unused objects to free memory.
Practical & Coding Questions
Q: Program to check if a number is prime
A: for(int i=2;i<=n/2;i++) if(n%i==0) not prime; else prime
Q: Program to reverse a string
A: StringBuilder sb = new StringBuilder(str); [Link]();
Q: Program to find the factorial of a number
A: int fact=1; for(int i=1;i<=n;i++) fact *= i;
Q: Program to implement a simple calculator
A: Use switch-case for +, -, *, / based on user input.
Q: Program to sort an array
A: [Link](array); or implement Bubble/Selection sort manually.
Java 8+ Features (For Experienced)
Q: What are Lambda expressions?
A: They provide a concise way to represent anonymous methods (functions).
Q: What is a Functional Interface?
A: An interface with a single abstract method. Used in lambda expressions.
Q: What is Stream API and how is it used?
A: Processes collections in a functional style. Used for filtering, mapping, etc.
Q: What are default and static methods in interfaces?
A: Java 8 allows interfaces to have method implementations using default/static.
Q: What is Optional class in Java 8?
A: Helps avoid NullPointerException by wrapping potentially null values.