Skip to content

This repository includes simple explanations, real-world examples, and best-practice answers for Core Java, OOP, Collections, JVM internals, Exception Handling, Multithreading, Java 8 features, Spring Boot, and more.

Notifications You must be signed in to change notification settings

softwaredeveloperhub01/Java-Interview-Questions-and-Answers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

Java Interview Questions and Answers

Java Interviews – Top Questions & Answers for Web Developers


1. What is Java?

Java is an object-oriented, platform-independent programming language used to build scalable, secure, and portable applications.


2. Why is Java platform-independent?

Because Java code is compiled into bytecode, which runs on the JVM, not directly on the OS.


3. What is the JVM?

JVM (Java Virtual Machine) executes Java bytecode. It provides memory management and OS abstraction.


4. What is the JDK?

JDK is a development kit containing JVM + JRE + compiler + tools for building Java applications.


5. What is the JRE?

JRE provides the environment required to run Java apps (JVM + libraries), but not compile them.


6. What is bytecode?

A platform-neutral intermediate code executed by the JVM.


7. What is OOP?

Object-Oriented Programming organizes code using objects, enabling reusability and modular design.


8. Explain four pillars of OOP.

  • Encapsulation – hiding internal details
  • Inheritance – reusing parent properties
  • Polymorphism – many forms (method overloading/overriding)
  • Abstraction – exposing only essentials

9. What is encapsulation?

Binding data and methods together while restricting direct access using private fields and getters/setters.


10. What is inheritance?

Acquiring properties of a parent class using extends.


11. What is polymorphism?

Ability of the same method to behave differently. Types: compile-time (overloading) and runtime (overriding).


12. What is abstraction?

Hiding complexity with abstract classes or interfaces.


13. Difference between abstract class and interface?

Feature Abstract Class Interface
Methods abstract + concrete abstract + default/static
Multiple Inheritance No Yes
Constructor Yes No

14. What is method overloading?

Same method name, different parameters.


15. What is method overriding?

Same method signature in child class, providing new behavior.


16. What is a constructor?

A special method that initializes objects when created.


17. What is a default constructor?

A no-argument constructor provided by Java if no other constructors exist.


18. Can a constructor be private?

Yes. Used in singletons and factory patterns.


19. What is the super keyword?

Used to access parent class methods/constructors.


20. What is the this keyword?

Refers to the current object; resolves naming conflicts.


21. What is a static method?

A method belonging to the class, not objects.


22. Can we override static methods?

No. They can be hidden, not overridden.


23. What is a final variable?

A constant whose value cannot change.


24. What is a final class?

A class that cannot be extended.


25. What is a final method?

A method that cannot be overridden.


26. What is garbage collection?

Automatic memory cleanup for unused objects.


27. How to request garbage collection?

System.gc() (only a suggestion to JVM).


28. What is the difference between == and .equals()?

  • == compares references
  • .equals() compares values

29. What is a package?

A namespace for organizing Java classes.


30. What is the default package?

Unnamed package when no package is declared.


31. What is an exception?

An event that interrupts normal program execution.


32. Types of exceptions?

  • Checked
  • Unchecked
  • Errors

33. What is a checked exception?

Exceptions checked at compile time. Example: IOException.


34. What is an unchecked exception?

Runtime exceptions like NullPointerException.


35. What is try-catch?

Block used to handle exceptions gracefully.


36. What is finally?

A block that always executes, even if exceptions occur.


37. What is a custom exception?

User-defined exception extending Exception class.


38. What is multithreading?

Executing multiple tasks concurrently within a program.


39. What is a thread?

A lightweight subprocess managed by JVM.


40. Ways to create a thread?

  • Extending Thread
  • Implementing Runnable

41. What is thread synchronization?

Mechanism to prevent simultaneous access to shared resources.


42. What is a synchronized block?

Ensures only one thread executes within the block.


43. What is the thread lifecycle?

New → Runnable → Running → Blocked/Waiting → Terminated


44. What is deadlock?

Two threads wait on each other’s resources and never finish.


45. What is a daemon thread?

Background service thread (e.g., garbage collector).


46. What is an immutable object?

Object whose state cannot change after creation (e.g., String).


47. How to create immutable classes?

  • Mark class final
  • Make fields private
  • Provide getters only
  • No setters

48. Why are Strings immutable?

