Java Interview Questions & Answers
1. What is Java?
Java is a high-level, object-oriented, platform-independent programming language used for developing web, desktop, mobile, and automation applications.
2. What are the features of Java?
- Platform independent
- Object-oriented
- Secure
- Robust
- Multithreaded
- Portable
3. What are variables in Java?
Variables are containers used to store data values.
Example:
int age = 25;
4. What are data types in Java?
Two types:
- Primitive → int, char, boolean
- Non-primitive → String, Array, Class
5. What is the difference between primitive and non-primitive data types?
| Primitive | Non-Primitive |
| Stores value | Stores reference |
| Fixed size | Dynamic size |
6. What is type casting in Java?
Converting one datatype into another datatype.
Example:
double d = 10.5;
int i = (int)d;
7. What are operators in Java?
Operators perform operations on variables.
Examples:
- Arithmetic (+)
- Logical (&&)
- Relational (>)
8. What is the difference between == and .equals()?
| == | .equals() |
| Compares memory reference | Compares values |
9. What are OOPs concepts in Java?
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
10. What is encapsulation?
Encapsulation means wrapping data and methods together in a single class and restricting direct access using private variables.
11. What is inheritance?
Inheritance allows one class to acquire properties and methods of another class using extends keyword.
12. What is polymorphism?
Polymorphism means one method can perform different actions based on the object.
Types:
- Overloading
- Overriding
13. What is abstraction?
Abstraction means hiding implementation details and showing only required functionality.
14. What is the difference between abstraction and encapsulation?
| Abstraction | Encapsulation |
| Hides implementation | Hides data |
15. What is method overloading?
Method overloading means creating multiple methods with same name but different parameters.
16. What is method overriding?
Method overriding means redefining parent class method in child class with same signature.
17. What is the difference between overloading and overriding?
| Overloading | Overriding |
| Same class | Parent-child class |
| Different parameters | Same parameters |
| Compile-time polymorphism | Runtime polymorphism |
18. What is an interface in Java?
An interface is a blueprint containing abstract methods that must be implemented by classes.
19. What is an abstract class?
An abstract class is a class declared using abstract keyword. It can contain abstract and non-abstract methods
20. What is the difference between interface and abstract class?
| Interface | Abstract Class |
| Supports multiple inheritance | Partial abstraction |
| Mostly abstract methods | Can contain concrete methods |
21. What is constructor in Java?
Constructor initializes object.
22. What is constructor overloading?
A constructor is a special method used to initialize objects.
23. What is constructor overloading?
Creating multiple constructors with different parameters is called constructor overloading
23. What is the difference between constructor and method?
| Constructor | Method |
| Initializes object | Performs operations |
| Same name as class | Any valid name |
24. What is this keyword?
Refers to current class object.
25. What is super keyword?
super refers to parent class object or constructor.
26. What is static keyword in Java?
static belongs to class instead of object.
27. What is final keyword in Java?
Used to:
- Prevent inheritance
- Prevent overriding
- Declare constants
28. Can we override static or final methods?
No, static and final methods cannot be overridden.
29. What is String in Java?
String is a sequence of characters used to store text.
30. What is the difference between String, StringBuffer, and StringBuilder?
| String | StringBuffer | StringBuilder |
| Immutable | Mutable | Mutable |
| Slow | Thread-safe | Faster |
31. Why is String immutable in Java?
For security, thread safety, and performance.
32. What is string pooling?
Java stores string literals in common memory called String Pool.
33. What is the difference between equals() and equalsIgnoreCase()?
| equals() | equalsIgnoreCase() |
| Case-sensitive | Ignores case |
34. How do you reverse a string in Java?
Using StringBuilder:
String rev = new StringBuilder(str).reverse().toString();
35. How do you remove duplicates from a string?
Duplicates can be removed using loops, Set collection, or HashSet.
36. How do you count characters in a string?
Using length() method.
str.length();
37. What is an array in Java?
An array is a collection of elements of the same datatype stored in a single variable. It is used to store multiple values together using index positions.
38. What is ArrayList?
ArrayList is a class in Java Collection Framework used to store dynamic collections of elements. Unlike arrays, its size can grow or shrink automatically.
39. What is the difference between Array and ArrayList?
| Array | ArrayList |
| Fixed size | Dynamic size |
| Stores primitives | Stores objects |
40. What is the difference between List, Set, and Map?
41. What is HashMap in Java?
Stores key-value pairs
42. What is the difference between HashMap and Hashtable?
| HashMap | Hashtable |
| Not synchronized | Synchronized |
43. What is the difference between HashSet and TreeSet?
| HashSet | TreeSet |
| Unordered | Sorted |
44. What is Iterator in Java?
Used to traverse collection elements.
45. What is the difference between Iterator and ListIterator?
| Iterator | ListIterator |
| Forward only | Forward & backward |
46. What is Collection Framework in Java?
Framework containing classes/interfaces for storing data.
47. What is Comparable and Comparator?
| Comparable | Comparator |
| Natural sorting | Custom sorting |
48. What is the difference between ArrayList and LinkedList?
| ArrayList | LinkedList |
| Faster retrieval | Faster insertion/deletion |
49. What is exception handling in Java?
Mechanism to handle runtime errors.
50. What is the difference between checked and unchecked exceptions?
| Checked | Unchecked |
| Compile-time | Runtime |
51. What is try-catch block?
Used to handle exceptions.
52. What is finally block?
Always executes whether exception occurs or not.
53. What is throw and throws keyword?
| throw | throws |
| Throws exception | Declares exception |
54. Can finally block execute without catch block?
Yes.
55. What is NullPointerException?
Occurs when accessing null object.
56. What is exception propagation?
Passing exception from one method to another.
57. What is file handling in Java?
Reading and writing files using Java classes.
58. How do you read data from a file in Java?
Using:
- BufferedReader
- Scanner
- FileReader
59. How do you write data into a file?
Using FileWriter or BufferedWriter.
60. What is BufferedReader?
Used to read text efficiently.
61. What is FileInputStream and FileOutputStream?
Used to read/write byte data.
62. What is multithreading in Java?
Executing multiple threads simultaneously.
63. What is thread in Java?
Smallest unit of execution.
64. What is the difference between process and thread?
| Process | Thread |
| Independent execution | Part of process |
65. How do you create a thread in Java?
Using:
- Thread class
- Runnable interface
66. What is synchronization?
Prevents multiple threads accessing shared resources simultaneously.
67. What is thread sleep?
Pauses thread execution temporarily.
68. What is the difference between start() and run()?
| start() | run() |
| Creates new thread | Executes normally |
1. What is a Constructor in Java?
A constructor in Java is a special block of code used to initialize an object when it is created.
- The constructor name must be the same as the class name
- It does not have any return type (not even
void) - It is automatically called when an object is created
- It is mainly used to assign initial values to object variables
If a class does not have any constructor, the Java compiler automatically provides a default constructor
class Student {
int id;
Student() { // Constructor
id = 100;
}
}
Key Points to Remember (for interview):
- Constructor ≠ Method
- Used for object initialization
- Can be overloaded (multiple constructors with different parameters)
- Types:
- Default constructor
- Parameterized constructor
2. What is Inheritance in Java?
Inheritance in Java is an OOP concept where one class (child/subclass) acquires the properties and behaviors (variables and methods) of another class (parent/superclass).
It helps in code reusability and reduces duplication.
Key Points (Interview Focus):
- Achieved using the
extendskeyword - Promotes code reuse
- Supports method overriding
- Java supports:
- Single inheritance
- Multilevel inheritance
- Hierarchical inheritance
- Java does not support multiple inheritance with classes (but supports it using interfaces)
class Animal {
void eat() {
System.out.println("Eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking...");
}
}
Here, Dog inherits the eat() method from Animal.
3. What is Polymorphism in Java?
Polymorphism in Java means “many forms”—it allows a single method or object to behave in different ways depending on the context.
Types of Polymorphism:
1. Compile-time Polymorphism (Method Overloading)
- Same method name
- Different parameters
- Decided at compile time
class Calculator {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
2. Runtime Polymorphism (Method Overriding)
- Same method name
- Same parameters
- Different implementation in child class
- Decided at runtime
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Bark");
}
}
Key Points (Interview Focus):
- Achieves flexibility and reusability
- Overloading → compile-time
- Overriding → runtime
- Runtime polymorphism uses inheritance