Sample questions
Question 1Easy
Which annotation marks a class holding business logic?
- @Service
- @RestController
- @Bean
- @Entity
Answer: A. @Service
Explanation
@Service is a specialisation of @Component used for the service layer.
Step-by-step solution
- Business logic belongs in the service layer.
- @Service marks a class as a service-layer bean.
- @RestController handles HTTP, @Bean declares a bean and @Entity maps a table.
- So the answer is @Service.
Why the other options are wrong
- B. @RestController marks a web controller, not business logic.
- C. @Bean declares a bean in a configuration class.
- D. @Entity maps a database table.
Tip: @Controller for web, @Service for logic, @Repository for data.
Question 2Easy
@SpringBootApplication combines which annotations?
- @SpringBootConfiguration, @EnableAutoConfiguration and @ComponentScan
- @Configuration, @EnableWebMvc and @EntityScan
- @Controller, @Service, @Repository and @Component
- @Bean, @Value, @Profile and @PropertySource
Answer: A. @SpringBootConfiguration, @EnableAutoConfiguration and @ComponentScan
Explanation
It marks a configuration class, enables auto-configuration and scans the package and its sub-packages.
Step-by-step solution
- @SpringBootApplication is a combined annotation.
- It includes @SpringBootConfiguration, which marks a configuration class.
- It includes @EnableAutoConfiguration and @ComponentScan.
- So the answer is @SpringBootConfiguration, @EnableAutoConfiguration and @ComponentScan.
Why the other options are wrong
- B. @EnableWebMvc and @EntityScan are not part of @SpringBootApplication.
- C. @Controller, @Service and similar are stereotypes, not its parts.
- D. @Bean, @Value and @Profile are separate annotations.
Tip: @SpringBootApplication = @SpringBootConfiguration + @EnableAutoConfiguration + @ComponentScan.
Question 3Easy
@Configuration classes are typically used to:
- Write unit tests
- Handle HTTP requests
- Map database tables
- Declare beans
Answer: D. Declare beans
Explanation
They are Java-based replacements for XML bean definitions.
Step-by-step solution
- A @Configuration class is a Java class that configures the container.
- Its @Bean methods declare the beans.
- It replaces XML bean definitions.
- So @Configuration classes declare beans.
Why the other options are wrong
- A. Unit tests are written as test classes.
- B. HTTP requests are handled by controllers.
- C. Database tables are mapped with @Entity.
Tip: @Configuration = Java-based bean definitions.
Question 4Easy
What does @Value("${app.timeout:30}") inject?
- Always 30
- The environment variable TIMEOUT
- The literal text ${app.timeout:30}
- app.timeout, else 30
Answer: D. app.timeout, else 30
Explanation
The part after the colon is a default value.
Step-by-step solution
- ${app.timeout:30} reads the property app.timeout.
- If the property is not set, the default after the colon is used.
- The default here is 30.
- So it injects app.timeout, else 30.
Why the other options are wrong
- A. The property is read first, so the value is not always 30.
- B. The environment variable TIMEOUT is not read here.
- C. The placeholder is resolved, so the literal text is not injected.
Tip: ${key:default} = use the property if set, otherwise the default.
Question 5Medium
Which annotation restricts a bean to be created only when a given profile is active?
- @Profile
- @Primary
- @Scope
- @Order
Answer: A. @Profile
Explanation
@Profile("dev") registers the bean only when the dev profile is active.
Step-by-step solution
- @Profile limits a bean to one or more named profiles.
- The bean is registered only when that profile is active.
- @Primary, @Scope and @Order do different jobs.
- So the answer is @Profile.
Why the other options are wrong
- B. @Primary chooses a default bean; it does not check profiles.
- C. @Scope sets the bean's lifecycle, such as singleton or prototype.
- D. @Order sets ordering, not activation.
Tip: @Profile("dev") = create this bean only when dev is active.
Question 6Medium
Which stereotype annotation adds translation of persistence exceptions into Spring's DataAccessException hierarchy?
- @Controller
- @Repository
- @Service
- @Configuration
Answer: B. @Repository
Explanation
@Repository beans get exception translation via a post-processor.
Step-by-step solution
- Persistence exceptions come from the data layer.
- @Repository enables translation of them into Spring's DataAccessException hierarchy.
- @Controller, @Service and @Configuration do not add this.
- So the answer is @Repository.
Why the other options are wrong
- A. @Controller marks a web controller.
- C. @Service marks business logic.
- D. @Configuration declares beans.
Tip: @Repository = data access class with exception translation.
Question 7Medium
Which annotation runs a method asynchronously in a separate thread, once enabled with @EnableAsync?
- @Thread
- @Async
- @Parallel
- @Background
Answer: B. @Async
Explanation
@Async methods run in a task executor and can return CompletableFuture.
Step-by-step solution
- Add @EnableAsync on a configuration class.
- Annotate the method with @Async.
- Spring runs it in a task executor thread.
Why the other options are wrong
- A. No such Spring annotation exists.
- C. No such Spring annotation exists.
- D. No such Spring annotation exists.
Question 8Medium
What is the purpose of @EnableScheduling together with @Scheduled(fixedRate = 5000)?
- Limit requests to 5000 per second
- Run a method every 5 seconds
- Delay startup by 5 seconds
- Cache results for 5000 minutes
Answer: B. Run a method every 5 seconds
Explanation
fixedRate is in milliseconds by default.
Step-by-step solution
- @EnableScheduling turns on scheduled task support.
- @Scheduled(fixedRate = 5000) runs the method at a fixed rate.
- The fixedRate value is in milliseconds, so 5000 is 5 seconds.
- So the method runs every 5 seconds.
Why the other options are wrong
- A. This is not a request limit; it is a schedule.
- C. The value sets the interval, not a startup delay.
- D. This is not a cache setting.
Tip: FixedRate is in milliseconds: 5000 = 5 seconds.
Question 9Hard
Why is @ConfigurationProperties often preferred over many @Value annotations?
- @Value does not work in Spring Boot
- It is the only way to read YAML
- It encrypts the properties
- Typed, grouped binding
Answer: D. Typed, grouped binding
Explanation
@ConfigurationProperties(prefix = "app") maps related settings with type conversion and optional validation.
Step-by-step solution
- @ConfigurationProperties binds a group of settings to a typed class.
- It converts types and can validate the values.
- This is cleaner than many separate @Value fields.
- So the reason is typed, grouped binding.
Why the other options are wrong
- A. @Value works in Spring Boot.
- B. YAML can also be read by other means; this is not the only way.
- C. Binding does not encrypt the properties.
Tip: Use @ConfigurationProperties for groups of related settings.
Question 10Hard
In a @Configuration class (full mode), one @Bean method calls another @Bean method. What does the call return?
- A compile error
- null
- A brand-new object every time
- The container's singleton
Answer: D. The container's singleton
Explanation
Configuration classes are CGLIB-proxied so inter-bean method calls return the managed bean.
Step-by-step solution
- In full mode, @Configuration classes are proxied.
- When one @Bean method calls another, the proxy intercepts the call.
- It returns the container's existing singleton bean.
- So the call returns the container's singleton.
Why the other options are wrong
- A. The call compiles; it is a valid Java method call.
- B. The call does not return null.
- C. The call does not create a new object each time in full mode.
Tip: Inside @Configuration, calling a @Bean method returns the managed singleton.
Practise all 15 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 →