Spring Framework Practice Test — Questions and Answers
Question 1: What is the meaning of spring?
- Spring is a development framework for .Net applications.
- Spring is an open source development framework for enterprise Java. (Correct answer)
- Spring is a development framework for PHP based applications.
- Spring is a proprietary framework.
Correct answer: Spring is an open source development framework for enterprise Java.
The Spring Framework is a widely adopted, open-source application framework specifically designed for the Java platform. It provides comprehensive infrastructure support for developing robust enterprise-level Java applications, offering features like dependency injection, aspect-oriented programming, and integration with various technologies to simplify development.
Question 2: Spring is licensed under a
- GNU Lesser General Public License
- Open source
- GNU
- Apache License 2.0 (Correct answer)
Correct answer: Apache License 2.0
The Spring Framework is licensed under the Apache License, Version 2.0. This is a permissive free software license that allows users to freely use, modify, and distribute the software, even for commercial purposes, while requiring attribution and preservation of copyright notices, making it a popular choice for open-source projects.
Question 3: In Spring, what are the IoC containers?
- BeanFactory, ApplicationContext, IocContextFactory
- BeanFactory, ApplicationContext, BeanContext
- BeanFactory, ApplicationContext (Correct answer)
- BeanFactory, BeanContext, IocContextFactory
Correct answer: BeanFactory, ApplicationContext
In Spring, the primary Inversion of Control (IoC) containers are the BeanFactory and the ApplicationContext. The BeanFactory provides the basic functionality for managing beans, while the ApplicationContext is an enhanced version that builds upon BeanFactory, offering additional enterprise-specific features like internationalization, event publishing, and declarative transaction management.
Question 4: Spring has been created by
- Daniel Fernandez
- Apache
- Pivotal Software (Correct answer)
- Red Hat Software
Correct answer: Pivotal Software
The Spring Framework was initially created by Rod Johnson. Over time, its development and stewardship evolved, and it was primarily maintained and supported by Pivotal Software. Pivotal Software was later acquired by Broadcom's VMware Tanzu division, but Pivotal Software is recognized as the key entity responsible for its development and advancement among the given choices.
Question 5: What is the scope of a request?
- This scopes the bean definition to HTTP Session
- This scopes a bean definition to an HTTP request. (Correct answer)
- This scopes the bean definition HTTP Application/ Global session.
- This scopes the bean definition to Spring IoC container.
Correct answer: This scopes a bean definition to an HTTP request.
In Spring, the 'request' scope dictates that a new instance of a bean is created for each incoming HTTP request. This specific bean instance is active and available only for the duration of that particular HTTP request. Once the request completes, the bean instance is destroyed, making it ideal for handling request-specific data.
Question 6: Dependent injection, often known as IOC, is a.. .
- Java Module
- Design Pattern (Correct answer)
- ORM Framework
- Framework
Correct answer: Design Pattern
Dependency Injection (DI), often referred to as a specific implementation of Inversion of Control (IoC), is a fundamental design pattern in software engineering. It promotes loose coupling by allowing an object's dependencies to be provided or 'injected' externally, rather than the object creating them itself, which enhances modularity, testability, and maintainability of code.
Question 7: Which of the following is a suitable replacement for Spring HibernateTemplate?
- HibernateContext
- Hibernate contextual sessions (Correct answer)
- All of the above
- None of the above
Correct answer: Hibernate contextual sessions
Spring's `HibernateTemplate` was deprecated because it tightly coupled Spring with Hibernate's API and often hid underlying `Session` management complexities. Modern Hibernate (3.0+) introduced "contextual sessions," which are managed by Hibernate itself, typically bound to the current transaction or thread. This approach aligns better with the standard Hibernate API and reduces the need for Spring-specific abstractions, making `SessionFactory.getCurrentSession()` the preferred way to obtain a session.
Question 8: What is autowiring Which of these isn't a Spring Module?
- Similar to byType, but type applies to constructor arguments. If there is not exactly one bean of the constructor argument type in the container, a fatal error is raised. (Correct answer)
- Autowiring by property name. Spring tries to match and wire its properties with the beans defined by the same names in the configuration file.
- Spring first tries to wire using autowire by constructor, if it does not work, Spring tries to autowire by byType.
- Autowiring by property type. Spring tries to match and wire a property if its type matches with exactly one of the beans name in configuration file.
Correct answer: Similar to byType, but type applies to constructor arguments. If there is not exactly one bean of the constructor argument type in the container, a fatal error is raised.
Autowiring by constructor attempts to inject dependencies into a bean's constructor. Spring looks for a constructor whose arguments match the types of beans available in the container. If there isn't exactly one bean of a particular argument type, Spring cannot resolve the dependency unambiguously and will throw a fatal error, preventing the application from starting. This ensures strong type safety and explicit dependency resolution.
Question 9: Which of these isn't a Spring Module?
- Spring MVC
- AOP
- HTML/JSP (Correct answer)
- O/R Integration
Correct answer: HTML/JSP
Spring Framework is modular, offering various components like Spring MVC for web applications, AOP for aspect-oriented programming, and O/R Integration for database interaction (e.g., with Hibernate or JPA). HTML/JSP are presentation technologies used *with* Spring MVC, but they are not modules *of* the Spring Framework itself. They are standard web technologies that Spring integrates with.
Question 10: After shutting the spring container, which of the following methods is used to gently end all bean processes?
- shutdownHook (Correct answer)
- session method
- destory method
- None of the above
Correct answer: shutdownHook
The `shutdownHook` method, typically registered via `AbstractApplicationContext.registerShutdownHook()`, allows the Spring container to gracefully shut down. When the JVM exits, this hook is invoked, ensuring that all singleton beans are properly destroyed by calling their configured `destroy` methods. This prevents resource leaks and ensures a clean application termination.
Question 11: What are the different types of advice?
- Before, after, after-returning, after-throwing, around (Correct answer)
- then, after, after-returning, after-throwing, around
- When, after, after-returning, around
- Where, after, after-returning, after-throwing, around
Correct answer: Before, after, after-returning, after-throwing, around
In Spring AOP (Aspect-Oriented Programming), advice defines the action an aspect takes at a particular join point. The five main types of advice are: `Before` (executes before a method), `After` (executes after a method, regardless of outcome), `After-returning` (executes after a method completes successfully), `After-throwing` (executes if a method exits by throwing an exception), and `Around` (surrounds a method call, allowing custom behavior before and after).
Question 12: Which of the following statements is correct?
- BeanFactory extends ApplicationContext
- ApplicationContext implements BeanFactory
- BeanFactory implements ApplicationContext
- ApplicationContext extends BeanFactory (Correct answer)
Correct answer: ApplicationContext extends BeanFactory
The `ApplicationContext` is Spring's advanced container, building upon the basic functionality of the `BeanFactory`. It provides more enterprise-specific features like AOP integration, message resource handling, event publication, and web application contexts. Therefore, `ApplicationContext` extends `BeanFactory`, inheriting its core bean management capabilities while adding significant enhancements.
Question 13: What is the scope of a bean in the context of a portlet?
- request
- prototype
- session
- global-session (Correct answer)
Correct answer: global-session
In a portlet environment, the `global-session` scope is specific to Spring WebFlux or Spring MVC applications running in a portlet container. It defines a bean's lifecycle to be shared across all portlets that are part of the same global session. This means the bean is available for the duration of the portlet application's global session, which is distinct from a standard HTTP session.
Question 14: What is the definition of the Join Point?
- This represents a point in your object where you join injected values.
- This represents a point in your application which joins two objects.
- This represents a point in your application where you can plug-in AOP aspect. (Correct answer)
- This represents a point in your object where you join values
Correct answer: This represents a point in your application where you can plug-in AOP aspect.
In Aspect-Oriented Programming (AOP), a join point is a specific point during the execution of a program, such as a method call, method execution, or field access. It is a candidate point where an aspect's advice can be applied. Spring AOP primarily supports method execution join points, allowing developers to "plug in" cross-cutting concerns at these well-defined points.
Question 15: By default, beans defined in the spring framework are..
- Final
- Abstract
- initialized
- Singleton (Correct answer)
Correct answer: Singleton
By default, Spring beans are singletons, meaning that the Spring container creates exactly one instance of the bean definition per container. All requests for that bean will return the same shared instance. This is a common and efficient default, as it reduces object creation overhead and promotes stateless services.
Question 16: Is it possible to integrate Struts and Spring?
- A) No
- B) Yes (Correct answer)
Correct answer: B) Yes
Yes, it is entirely possible to integrate Struts and Spring. Spring provides integration modules and mechanisms, such as `ContextLoaderPlugIn` for Struts 1 or listeners for Struts 2, to allow Struts actions to be managed as Spring beans. This enables developers to leverage Spring's dependency injection and other features within a Struts application.
Question 17: Which version of the spring framework introduced the Expression Language/ SpEL?
- 3.0 (Correct answer)
- 1.0
- 4.0
- 2.0
Correct answer: 3.0
Spring Expression Language (SpEL) was introduced with Spring Framework 3.0. SpEL is a powerful expression language that supports querying and manipulating an object graph at runtime. It can be used in various Spring components, such as XML and annotation-based configurations, to provide dynamic values and logic.
Question 18: What are the various kinds of bean injections?
- getter and setter
- constructor and setter (Correct answer)
- setter,getter and constructor
- constructor and getter
Correct answer: constructor and setter
Spring primarily supports two main types of dependency injection: constructor injection and setter injection. Constructor injection involves providing dependencies through a bean's constructor, ensuring that the bean is fully initialized upon creation. Setter injection involves providing dependencies through public setter methods, allowing for optional dependencies or later modification.
Question 19: What are the ways to use Spring to access Hibernate?
- Extending HibernateDAOSupport and Applying an AOP Interceptor node.
- Inversion of Control with a Hibernate Template and Callback.
- A and B (Correct answer)
- None of these
Correct answer: A and B
Spring provides multiple ways to integrate with Hibernate. One common approach is to extend `HibernateDAOSupport`, which provides convenient methods for interacting with Hibernate `SessionFactory`. Another powerful method involves using `HibernateTemplate` (though now deprecated in favor of contextual sessions) with a `Callback` interface, or more generally, using Inversion of Control (IoC) to manage Hibernate `SessionFactory` and `Session` objects, often combined with AOP for transaction management.
Question 20: What are the different types of autowire?
- byName , byType, constructor and autocorrect
- byName , byType, destructor and autodetect
- byName , byType, constructor and autodetect (Correct answer)
- byName , byMethod, constructor and autodetect
Correct answer: byName , byType, constructor and autodetect
Spring supports several autowiring modes to automatically resolve and inject dependencies. These include `byName` (matches bean properties by name), `byType` (matches bean properties by type), `constructor` (matches constructor arguments by type), and `autodetect` (Spring first tries `constructor` autowiring, then `byType`). These modes simplify configuration by reducing explicit dependency declarations.
Question 21: Before the initialization callback, which method is used to process the bean?
- postProcessBeforelnitialization() (Correct answer)
- postProcessAfterlnitialization()
- it's own constructor
- scope
Correct answer: postProcessBeforelnitialization()
The `postProcessBeforeInitialization()` method is part of the `BeanPostProcessor` interface in Spring. This method is invoked by the Spring container *before* any initialization callbacks (like `InitializingBean.afterPropertiesSet()` or a custom `init-method`) are called for a bean. It allows for custom logic or modifications to a bean instance before it is fully initialized.
Question 22: The following class can be used to build a custom spring event.
- ApplicationEvent (Correct answer)
- SpringEvent
- Event
- None of above
Correct answer: ApplicationEvent
To create a custom event in Spring, you need to extend the `ApplicationEvent` class. This class serves as the base for all events published by the `ApplicationContext`. By extending `ApplicationEvent`, your custom event can be published and listened to by `ApplicationListener` implementations within the Spring event publishing mechanism.
Question 23: Which tag is permitted by the static field?
- constructor-args
- util:constant (Correct answer)
- set
- list
Correct answer: util:constant
The `<util:constant>` tag, part of Spring's `util` schema, is used to expose a static field or enum value as a Spring bean. This allows you to easily reference static constants from your configuration or other beans, without having to define a separate bean for them. It's a convenient way to access static members.
Question 24: Which element is used to specify bean method access attributes?
- securityzprotect (Correct answer)
- securityzintercept-security
- securityzintercept
- none of above
Correct answer: securityzprotect
In Spring Security, the `<security:protect>` element (or `@Secured` annotation) is used to define access control attributes for specific methods of a bean. This allows you to specify roles or permissions required to invoke those methods, enforcing security at the method level. It's a key component for securing application services.
Question 25: The PostageServiceBean has an annotation that decorates it.
- @lnterceptors (Correct answer)
- @Autowire
- @Mention
- @Wire
Correct answer: @lnterceptors
The `@Interceptors` annotation, typically from the Java EE (JSR-250) or CDI specification, is used to declare interceptors for a class or method. While Spring AOP uses its own mechanisms, Spring can integrate with JSR-250 annotations. For a `PostageServiceBean`, `@Interceptors` would specify which interceptor classes should apply to its methods, allowing for cross-cutting concerns like logging or transaction management.
What is the meaning of spring?