For security, caching, and thread-safety.


49. Difference between String, StringBuilder, StringBuffer?

Type Mutable Thread Safe Speed
String No Yes Slow
StringBuilder Yes No Fast
StringBuffer Yes Yes Slower

50. What is the String pool?

JVM-managed area storing string literals to save memory.


51. What is the Java Collections Framework?

A set of classes/interfaces for storing and processing data.


52. Difference between List and Set?

  • List: ordered, allows duplicates
  • Set: unordered, no duplicates

53. Difference between ArrayList and LinkedList?

Feature ArrayList LinkedList
Structure Dynamic array Doubly linked list
Speed Fast read Fast insert/delete

54. Difference between HashSet and TreeSet?

  • HashSet: unordered
  • TreeSet: sorted

55. Difference between HashMap and TreeMap?

  • HashMap: unordered, faster
  • TreeMap: sorted keys

56. What is a Map?

A collection storing key-value pairs.


57. Can Map have duplicate keys?

No. Only one value per key.


58. What is Iterator?

Used to iterate collections safely.


59. What is fail-fast?

Collection throws error when modified during iteration.


60. What is fail-safe?

Iterator works on a copy and avoids exceptions.


61. What is Java Stream API?

Functional-style operations on collections.


62. What is lambda expression?

A short way to write anonymous functions.


63. What is functional interface?

Interface with exactly one abstract method (e.g., Runnable).


64. What is Optional?

A container to avoid NullPointerException.


65. What is method reference?

Shortcut for calling methods using :: operator.


66. What is the difference between parallelStream() and stream()?

  • parallelStream() uses multiple threads
  • stream() is sequential

67. What is JDBC?

Java Database Connectivity for connecting Java apps with databases.


68. Steps in JDBC connection?

  1. Load driver
  2. Open connection
  3. Create statement
  4. Execute query
  5. Close resources

69. What is connection pooling?

Reusing database connections to improve performance.


70. What is a servlet?

Java class handling web requests on a server.


71. What is JSP?

Java Server Pages for embedding Java in HTML.


72. What is a session?

Server-side user information stored across requests.


73. What is a cookie?

Small piece of client-side data stored by the browser.


74. What is REST API?

A lightweight architectural style using HTTP for communication.


75. What is JSON?

Data exchange format used widely in REST APIs.


76. What is Spring Framework?

A modern framework for building enterprise Java applications.


77. What is Spring Boot?

Spring with auto-configuration and fast setup.


78. What is dependency injection?

Injecting required objects instead of creating them manually.


79. What is Inversion of Control?

Framework manages object creation instead of the program.


80. What is a Spring Bean?

Object managed by the Spring container.


81. What is Spring Boot Starter?

Pre-configured dependencies for faster development.


82. What is Spring Data JPA?

Simplifies database access using repository interfaces.


83. What is Hibernate?

ORM tool mapping Java objects to database tables.


84. What is lazy loading?

Loading related data only when accessed.


85. What is eager loading?

Loading related data immediately.


86. What are ACID properties?

  • Atomicity
  • Consistency
  • Isolation
  • Durability

Ensures reliable database transactions.


87. What is serialization?

Converting an object into a byte stream.


88. What is deserialization?

Reconstructing object from byte stream.


89. What is the transient keyword?

Prevents a variable from being serialized.


90. What is reflection?

Inspecting classes, methods, fields at runtime.


91. What is an annotation?

Metadata providing instructions to compiler/JVM.


92. What is generics?

Type-safe programming avoiding casting issues.


93. What is autoboxing?

Automatic conversion between primitives and wrapper classes.


94. What is unboxing?

Reverse of autoboxing.


95. Why are wrapper classes needed?

Collections only store objects, not primitives.


96. What is the volatile keyword?

Ensures variable visibility across threads.


97. What is the synchronized keyword?

Prevents concurrent access to a block or method.


98. What is a classloader?

Loads Java classes into JVM memory.


99. What is the difference between heap and stack memory?

  • Heap: stores objects
  • Stack: stores method calls, local variables

100. Why is Java widely used?

Because it’s stable, secure, scalable, portable, and has a massive ecosystem.


About

This repository includes simple explanations, real-world examples, and best-practice answers for Core Java, OOP, Collections, JVM internals, Exception Handling, Multithreading, Java 8 features, Spring Boot, and more.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published