{"id":26084142,"url":"https://github.com/grootkng/javainterviewquestions","last_synced_at":"2026-05-29T03:31:15.611Z","repository":{"id":103822734,"uuid":"475901038","full_name":"grootkng/JavaInterviewQuestions","owner":"grootkng","description":"A Java and Spring source of interview questions","archived":false,"fork":false,"pushed_at":"2023-04-11T20:49:24.000Z","size":26,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-12-19T10:31:39.038Z","etag":null,"topics":["hacktoberfest","hacktoberfest2022","java"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/grootkng.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2022-03-30T13:52:14.000Z","updated_at":"2022-10-05T14:19:40.000Z","dependencies_parsed_at":null,"dependency_job_id":"a30c5504-f8a1-472b-9422-a3a6875f37bf","html_url":"https://github.com/grootkng/JavaInterviewQuestions","commit_stats":null,"previous_names":["grootkng/javainterviewquestions"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/grootkng/JavaInterviewQuestions","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grootkng%2FJavaInterviewQuestions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grootkng%2FJavaInterviewQuestions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grootkng%2FJavaInterviewQuestions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grootkng%2FJavaInterviewQuestions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/grootkng","download_url":"https://codeload.github.com/grootkng/JavaInterviewQuestions/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grootkng%2FJavaInterviewQuestions/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33635961,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-05-29T02:00:06.066Z","response_time":107,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hacktoberfest","hacktoberfest2022","java"],"created_at":"2025-03-09T04:50:18.589Z","updated_at":"2026-05-29T03:31:15.592Z","avatar_url":"https://github.com/grootkng.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003eA Java and Spring source of interview questions\u003c/h1\u003e\n\n\u003ch2 align=\"center\"\u003eQuestions\u003c/h2\u003e\n\n### Java\n1. What is the difference between string buffer and string builder?  \n```\n  String buffer and StringBuilder both are mutable classes which can be used to do operation on \n  string objects such as reverse of string, concating string and etc. We can modify string without \n  creating a new object of the string. \n  A string buffer is thread-safe whereas string builder is not thread-safe\n```\n\n2. Where do the normal variables, objects store in memory?\n```\n  Stack in java is a section of memory which contains methods, local variables, and reference variables\n  And in Java, all objects are dynamically allocated on Heap\n```\n\n3. Is java 100% OOP language? Explain it.\n```\n  No, because it uses primitive datatypes like int, float, char, double. \n  A pure OOP language can use nothing, but objects i.e everything must be a class. \n```\n\n4. Why do we have different memories like heap memory, stack memory, dynamic memory?\n```\nIn Java, we have different types of memory allocation to store data during program execution. \nEach type of memory serves a specific purpose, and the choice of memory allocation depends on \nthe nature of the data being stored and how long it needs to be stored.\n\n- Stack memory: It is used to store variables that are declared inside a method or \na block of code. The stack is a LIFO (Last-In-First-Out) data structure, which means \nthat the last item to be pushed onto the stack is the first one to be popped off. \nWhen a method is called, a new frame is created on the stack, which contains the local \nvariables and the return address. When the method returns, the frame is removed from the stack.\n\n- Heap memory: It is used to store objects that are dynamically allocated using the \"new\" keyword. \nThe heap is a region of memory that is shared among all threads of the program, and objects can \nbe accessed from any part of the program. The heap is managed by the garbage collector, \nwhich frees up memory that is no longer being used.\n\n- Dynamic memory is typically used for data structures that need to grow or shrink dynamically, \nsuch as arrays and linked lists. When you allocate dynamic memory, the memory is allocated \nfrom the heap, and a pointer to the memory is returned. You can then use this pointer to access the memory.\n\nIn summary, the different types of memory allocation in Java serve different purposes and are \nused to store data that has different lifetimes and scopes. Understanding these memory types is \nessential for writing efficient and reliable Java programs.\n```\n\n5. Why is string immutable? In which memory does it store data?\n```\nThe String is immutable in Java because of the security, synchronization and concurrency, \ncaching, and class loading. The reason of making string final is to destroy the \nimmutability and to not allow others to extend it. The String objects are cached in \nthe String pool, and it makes the String immutable.\n```\n\n6. How is the data stored in HashMap?\n```\nIn Java, a HashMap stores key-value pairs using an array of buckets, where each bucket contains \na linked list of entries. When a new key-value pair is added, the key is hashed to determine the \nbucket index and the new entry is added to the linked list at that index. To retrieve a value for \na given key, the key is hashed to find the corresponding bucket, and the linked list at that index \nis searched for the entry with the matching key. If the number of entries becomes too large, \nthe HashMap automatically resizes the array to maintain performance.\n```\n\n7. What is the difference between HashMap and HashTable?\n```\nIn Java, both HashMap and Hashtable are data structures that store key-value pairs using a hash table. \nHowever, there are some key differences between the two:\n\n- Thread-safety: Hashtable is synchronized, meaning that it is thread-safe, \nwhile HashMap is not synchronized by default. However, you can make a HashMap \nthread-safe by wrapping it using the Collections.synchronizedMap() method.\n\n- Null values: Hashtable does not allow null keys or values, while HashMap allows null keys and values.\n\n- Iteration: The iteration order of Hashtable is not guaranteed, while HashMap does not maintain \ninsertion order by default, but allows you to maintain insertion order using the LinkedHashMap class.\n\n- Performance: HashMap is generally faster than Hashtable, due to its lack of synchronization.\n\nIn summary, while both HashMap and Hashtable are hash table data structures that store key-value pairs, \nHashtable is synchronized and does not allow null keys or values, while HashMap is not synchronized \nby default, allows null keys and values, and has a more flexible iteration and ordering options.\n```\n\n8. What is the difference between HashSet and TreeSet?\n9. Can we extend a class to interface?\n```\nIn Java, you cannot extend a class to an interface, but you can implement an interface in a class.\n\nWhen you extend a class in Java, you are creating a subclass that inherits the properties and\nmethods of the parent class. In contrast, when you implement an interface, you are defining a \ncontract that the implementing class must follow, by providing implementations for all the methods defined in the interface.\n\nInterfaces are a way to achieve abstraction and polymorphism in Java, allowing you to define a \nset of methods that must be implemented by any class that wants to conform to the interface. \nBy implementing an interface, a class can provide a common behavior that can be used by different parts of the program.\n\nIn summary, you can implement an interface in a class in Java, but you cannot extend a class to an interface.\n```\n\n10. How will you clone the object in java? Which interface is used? Which method is used for cloning?\n12. What is the use of java streams? Why should we use streams?\n13. Why main function has a public static void? What does it mean?\n14. What is the difference between errors and exceptions?\n```\nIn Java, errors and exceptions are both types of throwable objects that can be used to handle \nunexpected or exceptional situations in a program. However, there are some differences between the two:\n\n- Cause: Errors are typically caused by problems that are outside the control of the program, \nsuch as hardware failures, out-of-memory errors, or stack overflow errors. Exceptions, on the other hand, \nare typically caused by problems that are within the control of the program, such as invalid input, \nmissing resources, or logic errors.\n\n- Handling: Errors are usually not recoverable, and they typically indicate a severe problem that \nrequires the program to terminate. As such, errors are usually not caught or handled by the program, \nand they are usually reported to the system or user in some way. Exceptions, on the other hand, \nare usually recoverable, and they can be caught and handled by the program using try-catch blocks or other mechanisms.\n\n- Hierarchy: Errors and exceptions are both subclasses of the throwable class, but they are located at \ndifferent levels in the hierarchy. Errors are located at the top of the hierarchy, \nand they are subclasses of the error class. Exceptions are located below errors, \nand they are further divided into checked exceptions and unchecked exceptions.\n\n- Checked vs. Unchecked: Checked exceptions are exceptions that must be caught or declared in the method \nsignature using the throws keyword, and they typically indicate a recoverable problem that the program can handle. \nUnchecked exceptions are exceptions that do not need to be caught or declared, and they typically indicate a more \nsevere or unexpected problem that the program cannot handle.\n\nIn summary, errors are typically caused by problems outside the control of the program and are not recoverable, \nwhile exceptions are typically caused by problems within the control of the program and are usually recoverable. \nErrors are usually not handled by the program, while exceptions can be caught and handled using try-catch blocks.\n```\n\n15. What is the difference between static and non-static context?\n```\nStatic context refers to methods and variables that are associated with the class itself, \nwhile non-static context refers to methods and variables that are associated with instances of the class. \nIn a static context, only static methods and variables can be accessed directly, \nand they are initialized when the class is loaded into memory. In a non-static context, \nboth static and non-static methods and variables can be accessed directly, \nand they are initialized when an instance of the class is created.\n```\n\n16. Can you name some synchronized collections?\n17. What is the difference between the sleep and wait for method in a thread? Which method is invoked once a thread is waiting?\n\n### Spring\n1. What is the difference between @Controller and @RestController?\n```\nIn the Spring Framework, @Controller and @RestController are used to define classes \nthat handle HTTP requests. However, there are some key differences between the two:\n\n- @Controller is used to define a class that handles HTTP requests and returns a view, \ntypically in the form of an HTML page. @RestController is used to define a class that handles \nHTTP requests and returns data in the form of JSON, XML, or any other format that can be consumed by another application.\n\n- With @Controller, the method that handles the HTTP request must return a view name, \nwhich is resolved by the view resolver to a concrete view. With @RestController, \nthe method that handles the HTTP request typically returns a data object that is automatically \nconverted to the requested format (e.g. JSON) using a message converter.\n\n- @RestController is a combination of @Controller and @ResponseBody annotations. The @ResponseBody \nannotation is used to indicate that the method return value should be bound to the web response body. \nTherefore, @RestController eliminates the need to annotate every request handling method of the controller class with @ResponseBody.\n\nIn summary, @Controller is used to handle HTTP requests and return views, while @RestController \nis used to handle HTTP requests and return data in the form of JSON or any other format that can be consumed by another application.\n```\n\n2. What is the @Transaction in Spring?\n3. What is the use of @ControllerAdvice in Spring?\n4. What is the difference between spring MVC and Spring boot?\n```\nSpring MVC and Spring Boot are both frameworks developed by the Spring community for building \nweb applications in Java. However, there are some key differences between the two:\n\n- Configuration: Spring MVC requires a lot of manual configuration to set up a web application, \nincluding configuring the dispatcher servlet, view resolver, database connection, and more. \nIn contrast, Spring Boot reduces the amount of boilerplate configuration required by providing \nauto-configuration that detects and configures many of the components needed to build a web application.\n\n- Embedded Server: Spring Boot provides an embedded server (Tomcat, Jetty, or Undertow) \nthat is included in the application. This allows developers to quickly and easily run and deploy the \napplication without the need for an external web server. Spring MVC, on the other hand, \nrequires an external web server to run the application.\n\n- Convention over Configuration: Spring Boot follows the \"convention over configuration\" principle, \nwhich means that it uses sensible defaults and conventions to minimize the need for explicit configuration. \nSpring MVC, on the other hand, requires more explicit configuration.\n\n- Dependency Management: Spring Boot provides dependency management out-of-the-box, which means \nthat it automatically manages the versions of all dependencies used in the application. \nSpring MVC, on the other hand, requires developers to manage dependencies manually.\n\nIn summary, Spring MVC is a framework for building web applications that requires a lot of manual \nconfiguration, while Spring Boot is a framework that provides auto-configuration, an embedded server, \nand follows the \"convention over configuration\" principle to reduce the amount of boilerplate code needed to build a web application.\n```\n\n5. What is the use of @SpringBootApplication?\n```\n@SpringBootApplication is a convenience annotation provided by the Spring Boot framework \nthat combines several other annotations commonly used in Spring applications. It is used to enable the \nSpring Boot features, such as auto-configuration, component scanning, and more.\n\nHere are the annotations that @SpringBootApplication combines:\n\n- @Configuration: This annotation is used to indicate that the class declares one or \nmore @Bean methods that are used to configure the Spring application context.\n\n- @EnableAutoConfiguration: This annotation is used to enable Spring Boot's auto-configuration mechanism. \nThis mechanism automatically configures the Spring application based on the dependencies on the classpath.\n\n- @ComponentScan: This annotation is used to enable component scanning in the Spring application context. \nIt allows Spring to scan and detect the beans (components, services, repositories, etc.) in \nyour application and make them available for dependency injection.\n\nBy using @SpringBootApplication, you can avoid manually configuring all of these annotations in your \napplication. It is a shortcut to enable all the commonly used features of Spring Boot and helps to reduce boilerplate code.\n\nIn summary, @SpringBootApplication is a convenience annotation that combines several other commonly \nused annotations in Spring applications. It is used to enable Spring Boot's features, \nsuch as auto-configuration, component scanning, and more.\n```\n\n6. What is Springboot Actuator?\n7. How do you communicate between services in two different boot services?\n8. Can we have two @ControllerAdvice in one project?\n9. What are the propagations and isolations levels do you know in Spring?\n10. Explain the difference between Spring Boot and Spring framework\n```\nSpring Boot is a more opinionated, lightweight, and streamlined version of the Spring Framework that \nprovides auto-configuration, an embedded server, and dependency management to reduce the amount of \nboilerplate code needed to build a web application. Spring Framework is a more comprehensive and complex \nframework that provides a wide range of features and capabilities for building enterprise-level applications, \nbut requires more manual configuration and management.\n```\n\n11. What is @Qualifier used for in Spring?\n\n### Hibernate\n1.  What is the use of @Lazy and @Eager in hibernate?\n2. What are the design patterns used in hibernate framework?\n```\nHibernate is a popular Object-Relational Mapping (ORM) framework that provides a set of design \npatterns to simplify the database interaction and management in Java applications. \nHere are some of the design patterns used in Hibernate:\n\n- Factory Method Pattern: Hibernate uses a SessionFactory to create Session instances, \nwhich represent a connection to the database. The SessionFactory is created using the Factory Method Pattern, \nwhich allows for flexibility and customization in creating the SessionFactory.\n\n- Proxy Pattern: Hibernate uses the Proxy Pattern to implement lazy loading of data. \nWhen a persistent object is loaded, Hibernate creates a proxy object that represents the persistent object. \nThe proxy object does not load the data until it is needed, which improves performance.\n\n- DAO Pattern: Hibernate encourages the use of the Data Access Object (DAO) Pattern to separate the \ndatabase access logic from the business logic of the application. The DAO Pattern provides a simple \ninterface for accessing the database, which can be easily replaced with a different implementation if needed.\n\n- Unit of Work Pattern: Hibernate uses the Unit of Work Pattern to track changes to persistent objects and \nmanage transactions. The Unit of Work Pattern ensures that changes to persistent objects are \nsynchronized with the database and that transactions are atomic.\n\n- Template Method Pattern: Hibernate provides a template for performing common database operations, \nsuch as CRUD (Create, Read, Update, Delete) operations. The template uses the Template Method Pattern to \ndefine the overall structure of the operation, while allowing for customization of individual steps.\n\nIn summary, Hibernate uses a variety of design patterns, including the Factory Method Pattern, \nProxy Pattern, DAO Pattern, Unit of Work Pattern, and Template Method Pattern, \nto simplify database interaction and management in Java applications.\n```\n\n3. How to enable the second-level cache?\n```\nTo enable the second-level cache in Hibernate, you need to follow these steps:\n\n- Add the necessary cache provider dependencies to your project.\n- Configure the cache provider and the cache region in your Hibernate configuration file.\n- Annotate the entity classes you want to cache with the @Cacheable annotation.\n- Enable caching for queries by setting the hibernate.cache.use_query_cache configuration property to true.\n\nOnce the second-level cache is enabled, Hibernate will cache the entity data and query results in memory, \nreducing the number of database queries and improving performance.\n```\n\n### Database | SQL\n1. What is View in POSTGRESQL | Materialized View in POSTGRESQL | Refresh Materialized view | SQL Views?\n```\n  In a database, a view is the result set of a stored query on the data, which the \n  database users can query just as they would in a persistent database collection object. \n  This pre-established query command is kept in the database dictionary\n```\n\n2. What is the difference between vertical partitioning vs horizontal partitioning in database?\n3. Hany types of indexes are there? What are they? When to use what? Is indexing DBA Job or Developer job?\n4. What is the best way to modify the existing composite key in SQL?\n5. Consider if any update is taking time. What are the consequences that are causing performance issues? How can you avoid it?\n```\nIf updates are taking a long time to complete, it can cause several performance issues in an application:\n\n- Increased response time: Slow updates can increase the response time of the application, \nwhich can lead to a poor user experience.\n\n- Locking and blocking: If updates are locking the database table or blocking other transactions, \nit can cause contention and reduce concurrency, leading to further performance issues.\n\n- Increased resource usage: Slow updates can increase the CPU, memory, and disk usage of the application \nserver and database server, reducing overall system performance.\n\nTo avoid these issues and improve the performance of updates, you can try the following techniques:\n\n- Optimize database design: Make sure the database schema is properly designed and normalized, \nand that indexes are created on the appropriate columns to speed up queries.\n\n- Optimize SQL queries: Ensure that SQL queries are optimized by using proper syntax, \navoiding unnecessary joins or subqueries, and using indexed columns in the WHERE clause.\n\n- Batch updates: If you are updating a large number of records, \nconsider using batch updates to minimize the number of round trips to the database.\n\n- Use caching: Consider caching frequently accessed data in memory to reduce the number of database queries.\n\n- Use asynchronous processing: If updates are taking a long time to complete, \nconsider using asynchronous processing techniques such as background tasks or queues to avoid blocking the application.\n\nBy applying these techniques, you can improve the performance of updates and avoid performance issues in your application.\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgrootkng%2Fjavainterviewquestions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgrootkng%2Fjavainterviewquestions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgrootkng%2Fjavainterviewquestions/lists"}