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 a constructor and a 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 an 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 the 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 a try-catch block?
Used to handle exceptions.
52. What is a finally block?
Always executes whether exception occurs or not.
53. What is throw and throws keywords?
| throw | throws |
| Throws exception | Declares exception |
54. Can a finally block execute without a catch block?
Yes.
55. What is NullPointerException?
Occurs when accessing a null object.
56. What is exception propagation?
Passing an 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 are FileInputStream and FileOutputStream?
Used to read/write byte data.
62. What is multithreading in Java?
Executing multiple threads simultaneously.
63. What is a 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 from accessing shared resources simultaneously.
67. What is thread sleep?
Pauses thread execution temporarily.