Java Interview Handbook - 100 Questions with Answers
Prepared for Interview Preparation
1. What is Java?
Java is an object-oriented, platform-independent programming language.
2. Why is Java platform independent?
Java code is compiled into bytecode which runs on the JVM.
3. Difference between JDK, JRE, JVM?
JDK=development tools, JRE=runtime environment, JVM=executes bytecode.
4. What is JVM?
Java Virtual Machine executes Java bytecode.
5. What is JIT?
Just-In-Time compiler converts bytecode into native machine code.
6. Heap vs Stack?
Heap stores objects; Stack stores method frames and local variables.
7. What are primitive data types?
byte, short, int, long, float, double, char, boolean.
8. What is wrapper class?
Object representation of primitive types.
9. What is autoboxing?
Automatic conversion of primitive to wrapper object.
10. == vs equals()?
== compares references; equals() compares content.
11. What is String Pool?
Memory area where string literals are stored.
12. Why is String immutable?
Security, thread safety, caching benefits.
13. String vs StringBuilder vs StringBuffer?
String immutable, Builder fast, Buffer synchronized.
14. What is constructor?
Special method used to initialize objects.
15. What is constructor overloading?
Multiple constructors with different parameters.
16. What is this keyword?
Refers to current object.
17. What is super keyword?
Refers to parent class members.
18. What is static keyword?
Belongs to class rather than object.
19. What is static block?
Executed once when class loads.
20. Can constructor be static?
No.
21. What is OOP?
Programming based on objects and classes.
22. What is Encapsulation?
Binding data and methods together.
23. What is Abstraction?
Hiding implementation details.
24. What is Inheritance?
Acquiring properties of parent class.
25. What is Polymorphism?
One interface, many forms.
26. Method Overloading?
Same method name, different parameters.
27. Method Overriding?
Child class redefines parent method.
28. Interface vs Abstract Class?
Interface defines contract; abstract class provides partial implementation.
29. Why no multiple inheritance with classes?
Avoids diamond problem.
30. What is Runtime Polymorphism?
Method overriding resolved at runtime.
31. What is Object class?
Root class of Java hierarchy.
32. Purpose of toString()?
Returns string representation of object.
33. Purpose of hashCode()?
Generates hash value for object.
34. Contract between equals and hashCode?
Equal objects must have same hashCode.
35. What is instanceof?
Checks object type.
36. Association?
Relationship between objects.
37. Aggregation?
Weak HAS-A relationship.
38. Composition?
Strong HAS-A relationship.
39. What is cloning?
Creating copy of object.
40. Shallow vs Deep Copy?
Reference copy vs complete copy.
41. What is Exception?
Event disrupting normal flow.
42. Checked vs Unchecked Exception?
Compile-time vs runtime exceptions.
43. try-catch-finally?
Handles exceptions and cleanup.
44. throw vs throws?
throw creates exception; throws declares it.
45. Can finally not execute?
Yes, during [Link]() or JVM crash.
46. Custom Exception?
User-defined exception class.
47. What is Exception Propagation?
Exception moves up call stack.
48. Try-with-resources?
Automatically closes resources.
49. Error vs Exception?
Errors are serious JVM issues.
50. Multiple catch blocks?
Handle different exceptions separately.
51. What is Collection Framework?
Standard architecture for storing data.
52. List vs Set?
List allows duplicates; Set doesn't.
53. ArrayList vs LinkedList?
ArrayList faster access; LinkedList faster insertion.
54. HashMap?
Stores key-value pairs.
55. How HashMap works?
Uses hashing and buckets.
56. Hashtable vs HashMap?
Hashtable synchronized; HashMap not.
57. ConcurrentHashMap?
Thread-safe map.
58. HashSet?
Stores unique elements.
59. TreeSet?
Sorted unique elements.
60. TreeMap?
Sorted key-value pairs.
61. LinkedHashMap?
Maintains insertion order.
62. Comparable?
Defines natural ordering.
63. Comparator?
Defines custom ordering.
64. Iterator?
Traverses collections.
65. Fail-fast Iterator?
Throws ConcurrentModificationException.
66. PriorityQueue?
Elements ordered by priority.
67. Vector?
Synchronized dynamic array.
68. CopyOnWriteArrayList?
Thread-safe list for read-heavy operations.
69. What is Queue?
FIFO data structure.
70. What is Deque?
Double-ended queue.
71. Process vs Thread?
Process is independent execution unit; thread is lightweight.
72. How to create thread?
Extend Thread or implement Runnable.
73. Runnable vs Callable?
Callable returns value.
74. Thread Lifecycle?
New, Runnable, Running, Waiting, Terminated.
75. Synchronization?
Controls concurrent access.
76. Deadlock?
Threads waiting on each other forever.
77. Volatile?
Ensures visibility across threads.
78. Atomic Classes?
Provide lock-free thread safety.
79. Executor Framework?
Manages thread pools.
80. Future?
Represents async result.
81. CountDownLatch?
Waits until tasks complete.
82. CyclicBarrier?
Synchronizes multiple threads.
83. Thread Pool?
Reusable worker threads.
84. Lambda Expression?
Anonymous function introduced in Java 8.
85. Functional Interface?
Interface with one abstract method.
86. Stream API?
Processes collections declaratively.
87. map()?
Transforms elements.
88. filter()?
Filters elements.
89. flatMap()?
Flattens nested structures.
90. Optional?
Avoids NullPointerException.
91. Method Reference?
Short form of lambda.
92. Predicate?
Functional interface returning boolean.
93. Consumer?
Consumes input without returning value.
94. Supplier?
Supplies values.
95. Garbage Collection?
Automatic memory management.
96. Memory Leak in Java?
Unused objects retained in memory.
97. What is Reflection?
Examines classes at runtime.
98. Serialization?
Converts object to byte stream.
99. transient keyword?
Excludes field from serialization.
100. What is ClassLoader?
Loads classes into JVM.
101. Scenario: HashMap key mutable?
Avoid mutable keys; retrieval may fail.
102. Scenario: Deadlock in production?
Analyze thread dump and lock ordering.
103. Scenario: Large data processing?
Use streams, batching, efficient collections.
104. Scenario: Concurrent access to map?
Use ConcurrentHashMap.