Spring Questions with Answers

13 practice questions on Spring (Advanced Java) with answers and step-by-step solutions. Try 10 samples below, then practise all 13 free in the AptiNest app.

Sample questions

Question 1Easy

What is Inversion of Control in Spring?

  1. Reversing the order in which methods are called at runtime
  2. The container builds and wires objects, not your code
  3. Running threads in reverse order of creation
  4. Storing application data in a database table

Answer: B. The container builds and wires objects, not your code

Explanation

Instead of code calling new for collaborators, the container creates and injects them.

Step-by-step solution

  1. Declare dependencies.
  2. Container builds them.
  3. Injects into your class.

Why the other options are wrong

  • A. It is not call order.
  • C. It has nothing to do with threads.
  • D. It is about object creation, not storage.

Question 2Easy

What does @SpringBootApplication combine?

  1. @Entity, @Id, @Table and @GeneratedValue together
  2. @Controller, @Service and @Repository together
  3. @Configuration, @EnableAutoConfiguration and @ComponentScan
  4. @Transactional, @Async and @EnableCaching together

Answer: C. @Configuration, @EnableAutoConfiguration and @ComponentScan

Explanation

It marks a configuration class, turns on Spring Boot auto configuration and scans the package for components.

Step-by-step solution

  1. Config class.
  2. Auto-configuration.
  3. Component scanning.

Why the other options are wrong

  • A. Those are JPA annotations.
  • B. Those are stereotype annotations, not config ones.
  • D. Those enable behaviours, not the app bootstrap.

Question 3Easy

@SpringBootApplication is a convenience that combines which three annotations?

  1. @Entity, @Table, @Id and @GeneratedValue together
  2. @Controller, @Service and @Repository
  3. @Autowired, @Qualifier and @Value used on every field
  4. @Configuration, @EnableAutoConfiguration and @ComponentScan

Answer: D. @Configuration, @EnableAutoConfiguration and @ComponentScan

Explanation

It marks a configuration class, switches on auto configuration based on the classpath, and scans the package for components.

Step-by-step solution

  1. Place the annotated class at the root package.
  2. Scanning finds the beans in sub packages.
  3. Auto configuration sets up defaults.

Why the other options are wrong

  • A. Those are JPA annotations.
  • B. Those are stereotype annotations scanned by it.
  • C. Those are injection annotations.

Question 4Easy

What does @Value("${server.port}") inject?

  1. A property value from the configuration
  2. A random free port chosen at startup
  3. The bean named server from the context
  4. A value from the incoming HTTP request header

Answer: A. A property value from the configuration

Explanation

${...} placeholders are resolved from properties files, environment or application config.

Step-by-step solution

  1. Placeholder is read from config.
  2. Value is converted to the field type.

Why the other options are wrong

  • B. It is not random.
  • C. It is not a bean reference (that would be #{...} or @Autowired).
  • D. It is not request data.

Question 5Medium

By default, when does a method annotated with @Transactional roll back?

  1. On every exception, including checked ones, with no configuration
  2. Never, it only rolls back when you call setRollbackOnly manually
  3. On unchecked exceptions (RuntimeException and Error)
  4. Only on SQLException

Answer: C. On unchecked exceptions (RuntimeException and Error)

Explanation

Spring's default rolls back for runtime exceptions and errors but commits for checked exceptions unless rollbackFor is set.

Step-by-step solution

  1. Method throws an exception.
  2. Spring checks the type.
  3. Unchecked leads to rollback, checked still commits.

Why the other options are wrong

  • A. Checked exceptions are excluded by default.
  • B. Rollback is automatic for unchecked ones.
  • D. SQLException is checked, so it does not roll back by default.

Tip: Use rollbackFor = Exception.class when needed.

Question 6Medium

What is the difference between @PathVariable and @RequestParam?

  1. PathVariable reads the URL path; RequestParam reads the query string or form
  2. PathVariable reads from request headers, RequestParam reads from cookies
  3. Both read only from the request body
  4. PathVariable is for POST requests only, RequestParam for GET only

Answer: A. PathVariable reads the URL path; RequestParam reads the query string or form

Explanation

For /users/7?sort=asc, 7 is a path variable and sort is a request parameter.

Step-by-step solution

  1. Path segment placeholders map to PathVariable.
  2. Query key-value pairs map to RequestParam.

Why the other options are wrong

  • B. Headers use @RequestHeader and cookies @CookieValue.
  • C. The body uses @RequestBody.
  • D. PathVariable works with any method.

Question 7Medium

Which statement about @RestController is correct?

  1. It is only meant for handlers that return JSP view names to be rendered
  2. It marks a class as a JPA repository that talks to the database
  3. It makes every method run in a new thread
  4. It combines @Controller and @ResponseBody, so return values are written to the response body

Answer: D. It combines @Controller and @ResponseBody, so return values are written to the response body

Explanation

A rest controller's handler return values are serialised (for example to JSON) directly instead of being resolved as view names.

Step-by-step solution

  1. Annotate the class with @RestController.
  2. Handlers return objects.
  3. A message converter writes them as the body.

Why the other options are wrong

  • A. View names are the job of plain @Controller.
  • B. Repositories use @Repository.
  • C. Threads come from the server, not this annotation.

Question 8Medium

In Spring MVC, how do @PathVariable and @RequestParam differ?

  1. @PathVariable reads request headers; @RequestParam reads cookies
  2. @PathVariable reads a value from the URL path; @RequestParam reads from the query string or form
  3. @PathVariable is for POST requests only; @RequestParam is for GET requests only
  4. They are aliases for the same thing

Answer: B. @PathVariable reads a value from the URL path; @RequestParam reads from the query string or form

Explanation

For /users/42?sort=asc, 42 binds with @PathVariable and sort with @RequestParam.

Step-by-step solution

  1. Path segment /users/{id} binds to @PathVariable.
  2. ?sort=asc binds to @RequestParam.
  3. Form fields also bind with @RequestParam.

Why the other options are wrong

  • A. Headers use @RequestHeader and cookies use @CookieValue.
  • C. Neither is limited to an HTTP method.
  • D. They bind different parts of the request.

Question 9Hard

A @Transactional method is called from another method in the same class (self-invocation). What is the usual result in proxy-based Spring AOP?

  1. The transaction is applied twice, nested inside itself
  2. An exception is thrown at startup
  3. The call becomes asynchronous
  4. The transactional behaviour is not applied

Answer: D. The transactional behaviour is not applied

Explanation

Transactions are applied by a proxy around the bean. A direct internal call bypasses the proxy, so no transaction starts.

Step-by-step solution

  1. Caller bean receives a proxy.
  2. Internal this.method() skips the proxy.
  3. No advice is applied.

Why the other options are wrong

  • A. No double application happens.
  • B. Spring does not fail at startup for this.
  • C. Async is a separate feature.

Tip: Move the method to another bean.

Question 10Hard

Inside one bean, method a() calls another method b() of the same class and b() is annotated with @Transactional. Called through a(), what happens by default with proxy based AOP?

  1. The advice is skipped, because the call bypasses the proxy
  2. A new transaction always starts because the annotation is on the method
  3. An exception is thrown at startup
  4. Both methods run in separate threads

Answer: A. The advice is skipped, because the call bypasses the proxy

Explanation

Spring wraps the bean in a proxy. A self call uses this, not the proxy, so the interceptor never runs and no transaction begins for b().

Step-by-step solution

  1. External callers go through the proxy.
  2. a() calls this.b() directly.
  3. The transactional interceptor is skipped.

Why the other options are wrong

  • B. That would need the proxy to see the call.
  • C. Startup does not detect self calls.
  • D. No threads are created.

Tip: Move b() to another bean.

Practise all 13 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Advanced Java topics · All topics