Spring Framework Spring Framework 3 — Questions and Answers
Question 1: Which Spring Data method naming convention generates a query to find records where a field is between two values?
- findByFieldRange
- findByFieldBetween (Correct answer)
- findByFieldFromTo
- findByFieldIn
Correct answer: findByFieldBetween
Spring Data interprets 'Between' in a method name to generate a BETWEEN SQL clause for the specified field.
Question 2: What is the default scope of a Spring bean?
- prototype
- request
- session
- singleton (Correct answer)
Correct answer: singleton
By default, Spring beans are singleton-scoped, meaning only one instance is created per ApplicationContext.
Question 3: In Spring AOP, what is a 'pointcut'?
- The actual code that runs as advice
- An expression that selects join points where advice should apply (Correct answer)
- The object created by the AOP framework
- A method that defines the target class
Correct answer: An expression that selects join points where advice should apply
A pointcut is a predicate or expression that matches join points, determining where advice will be applied.
Question 4: Which annotation would you use to inject the primary bean when multiple candidates exist in Spring?
- @Required
- @Primary (Correct answer)
- @Qualifier
- @Inject
Correct answer: @Primary
@Primary marks a bean as the default candidate when multiple beans of the same type exist and no @Qualifier is specified.
Question 5: What does the @RequestBody annotation do in a Spring MVC controller?
- Sends a response body to the client
- Maps the HTTP request body to a Java object via deserialization (Correct answer)
- Validates the request before processing
- Defines the content type of the response
Correct answer: Maps the HTTP request body to a Java object via deserialization
@RequestBody tells Spring MVC to deserialize the incoming HTTP request body (e.g., JSON) into the annotated parameter object.
Question 6: Which Spring Boot feature automatically configures beans based on the dependencies present on the classpath?
- Component scanning
- Auto-configuration (Correct answer)
- Dependency injection
- Bean post-processing
Correct answer: Auto-configuration
Spring Boot's auto-configuration uses @Conditional annotations to register beans only when specific classes or properties are present.
Question 7: In Spring's RestTemplate, which method sends a GET request and returns the response body as a specified type?
- postForObject
- exchange
- getForObject (Correct answer)
- delete
Correct answer: getForObject
getForObject sends a GET request and maps the response body to the given response type using message converters.
Which Spring Data method naming convention generates a query to find records where a field is between two values?