Bird
Raised Fist0
Spring Bootframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of the @Component annotation in Spring Boot?
easy
A. To mark a class so Spring automatically creates and manages its instance
B. To define a database entity
C. To configure application properties
D. To handle HTTP requests

Solution

  1. Step 1: Understand the role of @Component

    The @Component annotation tells Spring to create an instance of the class and manage it automatically.
  2. Step 2: Compare with other options

    The other options relate to other Spring features like entities, properties, or controllers, not @Component.
  3. Final Answer:

    To mark a class so Spring automatically creates and manages its instance -> Option A
  4. Quick Check:

    @Component = automatic management [OK]
Hint: Remember: @Component means Spring manages the class [OK]
Common Mistakes:
  • Confusing @Component with @Entity
  • Thinking it handles HTTP requests
  • Assuming it configures properties
2. Which of the following is the correct way to declare a Spring component class?
easy
A. @Component public class MyService {}
B. @Service public void MyService() {}
C. public class MyService {}
D. @Component void MyService() {}

Solution

  1. Step 1: Check annotation placement and class declaration

    The @Component annotation must be placed above a class declaration, like @Component public class MyService {}.
  2. Step 2: Identify incorrect syntax

    A plain public class misses the annotation, annotating a method with @Service or @Component is invalid since components must be classes.
  3. Final Answer:

    @Component public class MyService {} -> Option A
  4. Quick Check:

    Annotation + class declaration = correct syntax [OK]
Hint: Always put @Component above a class, not a method [OK]
Common Mistakes:
  • Annotating methods instead of classes
  • Missing the annotation entirely
  • Using wrong annotations for components
3. Given the code below, what will be the output when app.run() is called?
@Component
public class App {
  @Autowired
  private ServiceA serviceA;

  public void run() {
    System.out.println(serviceA.getName());
  }
}

@Component
public class ServiceA {
  public String getName() {
    return "ServiceA";
  }
}
medium
A. Runtime NullPointerException
B. null
C. ServiceA
D. Compilation error

Solution

  1. Step 1: Understand component scanning and injection

    Both App and ServiceA are annotated with @Component, so Spring creates and manages their instances. The @Autowired injects ServiceA into App.
  2. Step 2: Trace the method call

    Calling app.run() prints the result of serviceA.getName(), which returns "ServiceA".
  3. Final Answer:

    ServiceA -> Option C
  4. Quick Check:

    Autowired injects component = output string [OK]
Hint: If both classes are @Component, autowired works and prints value [OK]
Common Mistakes:
  • Assuming serviceA is null without injection
  • Confusing compilation vs runtime errors
  • Forgetting to annotate both classes
4. What is the problem with the following code snippet?
@Component
public class MyComponent {
  @Autowired
  private ServiceB serviceB;

  public void doWork() {
    serviceB.perform();
  }
}

Assuming ServiceB is not annotated with @Component or any stereotype, what will happen?
medium
A. Compilation error due to missing import
B. The code will compile and run fine
C. ServiceB will be automatically created by Spring
D. Spring will throw a runtime error due to missing bean

Solution

  1. Step 1: Check bean creation requirements

    Spring only manages classes annotated with @Component or similar. Since ServiceB lacks this, Spring won't create its bean.
  2. Step 2: Understand autowiring consequences

    When Spring tries to inject serviceB, it finds no bean and throws a runtime error (NoSuchBeanDefinitionException).
  3. Final Answer:

    Spring will throw a runtime error due to missing bean -> Option D
  4. Quick Check:

    Missing @Component = runtime injection error [OK]
Hint: All injected classes must be annotated for Spring to manage [OK]
Common Mistakes:
  • Assuming Spring creates beans without annotation
  • Confusing compile errors with runtime errors
  • Forgetting to annotate dependencies
5. You want to create two different implementations of an interface PaymentService and have Spring inject the correct one based on a custom qualifier. How should you use @Component to achieve this?
hard
A. Annotate only one implementation with @Component and inject by interface type
B. Annotate both implementations with @Component and use @Qualifier to specify which to inject
C. Use @Component only on the interface, not implementations
D. Create instances manually without @Component

Solution

  1. Step 1: Annotate both implementations

    Both classes implementing PaymentService must be annotated with @Component so Spring manages them.
  2. Step 2: Use @Qualifier to distinguish

    When injecting, use @Qualifier with the component name to tell Spring which implementation to use.
  3. Final Answer:

    Annotate both implementations with @Component and use @Qualifier to specify which to inject -> Option B
  4. Quick Check:

    Multiple components + qualifier = correct injection [OK]
Hint: Use @Component on all, @Qualifier to pick one [OK]
Common Mistakes:
  • Annotating only one implementation
  • Annotating interface instead of classes
  • Not using @Qualifier when multiple beans exist
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard