0
0
Spring Bootframework~20 mins

@Component annotation in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Spring Component Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a class is annotated with @Component in Spring Boot?

Consider a class annotated with @Component in a Spring Boot application. What is the main effect of this annotation?

AThe class is ignored during component scanning and no bean is created.
BThe class is compiled into a separate executable jar file.
CSpring automatically detects the class and creates a singleton bean instance for dependency injection.
DThe class is only used for configuration properties and not for beans.
Attempts:
2 left
💡 Hint

Think about what Spring does with classes marked as components during startup.

📝 Syntax
intermediate
2:00remaining
Which of the following is the correct way to annotate a class as a Spring component?

Choose the correct syntax to mark a class as a Spring component using the @Component annotation.

A
import org.springframework.Component;

@Component
public class MyService {}
B
import org.springframework.stereotype.Component;

@Component
public class MyService {}
C
import org.springframework.stereotype.Component;

@Component()
public class MyService {}
D
import org.springframework.stereotype.Component;

@Component class MyService {}
Attempts:
2 left
💡 Hint

Check the correct package for the @Component annotation and Java syntax rules.

state_output
advanced
2:00remaining
What is the scope of a bean created by @Component by default?

When you annotate a class with @Component in Spring Boot without specifying any scope, what is the default scope of the bean?

ASingleton - a single shared instance is created and reused.
BPrototype - a new instance is created every time it is requested.
CRequest - a new instance is created per HTTP request.
DSession - a new instance is created per user session.
Attempts:
2 left
💡 Hint

Think about how Spring manages beans by default unless told otherwise.

🔧 Debug
advanced
2:00remaining
Why does Spring fail to inject a @Component bean in this example?

Given the following code, why does Spring fail to inject MyService into MyController?

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

@Component
public class MyService {}

@Controller
public class MyController {
    private final MyService myService;

    public MyController(MyService myService) {
        this.myService = myService;
    }
}
AThe package containing MyService is not scanned by Spring component scanning.
BMyService is missing the @Service annotation instead of @Component.
CConstructor injection is not supported in Spring Boot.
DMyController must be annotated with @Component instead of @Controller.
Attempts:
2 left
💡 Hint

Check if Spring can find the classes during scanning.

🧠 Conceptual
expert
3:00remaining
How does @Component differ from @Service and @Repository in Spring Boot?

Which statement best describes the difference between @Component, @Service, and @Repository annotations in Spring Boot?

A<code>@Component</code> disables component scanning, while <code>@Service</code> and <code>@Repository</code> enable it.
B<code>@Component</code> creates prototype beans, <code>@Service</code> creates singleton beans, and <code>@Repository</code> creates request-scoped beans.
C<code>@Component</code> is used only for configuration classes, <code>@Service</code> for controllers, and <code>@Repository</code> for data models.
D<code>@Component</code> is a generic stereotype for any Spring-managed component, while <code>@Service</code> and <code>@Repository</code> are specializations that add semantic meaning and specific behaviors.
Attempts:
2 left
💡 Hint

Think about the purpose and behavior of these annotations beyond just creating beans.