Spring Certified Professional (2024) Exam-style mock test
Spring Certified Professional (2024) Practice Exams|Practice test
Full explanations
Exam format
Pass line 76%
33 USD One-time · lifetime access
Enroll →
Sample questions
Example1 · Spring Core & Dependency Injection
At a B2B SaaS company, a developer builds a billing module and defines a @Configuration class. They write a @Bean method named invoiceService() that internally calls another @Bean method, taxCalculator(), to wire the dependency. The team wants every direct call to taxCalculator() to return the same shared singleton instance managed by the container. Which behavior of the configuration class makes this inter-bean reference return the shared singleton?
✓Spring proxies the @Configuration class via CGLIB so the method call is intercepted and routed to the container
Correct. Full @Configuration classes are proxied with CGLIB, so inter-bean method calls are intercepted and delegated to the container, returning the managed singleton rather than a new instance.
・Spring caches the bean only when the method is annotated with the @Lazy marker on it
Incorrect. @Lazy controls when a bean is instantiated, not whether inter-bean method calls return the shared singleton; it is unrelated to this interception behavior.
・Spring reflectively rewrites the bytecode of each @Bean method at compile time to add caching
Incorrect. Spring does not rewrite @Bean bytecode at compile time; the singleton sharing comes from a runtime CGLIB proxy, not a compile-time transformation.
・Spring requires the method to be declared static so the container can share a single result across calls
Incorrect. Making the method static actually opts out of the CGLIB interception, so a static @Bean method would not return the container-managed shared instance through self-invocation.
Explanation
Classes annotated with @Configuration are enhanced by a CGLIB subclass proxy, which intercepts @Bean method calls so that inter-bean references resolve to the container's managed singleton.
Example2 · Spring Boot & Auto-Configuration
A developer at a B2B SaaS invoicing company creates a new microservice and annotates the main class with @SpringBootApplication. During a code review, a teammate asks which three annotations this single annotation combines. What is the correct answer?
・It combines @EnableScheduling for cron tasks, @ComponentScan for beans, and @Configuration for central wiring of the service
Incorrect. @EnableScheduling is not part of the composed annotation; it must be added explicitly when scheduled tasks are required.
✓It combines @Configuration, @EnableAutoConfiguration, and @ComponentScan into one convenient meta-annotation
Correct. @SpringBootApplication is a meta-annotation that bundles @Configuration, @EnableAutoConfiguration, and @ComponentScan.
・It combines @RestController, @EnableAutoConfiguration, and @ComponentScan so controllers are discovered automatically
Incorrect. @RestController is a web stereotype that is never implied by @SpringBootApplication on the main class.
・It combines @Configuration and @ComponentScan, while auto-configuration must still be enabled separately by the team
Incorrect. @EnableAutoConfiguration is also included, so auto-configuration does not need to be enabled separately.
Explanation
@SpringBootApplication composes @Configuration, @EnableAutoConfiguration, and @ComponentScan, which is why a single annotation bootstraps the whole application.
Example3 · Data Access & Transactions
A B2B SaaS billing service connects to PostgreSQL. The team wants Spring Boot to auto-configure a pooled DataSource from properties without declaring a @Bean, and HikariCP is already on the classpath. Which approach lets Spring Boot wire the DataSource correctly?
✓Declare the spring.datasource.url, username, password, and driver-class-name properties and let auto-configuration build the pooled DataSource for you
Correct. With a pooling provider like HikariCP on the classpath, Spring Boot auto-configures a pooled DataSource directly from the spring.datasource.* properties, so no @Bean is required.
・Annotate the main application class with @EnableDataSource and list the JDBC connection values inside that annotation's attributes
Incorrect. There is no @EnableDataSource annotation in Spring; DataSource auto-configuration is driven by classpath detection and spring.datasource.* properties.
・Define a DataSourceBuilder @Bean in a @Configuration class and manually call build() with hard-coded connection strings for each environment
Incorrect. A manual DataSourceBuilder @Bean works but it is not what is asked for; it also bypasses the requested property-driven auto-configuration and hard-codes values.
・Register a plain DriverManagerDataSource @Bean so every repository opens its own direct connection to PostgreSQL without pooling
Incorrect. DriverManagerDataSource creates a new connection per request with no pooling, which defeats the goal of a pooled DataSource.
Explanation
Spring Boot auto-configures a pooled DataSource from spring.datasource.* properties when a pool implementation such as HikariCP is present, with no explicit bean needed.
Example4 · Spring MVC & REST
A healthcare services firm exposes a controller endpoint that must respond only to HTTP GET requests at the path /patients/{id}. The team wants the most concise, idiomatic Spring MVC annotation on the handler method. Which annotation should they place on the method?
・Annotate the method with @RequestMapping(value = "/patients/{id}", method = RequestMethod.GET) to bind the path and verb
Incorrect. This works functionally but is more verbose than the composed annotation the team specifically asked for as the idiomatic choice.
✓Annotate the method with @GetMapping("/patients/{id}") so it handles only GET requests for that path
Correct. @GetMapping is the composed shortcut for @RequestMapping with method = GET, making it the concise idiomatic way to handle GET for that path.
・Annotate the method with @RequestMapping(path = "/patients/{id}") and rely on the default HTTP verb behavior
Incorrect. Plain @RequestMapping without a method defaults to handling all HTTP verbs, so it would not restrict the endpoint to GET only.
・Annotate the method with @PostMapping("/patients/{id}") since retrieving a patient reads server-side state
Incorrect. @PostMapping binds POST requests, which contradicts the requirement that the endpoint respond only to GET requests.
Explanation
@GetMapping is a composed annotation equivalent to @RequestMapping(method = GET) and is the preferred concise way to map GET requests in modern Spring MVC.
Example5 · Testing, Security & Actuator
A team at a healthcare services firm writes a test for the PatientController web layer only, and they want fast tests that mock the service layer while keeping Spring MVC infrastructure wired. Which annotation should they place on the test class?
✓@WebMvcTest, which loads only the web layer beans and auto-configures MockMvc for the controller under test
Correct. @WebMvcTest restricts the context to MVC web components and auto-configures MockMvc, making controller tests fast while service beans are mocked.
・@SpringBootTest, which starts the complete application context including the full service and persistence layers
Incorrect. @SpringBootTest loads the entire context, which is slower and broader than the web-layer-only slice the team wants here.
・@DataJpaTest, which configures an in-memory database and scans only the repository beans for the slice
Incorrect. @DataJpaTest targets JPA repositories and an embedded database, not the web controller layer under test.
・@JdbcTest, which configures a DataSource and template for lightweight JDBC repository slice testing
Incorrect. @JdbcTest targets JDBC-based persistence slices and does not wire the Spring MVC web layer being tested.
Explanation
@WebMvcTest is the controller slice annotation that auto-configures MockMvc and loads only web-layer beans, ideal for testing PatientController in isolation.
Example6 · Spring Core & Dependency Injection
A healthcare services firm defines a @Configuration class with two @Bean methods, where the patientRepository() method calls the shared auditClient() method directly to wire a dependency. The team expects both beans to share one AuditClient instance. Which mechanism guarantees that calling auditClient() returns the same singleton rather than a new object?
✓Spring subclasses the @Configuration class via CGLIB so inter-bean method calls are intercepted and routed to the managed singleton in the container
Correct. @Configuration classes are proxied with CGLIB, so inter-bean method invocations are intercepted and delegated to the container's managed singleton instance.
・Spring marks every @Bean method as static, which forces the JVM to cache and reuse a single shared return value across all callers
Incorrect. Static @Bean methods are a special case that actually bypasses the proxy-based singleton guarantee rather than enforcing it.
・Spring rewrites the bytecode of each @Bean method at compile time to inline the dependency as a constant field reference
Incorrect. Spring does not perform compile-time bytecode rewriting of @Bean methods to inline constants; wiring is handled at runtime by the container.
・Spring injects the AuditClient through reflection on each field annotated with @Autowired inside the configuration instance
Incorrect. Field @Autowired injection is unrelated to how direct inter-bean method calls resolve to the same singleton.
Explanation
A full @Configuration class is enhanced by a CGLIB subclass that intercepts @Bean method calls, ensuring they return the container-managed singleton instead of a fresh object.
Example7 · Spring Core & Dependency Injection
At the retailer UrbanThread, a @Configuration class defines a @Bean method named inventoryService() and another @Bean method pricingService() that calls inventoryService() directly to obtain its dependency. How does Spring ensure both beans share the same singleton InventoryService instance?
・Spring rejects the direct method call and forces you to pass InventoryService as a method parameter instead
Incorrect. Passing the dependency as a parameter is a valid alternative style, but Spring does not reject the direct inter-bean method call at all.
✓Spring enhances the @Configuration class with CGLIB so the inventoryService() call returns the managed singleton
Correct. A full @Configuration class is subclassed by CGLIB so inter-bean method calls are intercepted and return the container-managed singleton rather than a fresh object.
・Each call to inventoryService() creates a brand-new instance because plain Java method calls bypass the container
Incorrect. That would be true for a plain unmanaged class, but CGLIB enhancement of @Configuration intercepts the call and returns the shared singleton.
・The container caches the result only if you annotate inventoryService() with an explicit @Scope("singleton")
Incorrect. Singleton is already the default scope, and the CGLIB interception, not an explicit @Scope, is what guarantees the shared instance.
Explanation
Classes annotated with @Configuration are proxied by CGLIB, so calls between @Bean methods are intercepted and resolve to the single managed bean instance.
Example8 · Spring Core & Dependency Injection
UrbanThread's CheckoutService requires a mandatory PaymentGateway collaborator, and the team wants the field to be final and the object fully initialized before any method runs. Which injection style best satisfies these requirements?
・Field injection, placing @Autowired directly on the private PaymentGateway field to keep the class body short
Incorrect. Field injection cannot assign a final field and hides the mandatory dependency from the constructor contract.
・Setter injection, exposing a public setter so the gateway can be replaced later at runtime if needed
Incorrect. Setter injection suits optional or swappable dependencies but leaves the object constructible in an incomplete state with non-final fields.
✓Constructor injection, passing PaymentGateway as a constructor argument so the field is final
Correct. Constructor injection lets the field be final and guarantees the dependency is supplied and the object is fully initialized before use.
・Method injection using a @Lookup annotated abstract method that returns a fresh PaymentGateway each call
Incorrect. @Lookup method injection is for obtaining new prototype instances on demand, not for enforcing a single mandatory final collaborator.
Explanation
Constructor injection is the recommended style for mandatory dependencies because it supports final fields and ensures the bean is never used in a partially constructed state.
Example9 · Spring Core & Dependency Injection
UrbanThread has two beans implementing ShippingCalculator: standardShipping and expressShipping. A component autowires a single ShippingCalculator and startup fails with NoUniqueBeanDefinitionException. The team wants expressShipping chosen by default everywhere unless a bean explicitly asks otherwise. What should they do?
・Delete the standardShipping bean definition entirely so that only one ShippingCalculator candidate remains in the context
Incorrect. Removing the bean loses needed functionality; the goal is to keep both but make one the default.
✓Annotate the expressShipping bean definition with @Primary so it becomes the default autowiring candidate
Correct. @Primary designates a preferred candidate that wins autowiring by default while still allowing @Qualifier to select the other.
・Add @Qualifier("standardShipping") on every injection point across the whole application to remove the ambiguity
Incorrect. That forces the wrong default and requires annotating many points instead of declaring one preferred bean.
・Mark both beans with @Scope("prototype") so Spring creates a separate instance and the conflict disappears
Incorrect. Changing the scope does not resolve the type ambiguity; two matching candidates still exist for autowiring.
Explanation
@Primary marks one of several type-matching candidates as the default for autowiring, cleanly resolving the ambiguity without removing beans.
Example10 · Spring Core & Dependency Injection
A payments platform at NorthBank defines a @Configuration class with two @Bean methods, where the fraudCheck() method internally calls the auditLog() method to obtain its dependency. During testing the team observes that a single shared AuditLog instance is injected everywhere. In standard Spring, why does calling auditLog() from fraudCheck() return the same managed singleton rather than a brand-new object?
✓Spring proxies the @Configuration class by default so the inter-bean method call is intercepted and routed to the container's singleton cache
Correct. With the default proxyBeanMethods behavior Spring subclasses the @Configuration class via CGLIB, so inter-bean calls are intercepted and resolved from the singleton cache instead of executing the method body.
・Spring rewrites each @Bean method at compile time to always return the first object the method constructed during startup
Incorrect. Spring does not rewrite @Bean methods at compile time; the interception happens at runtime through a proxy, not by capturing the first constructed object.
・Spring caches every return value of any method declared inside a configuration class regardless of annotations
Incorrect. Only @Bean-annotated methods are intercepted for container management, not arbitrary methods declared in the class.
・Spring injects a fresh instance per call and later merges all the duplicates into one shared bean after refresh
Incorrect. Spring does not create duplicates and merge them; the proxy prevents duplicate creation by returning the cached singleton directly.
Explanation
Full @Configuration classes are proxied so that direct inter-bean method calls honor the container's scope and return the shared singleton.
Example11 · Spring Core & Dependency Injection
At a wealth-management firm, two beans implement the RiskScorer interface and startup fails with a NoUniqueBeanDefinitionException when another component autowires that interface by type. The architect wants one specific implementation selected at that single injection point without disturbing any other wiring in the application. Which approach satisfies exactly this requirement?
・Mark both competing RiskScorer implementations with @Primary so the container is always able to pick a winner whenever the type is ambiguous
Incorrect. Marking both as @Primary is contradictory and still leaves the container without a unique preferred candidate.
✓Add @Qualifier with the target bean name at that particular injection point so the container matches by qualifier there
Correct. @Qualifier at the injection point narrows the match to the named bean for that site only, leaving all other wiring untouched.
・Rename the autowired field to match one bean's id and rely on the container falling back to field-name resolution automatically
Incorrect. Field-name fallback exists but is implicit and brittle; it does not reliably express an intentional per-site selection the way @Qualifier does.
・Delete one of the two RiskScorer beans so only a single candidate of that type remains available
Incorrect. Deleting a bean changes application behavior and removes a needed implementation rather than targeting one injection point.
Explanation
@Qualifier is the precise, local mechanism to disambiguate a single injection point without changing global preferences or removing beans.
Example12 · Spring Core & Dependency Injection
At a manufacturing distributor, a developer creates a PricingService class annotated with @Service but the bean is never registered in the ApplicationContext. The class sits in the com.distributor.pricing package, while @ComponentScan only targets com.distributor.web. What is the most direct way to get Spring to register the bean?
✓Add com.distributor.pricing to the base packages scanned by the @ComponentScan declaration on the configuration class
Correct. Component scanning only registers stereotype-annotated classes found under the configured base packages, so adding the pricing package makes Spring detect the @Service bean.
・Replace the @Service annotation on PricingService with an explicit @Bean factory method inside the web configuration
Incorrect. Writing a @Bean method would work but it bypasses the stated goal of using component scanning and duplicates wiring that the stereotype already provides.
・Mark PricingService with @Lazy so the container defers and then performs the component scan of its package automatically
Incorrect. @Lazy only delays instantiation of an already-registered bean; it does nothing to broaden which packages are scanned for candidates.
・Move the @Configuration class itself into the com.distributor.pricing package so the default scan covers that location
Incorrect. Relocating the configuration class changes the default base package, but it is a disruptive restructuring rather than the direct fix of extending the scan targets.
Explanation
A @Service bean is only registered if its package falls within the configured @ComponentScan base packages; extending the scanned packages is the direct remedy.
Example13 · Spring Core & Dependency Injection
A warehouse module defines two beans implementing ShippingCalculator: 'groundShipping' and 'expressShipping'. An OrderDispatcher constructor injects a single ShippingCalculator and startup fails with NoUniqueBeanDefinitionException. The team wants OrderDispatcher to always receive the ground implementation. Which approach resolves this cleanly?
✓Annotate the ShippingCalculator constructor parameter in OrderDispatcher with @Qualifier("groundShipping") to select that bean
Correct. @Qualifier with the bean name narrows the candidate set to exactly one matching bean, resolving the ambiguity at the injection point.
・Delete the expressShipping bean definition entirely so that only one ShippingCalculator candidate remains available for injection
Incorrect. Removing the express bean destroys needed functionality elsewhere and is not a valid way to disambiguate injection.
・Change the injected field type from the ShippingCalculator interface to the concrete GroundShipping class the dispatcher needs
Incorrect. Depending on the concrete type couples the dispatcher to an implementation and defeats the purpose of programming to the interface.
・Add @Scope("prototype") to the groundShipping bean so a fresh instance is created and preferred during the constructor resolution
Incorrect. Changing the scope to prototype affects instance lifecycle, not candidate selection, so the ambiguity would still trigger the exception.
Explanation
When multiple beans of the same type exist, @Qualifier identifies the intended bean by name at the injection point, cleanly resolving NoUniqueBeanDefinitionException.
Example14 · Spring Core & Dependency Injection
A telecom operator's billing service class is annotated with @Service, and it declares a single constructor that takes a RatePlanRepository parameter. The RatePlanRepository is also a component in the scanned package. No @Autowired annotation appears anywhere in the billing service. When the context starts, what does Spring do with the RatePlanRepository dependency?
・Spring throws a NoSuchBeanDefinitionException because the constructor parameter was never explicitly marked with @Autowired for injection
Incorrect. A single constructor does not require an explicit @Autowired, so no exception is raised for the missing annotation here.
✓Spring injects the RatePlanRepository automatically because a single constructor makes @Autowired implicit for that constructor
Correct. Since Spring 4.3, a class with exactly one constructor has that constructor treated as autowired implicitly, so the repository is injected.
・Spring injects null into the constructor and logs a warning that the dependency annotation is missing from the parameter
Incorrect. Spring does not inject null and emit a warning; it resolves the single constructor and supplies the repository bean.
・Spring creates the bean but leaves the field unset until a setter is later invoked by the container at runtime
Incorrect. Constructor injection happens at creation time, and no setter deferral occurs for a single-constructor component.
Explanation
A component with exactly one constructor has that constructor treated as @Autowired implicitly, so Spring resolves and injects matching beans without an explicit annotation.
Example15 · Spring Core & Dependency Injection
A telecom provisioning module defines two beans of type NotificationChannel: smsChannel and emailChannel. A SubscriberService constructor requires a single NotificationChannel. At startup Spring reports an unsatisfied dependency because the type is ambiguous. Which approach cleanly selects smsChannel without removing the emailChannel bean?
✓Rename the SubscriberService constructor parameter to smsChannel so Spring matches the bean name against the parameter name during injection
Correct. Spring falls back to matching the parameter name against bean names, so naming the parameter smsChannel disambiguates while keeping both beans.
・Delete the emailChannel bean definition so that only one NotificationChannel candidate remains available for the constructor injection point
Incorrect. Deleting emailChannel removes a bean the module still needs, so it does not cleanly select smsChannel without loss.
・Mark both NotificationChannel beans as @Lazy so the container postpones resolving the ambiguity until the service is first invoked
Incorrect. @Lazy defers creation but does not resolve which of two candidates should be chosen for the injection point.
・Add @Primary to the emailChannel bean definition so Spring prefers it whenever a single NotificationChannel is requested
Incorrect. @Primary on emailChannel would select emailChannel, which is the opposite of the required smsChannel selection.
Explanation
When multiple candidates share a type, Spring can match the injection-point name to a bean name, so aligning the parameter name with smsChannel resolves the ambiguity without deleting beans.
▶ Try 10 free questions (graded)